标签:
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; const int maxn = 205; int cost[maxn][maxn]; int n, m, u, v, c, su, eu; int mincost[maxn], s[maxn]; int main() { int i, j, ii; while (~scanf("%d%d", &n, &m)) { for (i = 0; i <= n; i++)for (j = 0; j <= n; j++)cost[i][j] = 999999999; for (i = 0; i < m; i++) { scanf("%d%d%d", &u, &v, &c); if (c < cost[u][v]) cost[u][v] = c, cost[v][u] = c; } scanf("%d%d", &su, &eu); for (i = 0; i < n; i++) mincost[i] = cost[su][i], s[i] = 0; s[su] = 1; int x; mincost[su] = 0; for (ii = 0; ii < n - 1; ii++) { int minn = 999999999, flag = 0; for (i = 0; i < n; i++) if (!s[i] && mincost[i] < minn) minn = mincost[i], v = i, flag = 1; if (!flag) continue; s[v] = 1; for (i = 0; i < n; i++) if (!s[i] && mincost[v] + cost[v][i] < mincost[i]) mincost[i] = mincost[v] + cost[v][i]; } if (mincost[eu] != 999999999) printf("%d\n", mincost[eu]); else printf("-1\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zufezzt/p/4495191.html