标签:cst output code stream ems 最大的 other void ott
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 50818 Accepted Submission(s): 16897
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int INF = 1 << 20; // INF 的设置一定要足够的大 const int maxn = 1000 + 5; int dist[maxn], map[maxn][maxn], vis[maxn]; int T, S, D, n; void Init() { for (int i = 0; i < maxn; i++) // 只能小于,不能等于,否则会TLE!! { for (int j = 0; j < maxn; j++) // 只能小于,不能等于, 否则会TLE!! map[i][j] = (i == j ? 0 : INF); } int st, end, time; n = 0; for (int i = 0; i < T; i++) { scanf("%d%d%d", &st, &end, &time); if (map[st][end] > time) // 有重边 map[st][end] = map[end][st] = time; // 找出最大边的编号 n = max(n, max(st, end)); } n++; // 假设是理想中的终点(比最大的顶点大1)的编号 for (int i = 0; i < S; i++) { scanf("%d", &st); map[0][st] = map[st][0] = 0; // 草儿家到相邻城市的距离为0 } for (int i = 0; i < D; i++) { scanf("%d", &st); map[st][n] = map[n][st] = 0; // 想去的地方到理想中的终点距离为0 } } void Dijkstra() { for (int i = 0; i <= n; i++) // 从0改为1 dist[i] = map[0][i]; // 以草儿家和她相邻的点作为起点,求出该起点到相邻点的时间 memset(vis, 0, sizeof(vis)); for (int i = 0; i <= n; i++) // 0 改为 1 也行 { int u; int maxx = INF; for (int j = 0; j <= n; j++) { if (!vis[j] && dist[j] < maxx) maxx = dist[u=j]; } vis[u] = 1; for (int j = 0; j <= n; j++) { if (dist[j] > dist[u] + map[u][j]) dist[j] = dist[u] + map[u][j]; } } } int main() { while (scanf("%d%d%d", &T, &S, &D) != EOF) { Init(); Dijkstra(); printf("%d\n", dist[n]); } return 0; }
标签:cst output code stream ems 最大的 other void ott
原文地址:https://www.cnblogs.com/-citywall123/p/9523058.html