UVa 10986 Dijkstra TLE
时限是 3s, 当然不能用 dijksra;
N 最大是 20005,数组 d[N][N] 可以开;
------------------------------------------------------------------------
# include <stdio.h> # include <string.h> # define N 20005 # define M 50005 # define INF N*M int n, m, s, t; int w[N][N]; int min(int x, int y) { return (x<y ? x:y); } void init(void) { int i, j, k, c; scanf("%d%d%d%d", &n, &m, &s, &t); for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) { w[i][j] = INF; } for (k = 1; k <= m; ++k) { scanf("%d%d%d", &i, &j, &c); w[i][j] = w[j][i] = c; } } void solve(void) { char v[N]; int d[N], m, x, i, y; memset(v, 0, sizeof(v)); for (i = 0; i < n; ++i) d[i] = (i==s ? 0:INF); for (i = 0; i < n; ++i) { m = INF; for (y = 0; y < n; ++y) if (!v[y] && d[y]<m) m = d[x = y]; //if (x == t) break; v[x] = 1; for (y = 0; y < n; ++y) d[y] = min(d[y], d[x]+w[x][y]); } if (d[t] >= INF) printf("unreachable\n"); else printf("%d\n", d[t]); } int main() { int T, i; scanf("%d", &T); for (i = 1; i <= T; ++i) { printf("Case #%d: ", i); init(); solve(); } return 0; }
------------------------------------------------------------------------