ACM 第四天

A - 最短路

在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?


Input输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
输入保证至少存在1条商店到赛场的路线。
Output对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间Sample Input
2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0
Sample Output
3
2
 1 #include<stdio.h>
 2 #include<cstring>
 3 const int N=105,INF=9999999;
 4 int d[N],w[N][N],vis[N],n,m;
 5 void dij(int src)
 6 {
 7     for(int i=1; i<=n; i++)
 8         d[i]=INF;
 9     d[src]=0;
10     memset(vis,0,sizeof(vis));
11     for(int i=1; i<=n; i++)
12     {
13         int u=-1;
14         for(int j=1; j<=n; j++)
15             if(!vis[j])
16             {
17                 if(u==-1 || d[j]<d[u]) u=j;
18             }
19         vis[u]=1;
20         for(int j=1; j<=n; j++)
21             if(!vis[j])
22             {
23                 int tmp=d[u]+w[u][j];
24                 if(tmp<d[j]) d[j]=tmp;
25             }
26     }
27 }
28 int main()
29 {
30     int a,b,c;
31     while(~scanf("%d %d",&n,&m) &&( n|| m) )
32     {
33         for(int i=1; i<=n; i++)
34         {
35             w[i][i]=INF;
36             for(int j=i+1; j<=n; j++)
37             {
38                 w[i][j]=w[j][i]=INF;
39             }
40         }
41         for(int i=0; i<m; i++)
42         {
43             scanf("%d %d %d",&a,&b,&c);
44             w[a][b]=w[b][a]=c;
45         }
46         dij(1);
47         printf("%d\n",d[n]);
48     }
49     return 0;
50 }
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.

InputThe first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
OutputYou should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
Sample Output
179
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 using namespace std;
 7 #define N 150
 8 #define INF 99999999
 9 int ma[N][N];
10 long long d[N],vis[N];
11 int n;
12 long long prim(int s)
13 {
14     for(int i=1; i<=n; i++)
15         d[i]=i==s?0:ma[s][i];
16     vis[s]=1;
17     long long ans=0;
18     for(int i=1; i<n; i++)
19     {
20         int maxn=INF,v;
21         for(int j=1; j<=n; j++)
22             if(!vis[j]&&maxn>d[j])
23             {
24                 maxn=d[j];
25                 v=j;
26             }
27         vis[v]=1;
28         ans+=maxn;
29         for(int j=1; j<=n; j++)
30             if(!vis[j]&&ma[v][j]<d[j])
31                 d[j]=ma[v][j];
32     }
33     return ans;
34 }
35 int main()
36 {
37     int m;
38     int s,e;
39     while(~scanf("%d",&n))
40     {
41         memset(vis,0,sizeof(vis));
42         for(int i=1; i<=n; i++)
43             for(int j=1; j<=n; j++)
44                 scanf("%d",&ma[i][j]);
45         scanf("%d",&m);
46         while(m--)
47         {
48             scanf("%d %d",&s,&e);
49             ma[s][e]=0;
50             ma[e][s]=0;
51         }
52         long long ans=prim(1);
53         printf("%lld\n",ans);
54     }
55     return 0;
56 }

猜你喜欢

转载自www.cnblogs.com/weixq351/p/9375758.html