poj
来源:互联网 发布:网络身份认证技术 编辑:程序博客网 时间:2024/06/08 15:41
Constructing Roads
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 25409 Accepted: 11102
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
#include<iostream>#include<cstdio>int main(){ int n,q,a[105][105],sum=0; int flag[105]={0}; scanf("%d",&n); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ scanf("%d",&a[i][j]); } } scanf("%d",&q); int b,c; for(int i=1;i<=q;i++){ scanf("%d%d",&b,&c); a[b][c]=0; a[c][b]=0; } flag[1]=1;//第一个结点被覆盖掉 for(int k=1;k<n;k++){//循环n-1次找到n个结点就可 int min1=-1,min_i; for(int i=2;i<=n;i++){//选取下一个最小权值的结点 if(flag[i]==0&&(min1==-1||a[1][i]<min1)){ min1=a[1][i]; min_i=i;//记录被选的结点号 } } flag[min_i]=1; for(int i=2;i<=n;i++){//更新未覆盖结点距离 if(flag[i]==0&&a[1][i]>a[min_i][i]){ a[1][i]=a[min_i][i]; } } sum+=a[1][min_i];//加上权值 } printf("%d\n",sum);}
这道题多了一个已建立的两个村庄之间的路(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.)已建立的村庄就不用我们再建了,所以就赋值该两个村庄的距离为0,不算到mininum里。
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- ehcache缓存技术讲解
- iOS开发—使用GCD实现多线程(单次或重复执行任务)
- Bitmap转为单色位图,打印电子签名
- DevOps和云计算在一起至关重要
- Druid--基本配置及内置监控使用
- poj
- ros学习笔记(2):编写publisher
- 洛谷P1802 5倍经验日
- 数据结构实验之二叉树五:层序遍历
- CoordinatorLayout 的属性
- SLAM介绍
- 应用层笔记
- wait ,notfiy 配合synchronized关键字使用,wait立即释放锁,而notify并不立即释放
- 用c语言编写求n的k次方函数,用迭代和递归两种方法