hdu 2544 最短路
/* 题意:中文题 题解:最短路,大水题dijkstra直接过 */ #include <cstdio> #include <cstring> int map[105][105]; int cost[105],dist[105],rank[105],vis[105]; int max,n; int dijkstra()//迪杰斯特拉算法求最短路 { int i,j; int node,sd; for(i=1; i<=n; i++) dist[i] = map[1][i]; memset(vis,0,sizeof(vis)); for(i=1; i<=n; i++) { node = 0; sd = 2147483647; for(j=1; j<=n; j++) { if (!vis[j] && sd>dist[j])//找出未访问的点中路径最短的点并赋给node { sd = dist[j]; node = j; } } if (node == 0)//当点已经遍历完,break; break; vis[node] = 1; for(j=1; j<=n; j++) if (!vis[j] && map[node][j]>0 && dist[j]>dist[node]+map[node][j]) //以node为基点对其它未访问的点进行松弛 dist[j] = dist[node]+map[node][j]; } return dist[n]; } int main(void) { int m; while (~scanf("%d%d",&n,&m), n || m) { for(int i=0; i<=n; i++) for(int j=0; j<=n; j++) if (i != j) map[i][j] = 1e8; while (m--) { int u,v,w; scanf("%d%d%d",&u,&v,&w); map[u][v] = map[v][u] = w; } printf("%d\n",dijkstra()); } return 0; }