poj 2421 Constructing Roads
来源:互联网 发布:为知云笔记使用教程 编辑:程序博客网 时间:2024/06/13 20:19
Constructing Roads
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 25042 Accepted: 10898
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
Source
PKU Monthly,kicc
prim模板题,只是多了 给出了一些已经联通的边 的条件,只要将已经联通的边权值改为0 就可以了
#include<iostream>#include<cstring>using namespace std;int ma[111][111];char ma1[111][111];int n,m;void prim(){bool vis[111];memset(vis,0,sizeof(vis));int dis[111];for(int i=1;i<=n;i++)dis[i]=ma[1][i];vis[1]=1;long long int ans=0;int minn;int k;for(int i=1;i<n;i++){minn=99999999;for(int j=1;j<=n;j++){if(vis[j]==0&&dis[j]<minn){minn=dis[j]; k=j;}}if(minn==99999999)break;vis[k]=1;ans+=dis[k];for(int s=1;s<=n;s++){if(vis[s]==0&&dis[s]>ma[k][s])dis[s]=ma[k][s];}}cout<<ans<<endl;}int main(){ while(cin>>n){ int i,j; for(i=1;i<=n;i++) for(j=1;j<=n;j++) cin>>ma[i][j]; int q; cin>>q; int x,y; while(q--) { cin>>x>>y; ma[x][y]=ma[y][x]=0;}prim(); }return 0;}
阅读全文
0 0
- POJ 2421Constructing Roads
- poj 2421 Constructing Roads
- poj 2421 Constructing Roads
- POJ 2421 Constructing Roads
- POJ 2421 Constructing Roads
- POJ 2421 constructing roads
- POJ-2421-Constructing Roads
- POJ 2421 Constructing Roads
- POJ:2421 Constructing Roads
- poj 2421Constructing Roads
- POJ 2421 Constructing Roads
- POJ 2421 Constructing Roads
- POJ-2421 Constructing Roads
- POJ 2421 Constructing Roads
- POJ 2421 Constructing Roads
- poj 2421 Constructing Roads
- poj 2421 Constructing Roads
- POJ - 2421 Constructing Roads
- echarts x轴文字显示不全问题
- linux JDK 配置
- Python解析excel
- 2017多校第3场 HDU 6059 Trie树,贡献统计
- 使用push_back()函数
- poj 2421 Constructing Roads
- Linux 逻辑卷管理器 LVM
- 如何“背”程序
- POJ 4825 Xor Sum 字典树
- 2017.08.05【NOIP提高组】模拟赛B组
- English story 15
- poj 3632 Optimal Parking
- ubuntu 查看文件编码并进行批量编码修改
- Spring系列之Spring常用注解总结