hdu 2544
求点1到点n的最短路 无向图
Sample Input
2 1 //结点数 边数
1 2 3 //u v w
3 3
1 2 5
2 3 5
3 1 2
0 0
Sample Output
3
2
堆优化Dijstra模板
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # include <queue> 7 # define LL long long 8 using namespace std ; 9 10 const int INF=0x3f3f3f3f; 11 const int MAXN=110; 12 struct qnode 13 { 14 int v; 15 int c; 16 qnode(int _v=0,int _c=0):v(_v),c(_c){} 17 bool operator <(const qnode &r)const 18 { 19 return c>r.c; 20 } 21 }; 22 struct Edge 23 { 24 int v,cost; 25 Edge(int _v=0,int _cost=0):v(_v),cost(_cost){} 26 }; 27 vector<Edge>E[MAXN]; 28 bool vis[MAXN]; 29 int dist[MAXN]; 30 int n ; 31 void Dijkstra(int start)//点的编号从1开始 32 { 33 memset(vis,false,sizeof(vis)); 34 for(int i=1;i<=n;i++)dist[i]=INF; 35 priority_queue<qnode>que; 36 while(!que.empty())que.pop(); 37 dist[start]=0; 38 que.push(qnode(start,0)); 39 qnode tmp; 40 while(!que.empty()) 41 { 42 tmp=que.top(); 43 que.pop(); 44 int u=tmp.v; 45 if(vis[u])continue; 46 vis[u]=true; 47 for(int i=0;i<E[u].size();i++) 48 { 49 int v=E[tmp.v][i].v; 50 int cost=E[u][i].cost; 51 if(!vis[v]&&dist[v]>dist[u]+cost) 52 { 53 dist[v]=dist[u]+cost; 54 que.push(qnode(v,dist[v])); 55 } 56 } 57 } 58 } 59 void addedge(int u,int v,int w) 60 { 61 E[u].push_back(Edge(v,w)); 62 } 63 64 int main () 65 { 66 // freopen("in.txt","r",stdin) ; 67 int m ; 68 while (scanf("%d %d" , &n , &m) !=EOF) 69 { 70 if (n==0 && m==0) 71 break ; 72 int u , v , w ; 73 int i , j ; 74 for(i=1;i<=n;i++) 75 E[i].clear(); 76 77 while(m--) 78 { 79 scanf("%d%d%d" , &u , &v , &w) ; 80 addedge(u,v,w) ; 81 addedge(v,u,w) ; 82 } 83 Dijkstra(1) ; 84 printf("%d\n" , dist[n]) ; 85 } 86 87 return 0 ; 88 }
hdu 1874
输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.
这题有重边
Sample Input
3 3 //结点数 边数
0 1 1//u v w
0 2 3
1 2 1
0 2 //起点 终点
3 1
0 1 1
1 2
Sample Output
2
-1
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # define LL long long 7 using namespace std ; 8 9 const int MAXN=300; 10 const int INF=0x3f3f3f3f; 11 int n ; 12 bool vis[MAXN]; 13 int cost[MAXN][MAXN] ; 14 int lowcost[MAXN] ; 15 int pre[MAXN]; 16 void Dijkstra(int beg) 17 { 18 for(int i=0;i<n;i++) 19 { 20 lowcost[i]=INF;vis[i]=false;pre[i]=-1; 21 } 22 lowcost[beg]=0; 23 for(int j=0;j<n;j++) 24 { 25 int k=-1; 26 int Min=INF; 27 for(int i=0;i<n;i++) 28 if(!vis[i]&&lowcost[i]<Min) 29 { 30 Min=lowcost[i]; 31 k=i; 32 } 33 if(k==-1) 34 break ; 35 vis[k]=true; 36 for(int i=0;i<n;i++) 37 if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i]) 38 { 39 lowcost[i]=lowcost[k]+cost[k][i]; 40 pre[i]=k; 41 } 42 } 43 44 } 45 46 int main () 47 { 48 // freopen("in.txt","r",stdin) ; 49 int m ; 50 while (scanf("%d %d" , &n , &m) !=EOF) 51 { 52 53 int u , v , w ; 54 int i , j ; 55 for (i = 0 ; i < n ; i++) 56 for (j = 0 ; j < n ; j++) 57 cost[i][j] = INF ; 58 while(m--) 59 { 60 scanf("%d%d%d" , &u , &v , &w) ; 61 if (w < cost[u][v]) //防止重边 62 { 63 cost[u][v] = w ; 64 cost[v][u] = w ; 65 } 66 } 67 scanf("%d %d" , &u , &v) ; 68 Dijkstra(u) ; 69 if (lowcost[v] != INF) 70 printf("%d\n" , lowcost[v]) ; 71 else 72 printf("-1\n") ; 73 } 74 75 return 0 ; 76 }
poj 2387
有重边 无向图
Sample Input
5 5 // m n
1 2 20 //u v w
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # define LL long long 7 using namespace std ; 8 9 const int MAXN=1100; 10 const int INF=0x3f3f3f3f; 11 int n ; 12 bool vis[MAXN]; 13 int cost[MAXN][MAXN] ; 14 int lowcost[MAXN] ; 15 int pre[MAXN]; 16 void Dijkstra(int beg) 17 { 18 for(int i=0;i<n;i++) 19 { 20 lowcost[i]=INF;vis[i]=false;pre[i]=-1; 21 } 22 lowcost[beg]=0; 23 for(int j=0;j<n;j++) 24 { 25 int k=-1; 26 int Min=INF; 27 for(int i=0;i<n;i++) 28 if(!vis[i]&&lowcost[i]<Min) 29 { 30 Min=lowcost[i]; 31 k=i; 32 } 33 if(k==-1) 34 break ; 35 vis[k]=true; 36 for(int i=0;i<n;i++) 37 if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i]) 38 { 39 lowcost[i]=lowcost[k]+cost[k][i]; 40 pre[i]=k; 41 } 42 } 43 44 } 45 46 int main () 47 { 48 // freopen("in.txt","r",stdin) ; 49 int m ; 50 while (scanf("%d %d" , &m , &n) !=EOF) 51 { 52 53 int u , v , w ; 54 int i , j ; 55 for (i = 0 ; i < n ; i++) 56 for (j = 0 ; j < n ; j++) 57 { 58 if (i == j) 59 cost[i][j] = 0 ; 60 else 61 cost[i][j] = INF ; 62 } 63 while(m--) 64 { 65 scanf("%d%d%d" , &u , &v , &w) ; 66 if (w < cost[u-1][v-1]) //防止重边 67 { 68 cost[u-1][v-1] = w ; 69 cost[v-1][u-1] = w ; 70 } 71 } 72 Dijkstra(n-1) ; 73 if (lowcost[0] != INF) 74 printf("%d\n" , lowcost[0]) ; 75 76 } 77 78 return 0 ; 79 }