POJ 2421 Constructing Roads
2015-08-31 20:22
357 查看
题目链接:http://poj.org/problem?id=2421
Constructing Roads
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
Sample Output
题意:
n个村庄,已知每个村庄彼此的距离,Q个询问,输入a,b,表示a到b的马路已经修好,求连到所有村庄的马路最小长度是多少
思路一:
prim最小生成树求,唯一的地方就是a到b的马路修完,即处理成距离为0;
[b]思路二:
Kruskal最小生成树求,点权边注意一下即可;
Constructing Roads
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21105 | Accepted: 8916 |
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
题意:
n个村庄,已知每个村庄彼此的距离,Q个询问,输入a,b,表示a到b的马路已经修好,求连到所有村庄的马路最小长度是多少
思路一:
prim最小生成树求,唯一的地方就是a到b的马路修完,即处理成距离为0;
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> using namespace std; #define N 105 #define INF 0x3f3f3f3f int mat ,vis ,low ; int n,m; int prim() { int i,j,ans=0,pos; memset(vis,0,sizeof(vis)); pos=1; vis[1]=1; for(i=1; i<=n; i++) low[i]=mat[pos][i]; for(i=1; i<n; i++) { int MIN=INF; for(j=1; j<=n; j++) if(!vis[j] && MIN>low[j]) { pos=j,MIN=low[j]; } if(MIN==INF)break; ans+=MIN; vis[pos]=1; for(j=1; j<=n; j++) { if(!vis[j] && low[j]>mat[pos][j]) { low[j]=mat[pos][j]; } } } return ans; } int main() { int ans,i,j,a,b; while(~scanf("%d",&n)) { memset(mat,INF,sizeof(mat)); for(i=1; i<=n; i++) for(j=1; j<=n; j++) { int u; scanf("%d",&u); mat[i][j]=(i==j?0:u); } scanf("%d",&m); for(i=1; i<=m; i++) { scanf("%d%d",&a,&b); mat[a]=mat[b][a]=0; } printf("%d\n",prim()); } return 0; }
[b]思路二:
Kruskal最小生成树求,点权边注意一下即可;
#include <cstdio> #include <cmath> #include <cstring> #include <algorithm> using namespace std; #define N 105 #define INF 0x3f3f3f3f int n,m,k; int pre ,mat ; struct pp { int u,v,w; } edge[N*N]; int cmp(pp a,pp b) { return a.w<b.w; } int find(int x) { return pre[x]==x?x:find(pre[x]); } int Kruskal() { int i,j,ans=0; for(i=1; i<=n; i++) pre[i]=i; sort(edge+1,edge+k+1,cmp); for(i=1; i<=k; i++) { int fx=find(edge[i].u); int fy=find(edge[i].v); if(fx!=fy) { pre[fx]=fy; ans+=edge[i].w; } } return ans; } int main() { int ans,i,j,a,b; while(~scanf("%d",&n)) { memset(mat,INF,sizeof(mat)); for(i=1; i<=n; i++) for(j=1; j<=n; j++) { int u; scanf("%d",&u); mat[i][j]=(i==j?0:u); } scanf("%d",&m); for(i=1; i<=m; i++) { scanf("%d%d",&a,&b); mat[a][b]=mat[b][a]=0; } k=1; for(i=1; i<n; i++) for(j=i+1; j<=n; j++) { edge[k].u=i; edge[k].v=j; edge[k].w=mat[i][j]; k++; } printf("%d\n",Kruskal()); } return 0; }
相关文章推荐
- 【转载】纯CSS3实现飘逸洒脱带有飞行效果的三级下拉菜单
- ArcGIS for Android 在Eclipse上的安装配置 (下:安装配置篇)
- winform导入excel到access数据库中
- TCP/IP三次握手,四次分手
- android:configChanges属性
- [JavaWeb基础] 015.Struts2 表单验证框架
- 如何使用UIAutomation进行iOS 自动化测试(Part II)
- CSDN第一篇
- 【数据库 Oracle】--去除重复字段
- 【python】__sub__方法:切片
- scala实现解释器模式
- MySQL存储过程之参数和复合语句
- 写一篇文章纪念一下儿子入学
- 网络编程
- Cookies
- maven学习笔记(二)
- Codeforces Round #250 (Div. 2) 437A The Child and Homework(模拟)
- HLG2179 组合(dfs水水更健康)
- 通过一个date获取年龄
- ios MVC笔记