标签:style blog http java color os
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
3 2
解题:最短路。。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #define LL long long 13 #define INF 0x3f3f3f 14 using namespace std; 15 int mp[110][110],d[110],n,m; 16 bool vis[110]; 17 void dij(){ 18 int i,j,theMin,index; 19 for(i = 0; i <= n; i++) 20 d[i] = INF>>1; 21 d[1] = 0; 22 memset(vis,false,sizeof(vis)); 23 for(i = 0; i < n; i++){ 24 theMin = INF; 25 for(j = 1; j <= n; j++){ 26 if(!vis[j] && theMin > d[j]) theMin = d[index = j]; 27 } 28 vis[index] = true; 29 if(index == n) break; 30 for(j = 1; j <= n; j++){ 31 if(!vis[j] && d[j] > d[index]+mp[index][j]) 32 d[j] = d[index] + mp[index][j]; 33 } 34 } 35 } 36 int main(){ 37 int i,j,u,v,w; 38 while(scanf("%d%d",&n,&m),n||m){ 39 for(i = 0; i <= n; i++) 40 for(j = 0; j <= n; j++) 41 mp[i][j] = INF; 42 for(i = 0; i < m; i++){ 43 scanf("%d%d%d",&u,&v,&w); 44 mp[u][v] = mp[v][u] = w; 45 } 46 dij(); 47 printf("%d\n",d[n]); 48 } 49 return 0; 50 }
图论trainning-part-1 A. 最短路,布布扣,bubuko.com
标签:style blog http java color os
原文地址:http://www.cnblogs.com/crackpotisback/p/3858673.html