【HDOJ】2722 Here We Go(relians) Again

时间:2021-10-22 08:55:37

根据矩阵建图,然后求最短路径。

 #include <cstdio>
#include <cstring>
#include <cstdlib> #define L 2520
#define MAXN 600
#define INF 0x2f2f2f2f int map[MAXN][MAXN];
int dis[MAXN];
int path[MAXN];
int vn;
bool visit[MAXN]; void dijkstra() {
int i, j, v, min; memset(visit, false, sizeof(visit));
memset(path, , sizeof(path));
visit[] = true;
dis[] = ;
for (i=; i<vn; ++i)
dis[i] = map[][i];
for (j=; j<vn; ++j) {
min = INF;
for (i=; i<vn; ++i) {
if (!visit[i] && dis[i]<min) {
min = dis[i];
v = i;
}
}
if (min == INF)
break;
visit[v] = true;
for (i=; i<vn; ++i) {
if (!visit[i] && dis[i]>min+map[v][i]) {
dis[i] = min + map[v][i];
path[i] = v;
}
}
}
} void output() {
int i, j; for (i=; i<vn; ++i) {
for (j=; j<vn; ++j) {
printf("%d ", map[i][j]);
}
printf("\n");
}
} int main() {
int n, m;
char dir[];
int i, j, k, v, r; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif while (scanf("%d %d",&n,&m)!=EOF && (n||m)) {
vn = (m+) * (n+);
memset(map, 0x2f, sizeof(map));
r = ;
for (j=; j<=m; ++j) {
scanf("%d %s", &v, dir);
if (v == ) {
map[j-][j] = map[j][j-] = INF;
} else {
if (dir[] == '<' || dir[] == '*')
map[j][j-] = L/v;
if (dir[] == '>' || dir[] == '*')
map[j-][j] = L/v;
}
}
k = m+;
for (i=; i<=n; ++i) {
// column
for (j=; j<=m; ++j) {
scanf("%d %s", &v, dir);
if (v == ) {
map[r+j][r+k+j] = map[r+k+j][r+j] = INF;
} else {
if (dir[] == '^' || dir[] == '*')
map[r+k+j][r+j] = L/v;
if (dir[] == 'v' || dir[] == '*')
map[r+j][r+k+j] = L/v;
}
}
r += k;
// row
for (j=; j<=m; ++j) {
scanf("%d %s", &v, dir);
if (v == ) {
map[r+j-][r+j] = map[r+j][r+j-] = INF;
} else {
if (dir[] == '<' || dir[] == '*')
map[r+j][r+j-] = L/v;
if (dir[] == '>' || dir[] == '*')
map[r+j-][r+j] = L/v;
}
}
}
dijkstra();
if (dis[vn-] == INF)
printf("Holiday\n");
else
printf("%d blips\n", dis[vn-]);
} return ;
}