最短路(Dijkstra模板题)

就不写题目链接了

Sample Input

5 5  点个数a,边个数b
1 2 20      点,点,权值
2 3 30
3 4 20
4 5 20
1 5 100      
          求出1到a的最短距离

Sample Output

90
防止有重边
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 #define Max 1000+10
 5 #define INF 0x3f3f3f3f
 6 int cost[Max][Max];
 7 int lowcost[Max];
 8 bool vis[Max];
 9 int N,T;
10 void dijkstra(int s)
11 {
12 
13     int i,u,v;
14     for(u=1;u<=N;u++)
15     {
16         lowcost[u]=INF;
17         vis[u]=0;
18     }
19     lowcost[1]=0;
20     int Min=INF;
21     
22     while(1)
23     {
24         int p=-1;
25         for(u=1;u<=N;u++)
26         {
27             if(!vis[u]&&(p==-1||lowcost[u]<lowcost[p]))
28                 p=u;
29         }
30         if(p==-1)    break;
31         vis[p]=1;
32         for(u=1;u<=N;u++)
33             if(!vis[u]&&(lowcost[p]+cost[p][u])<lowcost[u])
34                 lowcost[u]=lowcost[p]+cost[p][u];
35     }
36 }
37 int main()
38 {
39     int i,j;
40     int a,b,w;
41     freopen("in.txt","r",stdin);
42     while(scanf("%d%d",&T,&N)!=EOF)
43     {
44         for(i=1;i<=N;i++)
45         {
46             for(j=1;j<=N;j++)
47             {
48                 if(j==i)
49                     cost[i][j]=0;
50                 else
51                     cost[i][j]=cost[j][i]=INF;
52             }
53         }
54         for(i=0;i<T;i++)
55         {
56             scanf("%d%d%d",&a,&b,&w);
57             if(w<cost[a][b])
58             {
59                 cost[a][b]=cost[b][a]=w;
60             }
61         }
62         dijkstra(1);
63         printf("%d\n",lowcost[N]);
64     }
65 }
View Code
Sample Input
3 3   点,边数
0 1 1     边
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 using namespace std;
 4 #define Max 200+10
 5 #define INF 0x3f3f3f3f
 6 int cost[Max][Max];
 7 int lowcost[Max];
 8 bool vis[Max];
 9 int N,M;
10 void dijkstra(int s)
11 {
12     int u,i,p;
13     for(u=0;u<N;u++)
14     {
15         vis[u]=0;
16         lowcost[u]=INF;
17     }
18     lowcost[s]=0;
19     while(1)
20     {
21         p=-1;
22         for(u=0;u<N;u++)
23             if(!vis[u]&&(p==-1||lowcost[u]<lowcost[p]))
24                 p=u;
25         if(p==-1)    break;
26         vis[p]=1;
27         for(u=0;u<N;u++)
28             if(!vis[u]&&(lowcost[p]+cost[u][p])<lowcost[u])
29                 lowcost[u]=lowcost[p]+cost[u][p];
30     }
31 }
32 int main()
33 {
34     int i,j;
35     int a,b,w,s,e;
36     freopen("in.txt","r",stdin);
37     while(~scanf("%d%d",&N,&M))
38     {
39 
40         for(i=0;i<N;i++)
41             for(j=0;j<N;j++)
42                 if(i==j)    cost[i][j]=cost[j][i]=0;
43                 else    cost[i][j]=cost[j][i]=INF;
44 
45         for(i=0;i<M;i++)
46         {
47             scanf("%d%d%d",&a,&b,&w);
48             if(w<cost[a][b])    cost[a][b]=cost[b][a]=w;        /*解决重边*/
49         }
50         scanf("%d%d",&s,&e);
51         dijkstra(s);
52         if(lowcost[e]==INF)
53             printf("-1\n");
54         else
55             printf("%d\n",lowcost[e]);
56     }
57 }
View Code

 

posted @ 2015-11-25 20:19  御心飞行  阅读(398)  评论(0编辑  收藏  举报