Constructing Roads HDU杭电1102【Kruscal || Prim】
2015-08-11 16:16
253 查看
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
Sample Output
//输入方式:第一行有N个村庄,然后是N行,每行是一个村庄与其他村庄的距离,然后是一个T,表示已经修好的路的条数,求最少还要修多长的路
//Kruscal
//prim
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个村庄,然后是N行,每行是一个村庄与其他村庄的距离,然后是一个T,表示已经修好的路的条数,求最少还要修多长的路
//Kruscal
#include<cstdio> #include<algorithm> using namespace std; struct node { int u,v,w; int flag; }; bool cmp(node a,node b) { if(a.flag==b.flag) return a.w<b.w; return a.flag>b.flag; } node arr[10000]; int per[110]; int n; void init() { for(int i=1;i<=n;++i) { per[i]=i; } } int find(int x) { if(x==per[x]) return x; return per[x]=find(per[x]); } bool join(int x,int y,int f) { int fx=find(x); int fy=find(y); if(fx!=fy) { per[fx]=fy; if(f==1) return 0; return 1; } return 0; } int main() { int a,b,temp; int T; int i,j; while(~scanf("%d",&n)) { init(); int k=0; for(i=1;i<=n;++i) { for(j=1;j<=n;++j) { scanf("%d",&temp); if(temp) { arr[k].u=i; arr[k].v=j; arr[k].flag=0; arr[k++].w=temp; } } } scanf("%d",&T); while(T--) { scanf("%d%d",&a,&b); for(i=0;i<k;++i) { if(arr[i].u==a&&arr[i].v==b) { arr[i].flag=1; break; } } } sort(arr,arr+k,cmp); int sum=0; for(i=0;i<k;++i) { if(join(arr[i].u,arr[i].v,arr[i].flag)) { sum+=arr[i].w; } } printf("%d\n",sum); } return 0; }
//prim
#include<stdio.h> #include<string.h> #define INF 0x3f3f3f3f #define N 110 int n; int i,j; int map ; int a,b; int low ; bool vis ; void input() { for(i=1;i<=n;++i) { for(j=1;j<=n;++j) { scanf("%d",map[i]+j); } } int m; scanf("%d",&m); while(m--) { scanf("%d%d",&a,&b); //vis[a]=vis[b]=1;//把已经连接了的两个点加入最小生成树集合 map[a][b]=map[b][a]=0; //把ab两点的权值变为0 } } void prim() { int pos=1; int sum=0; for(i=1;i<=n;++i)//第一次给low赋值 { low[i]=map[pos][i]; } vis[pos]=1; //加入最小生成树集合 for(i=1;i<n;++i)//再找n-1个点 { int min=INF; for(j=1;j<=n;++j) { if(!vis[j]&&min>low[j]) { min=low[j]; pos=j;//把找到的点记录下 } } sum+=min; vis[pos]=1; for(j=1;j<=n;++j) { if(!vis[j]&&low[j]>map[pos][j]) { low[j]=map[pos][j]; } } } printf("%d\n",sum); } int main() { while(~scanf("%d",&n)) { memset(vis,0,sizeof(vis)); input(); prim(); } return 0; }
相关文章推荐
- libevent 安装与使用说明
- Maven直接部署Web应用Tomcat
- C或C++输入缓冲区问题
- The shortest problem(hdu,多校
- DFA最小化 -- Hopcroft算法 Python实现
- onNewIntent调用时机
- Word2013商业文档排版技巧-吴婷-专题视频课程
- HDOJ 1879 继续畅通工程 最小生成树 kruskal && prim
- 商业数据分析与处理-吴婷-专题视频课程
- Cashe的使用
- 详解Objective-C runtime
- HDOJ 1879 继续畅通工程(最小生成树)
- 23-IO流-42-IO流(Properties集合的-基本功能)
- Murano.conf的配置
- Oracle的监听(listener)到底是什么
- 使用重写的$.ajax()实现后台处理进度显示(gif)
- C语言与汇编混合编程
- Codeforces Round #315 (Div. 2)569C Primes or Palindromes?(预处理)
- 富文本可上传图片版
- [转载]java int与integer的区别