HDOJ 1102 Constructing Roads(最小生成树)
来源:互联网 发布:近视训练软件 编辑:程序博客网 时间:2024/04/27 17:18
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17206 Accepted Submission(s): 6532
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.
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.
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
30 990 692990 0 179692 179 011 2
Sample Output
179
坑啊,这特么的输出样例真不懂,QAQ。。。英语瞎子
题意:有n个城市,给出一个n*n矩阵,矩阵中第i行第j个数表示城市i与j间的距离。再给出Q,有Q行数据表示x城市与y城市间已经有道路,求出,畅通所有城市需要建设的道路的最小长度。
Kruskal算法,将矩阵化为结构体储存好城市间信息即可,代码如下:
#include<cstdio>#include<algorithm>using namespace std;int set[110];struct node{int s,e,w;}row[10010];int cmp(node a,node b){return a.w<b.w;}int find(int x){int r=x,t;while(r!=set[r]) r=set[r];while(x!=r){t=set[x];set[x]=r; x=t;}return r;} int merge(int a,int b){int fa,fb;fa=find(a);fb=find(b);if(fa!=fb) { set[fa]=fb; return 1; } return 0;}int main(){int n,i,j,k,way,Q,x,y,sum;while(scanf("%d",&n)!=EOF){for(i=1;i<=n;i++) set[i]=i;k=0;for(i=1;i<=n;i++){for(j=1;j<=n;j++){scanf("%d",&way);if(j>i){row[k].s=i; row[k].e=j; row[k].w=way; k++;}}}sort(row,row+k,cmp);scanf("%d",&Q);for(i=0;i<Q;i++){scanf("%d%d",&x,&y);merge(x,y);//注意合并根节点啊,wa了好几次 }sum=0;for(i=0;i<k;i++){if(merge(row[i].s,row[i].e)) sum+=row[i].w;}printf("%d\n",sum);}return 0;}
0 0
- HDOJ 1102 Constructing Roads(最小生成树)
- hdoj 1102 Constructing Roads 【最小生成树】
- hdoj-1102-Constructing Roads【最小生成树】
- hdoj 1102 Constructing Roads 【最小生成树】
- HDOJ 1102 Constructing Roads(最小生成树)
- hdoj Constructing Roads(最小生成树)
- HDOJ题目1102Constructing Roads(最小生成树,prim)
- HDOJ 1102 Constructing Roads(最小生成树--prime)
- HDOJ 1102 Constructing Roads 最小生成树 kruskal && Prim
- HDOJ 1102 Constructing Roads (最小生成树)
- hdoj 1102 Constructing Roads 【最小生成树&&prim】
- HDOJ-1102 Constructing Roads(最小生成树)两种做法
- HDOJ 1102 Constructing Roads(最小生成树)
- hdu 1102 Constructing Roads 最小生成树
- Constructing Roads - HDU 1102 最小生成树
- hdu 1102 Constructing Roads 最小生成树
- Constructing Roads(HDU 1102 最小生成树)
- hdu 1102 Constructing Roads 最小生成树
- [NOIP 2013]货车运输
- leetcode: Binary Tree Preorder Traversal
- hdu 1068 Girls and Boys 二分图的最大匹配
- 【bzoj4004】 JLOI2015装备购买 拟阵+线性基
- LintCode-分糖果
- HDOJ 1102 Constructing Roads(最小生成树)
- 前言:关于nagios监控
- sysctl.conf学习和调优
- 线程和进程
- struts2-ValueStack详解
- Python使用struct处理二进制(pack和unpack用法)
- 用链表实现电子词典
- 笔试题:第一个只出现一次的字符
- 2015 Multi-University Training Contest 7 -- 1007 Gray code