博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1102 Constructing Roads
阅读量:4972 次
发布时间:2019-06-12

本文共 3115 字,大约阅读时间需要 10 分钟。

Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11576    Accepted Submission(s): 4353

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
 
Sample Output
179
 
Source
 
Recommend
Eddy
 
思路:简单最小生成树,使用Kruskal,不知道为什么使用Prim总是WA
 
代码:
#include 
#include
#include
#include
#include
#include
using namespace std;const int MAXN = 100010;int map[110][110];int a,b;int n,q;int USETree[110];int the_last_flag;int the_last_sum;struct Node{ int first,end; int value; bool select;}Edge[MAXN];int find(int x){ while(x != USETree[x]) { x = USETree[x]; } return x;}void Merge(int x,int y){ int p = find(x); int q = find(y); if(p < q) USETree[q] = p; else USETree[p] = q;}bool cmp(Node a,Node b){ if(a.value != b.value) return a.value < b.value; if(a.first != b.first) return a.first < b.first; return a.end < b.end;}void Kruskal(Node *Egde,int n,int m){ the_last_flag = 0; sort(Edge + 1,Edge + m + 1,cmp); for(int i = 1;i <= m;i ++) { if(the_last_flag == n - 1) break ; int x = find(Edge[i].first); int y = find(Edge[i].end); if(x != y) { Merge(x,y); the_last_flag ++; Edge[i].select = true; } }}int main(){ while(~scanf("%d",&n)) { memset(Edge,0,sizeof(Edge)); for(int i = 1;i <= n;i ++) USETree[i] = i; for(int i = 1;i <= n;i ++) for(int j = 1;j <= n;j ++) scanf("%d",&map[i][j]); scanf("%d",&q); while(q --) { scanf("%d%d",&a,&b); map[a][b] = 0; map[b][a] = 0; } int pos = 0; for(int i = 1;i <= n;i ++) for(int j = 1;j <= n;j ++) { if(j > i) { Edge[++ pos].value = map[i][j]; Edge[pos].first = i;Edge[pos].end = j; } } Kruskal(Edge,n,pos); the_last_sum = 0; for(int i = 1;i <= pos;i ++) { if(Edge[i].select == 1) the_last_sum += Edge[i].value; } printf("%d\n",the_last_sum); } return 0;}

 

转载于:https://www.cnblogs.com/GODLIKEING/p/3376687.html

你可能感兴趣的文章
unity3d 移动与旋转 2
查看>>
寻找二叉查找树中比指定值小的所有节点中最大的那个节点
查看>>
如何设置输入框达到只读效果
查看>>
RT3070 USB WIFI 在连接socket编程过程中问题总结
查看>>
MIS外汇平台荣获“2013年全球最佳STP外汇交易商”
查看>>
LeetCode 题解之Add Digits
查看>>
hdu1502 , Regular Words, dp,高精度加法
查看>>
20120227_CET6
查看>>
SpringBoot在idea中的热部署配置
查看>>
MyEclipse连接SQL Server 2008数据库的操作方法
查看>>
leetcode【67】-Bulb Switcher
查看>>
JS验证图片格式和大小并预览
查看>>
laravel5.2 移植到新服务器上除了“/”路由 ,其它路由对应的页面显示报404错误(Object not found!)———新装的LAMP没有加载Rewrite模块...
查看>>
编写高质量代码--改善python程序的建议(六)
查看>>
windows xp 中的administrator帐户不在用户登录内怎么解决?
查看>>
接口和抽象类有什么区别
查看>>
Codeforces Round #206 (Div. 2)
查看>>
**p
查看>>
优先队列详解
查看>>
VS2012 创建项目失败,,提示为找到约束。。。。
查看>>