其他分享
首页 > 其他分享> > Constructing Roads 最小生成树(prime()),注意边的处理方式

Constructing Roads 最小生成树(prime()),注意边的处理方式

作者:互联网

Problem Description 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.  

 

Input The 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.  

 

Output You 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 *************************************************************************************************************************** 注意边的处理方式 ***************************************************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<cstdio>
 6 using namespace std;
 7 int map[1001][1001],dis[1001],vis[1001];
 8 int n,m,i,j,k;
 9 //int ans;
10 int prime(int start)//求最小生成树
11 {
12     int ans=0;
13     int it,jt,kt,temp1,minn;
14     for(it=1;it<=n;it++)
15     {
16         dis[it]=map[start][it];
17         vis[it]=0;
18     }
19     //for(it=1;it<=n;it++)
20     //cout<<"dis:: "<<dis[it]<<endl;
21     vis[start]=1;
22     for(it=1;it<=n;it++)
23     {
24         minn=99999;
25         for(jt=1;jt<=n;jt++)
26         {
27             if(minn>dis[jt]&&!vis[jt])
28             {
29                 minn=dis[jt];
30                 temp1=jt;
31             }
32         }
33         if(minn==99999)
34             break;
35         vis[temp1]=1;
36         ans+=minn;
37         //cout<<"ans++:: "<<ans<<"minn:: "<<minn<<endl;
38         for(jt=1;jt<=n;jt++)
39         {
40             if(!vis[jt]&&dis[jt]>map[temp1][jt])
41                dis[jt]=map[temp1][jt];
42         }
43     }
44     return ans;
45 }
46 int main()
47 {
48     int a,b;
49     while(scanf("%d",&n)!=EOF)
50     {
51         for(i=1;i<=n;i++)
52          for(j=1;j<=n;j++)
53             scanf("%d",&map[i][j]);
54          scanf("%d",&m);
55          for(i=1;i<=m;i++)
56          {
57             scanf("%d%d",&a,&b);
58             map[a][b]=map[b][a]=0;//注意此处的赋值方式
59          }
60          printf("%d\n",prime(1));
61     }
62 }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3400079.html

标签:prime,Constructing,int,there,roads,jt,temp1,Roads,include
来源: https://blog.csdn.net/weixin_34408624/article/details/94300157