3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2
2 -1
又一次跪到重复路径上了,啊啊啊啊!以后一定要注意!
代码如下:
#include <cstdio> #include <algorithm> #define MAX 111111111 using namespace std; int dp[211][211]; int dis[211]; int used[211]; int n,m; int st,en; int min(int a,int b) { if (a < b) return a; return b; } void init() { for (int i = 0 ; i < n ; i++) { dis[i] = MAX; used[i] = 0; for (int j = 0 ; j < n ; j++) { dp[i][j] = MAX; dp[j][i] = MAX; } } } void dijkstra() { dis[st] = 0; while (1) { int v = -1; for (int i = 0 ;i < n ; i++) { if (used[i] == 0 && (v == -1 || dis[i] < dis[v])) v = i; } if (v == -1) break; used[v] = 1; for (int i = 0 ; i < n ; i++) { if (dis[i] > (dis[v] + dp[v][i])) dis[i] = dis[v] + dp[v][i]; } } } int main() { while (~scanf ("%d %d",&n,&m)) { init(); while (m--) { int t1,t2,t3; scanf ("%d %d %d",&t1,&t2,&t3); dp[t1][t2] = dp[t2][t1] =min(t3,dp[t1][t2]); //跪了 } scanf ("%d %d",&st,&en); dijkstra(); if (dis[en] < MAX) printf ("%d\n",dis[en]); else printf ("-1\n"); } return 0; }