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.
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
题意:给你一个数字N,代表村庄数,下面是一个n*n的矩阵,对应的i,j坐标是i到j修建道路所用长度。再输入的Q的值是有Q条路已经修建,即i到j的路已经给你修好了.
解法:你只需要把已经修筑好的Q条路变为“0”长度就好了,但是我还是wa了一次,超时一次才解决,这道题和poj的POJ 2485很像哦,那道题他给你提示用scanf输入,
这道题却没给你提示(话说我这个人很懒得,我直接用取消同步做的,他没卡我数据我过了)。这道题我wa是因为我寻找对应的路线的权值方法不对导致的,
我最后选择直接继续往结构体里存(反正结构体开的够大,用kruskal算法有排序,我怕啥,emmm)。但是杭电他居然卡我数据时间,我取消同步都不让我过,
最后一气之下我用scanf做就好了(我认怂,zz),直接轻易过。
1 #include<stdio.h> 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 1e3; 6 int p[maxn]; 7 int n,m; 8 struct face//存储数据 9 { 10 int u,v,w; 11 } edge[maxn*maxn]; 12 bool cmp(face a,face b)//权值排序 13 { 14 return a.w<b.w; 15 } 16 void init()//并查集1 17 { 18 for(int i=1; i<=n; i++) 19 p[i]=i; 20 } 21 int find(int x)//并查集2 22 { 23 return x==p[x]?x:p[x] = find(p[x]); 24 } 25 long long kruskal()//核心算法,记得用longlong,他的数据挺大的。 26 { 27 long long ans=0;//防止ans存不下 28 init(); 29 sort(edge,edge+m,cmp);//此处就是将给的权值先排序 30 for(int i=0; i<m; i++) 31 { 32 int x = find(edge[i].u); 33 int y = find(edge[i].v); 34 if(x!=y) 35 { 36 ans+=edge[i].w; 37 p[x] = y; 38 } 39 } 40 return ans; 41 } 42 int main() 43 { 44 // std::ios::sync_with_stdio(false); 45 while(scanf("%d",&n)!=EOF)//但是注意输入,HDU - 1233 和HDU - 1863给的n和m不一样(切记注意) 46 { 47 m=0; 48 for(int i=1; i<=n; i++) 49 { 50 for(int j=1; j<=n; j++) 51 { 52 int a; 53 scanf("%d",&a); 54 edge[m].u=i; 55 edge[m].v=j; 56 edge[m].w=a; 57 m++; 58 } 59 } 60 int t; 61 scanf("%d",&t); 62 for(int i=1;i<=t;i++) 63 { 64 int a,b; 65 scanf("%d%d",&a,&b); 66 edge[m].u=a; 67 edge[m].v=b; 68 edge[m].w=0; 69 m++; 70 } 71 printf("%lld ",kruskal());//longlong输出。 72 } 73 return 0; 74 }