hdoj 1102 Constructing Roads 【最小生成树】
来源:互联网 发布:围堰稳定性计算软件 编辑:程序博客网 时间:2024/03/28 18:32
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15020 Accepted Submission(s): 5732
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.
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
此题有坑,需要输入多组测试数据。附上两种代码:
Kruskal算法:
#include<stdio.h>#include<algorithm>#define max 10000+10using namespace std;int set[max];struct record{ int dis;//记录村庄间的距离 int start;//线路起点 int end;//终点 }num[max];bool cmp(record a,record b){ return a.dis<b.dis;}int find(int p){ int child=p; int t; while(p!=set[p]) p=set[p]; while(child!=p) { t=set[child]; set[child]=p; child=t; } return p;}void merge(int x,int y){ int fx=find(x); int fy=find(y); if(fx!=fy) set[fx]=fy;} int main(){ int village,d;//村庄数和相互间的距离 int road;//已有的道路数 int need;//需要建的道路长度 int i,j,x,y,t; while(scanf("%d",&village)!=EOF) { t=0; for(i=1;i<=village;i++) { set[i]=i; for(j=1;j<=village;j++) { scanf("%d",&d); if(j>i) { num[t].start=i; num[t].end=j; num[t].dis=d; t++; } } } sort(num,num+t,cmp);//排序 scanf("%d",&road); while(road--) { scanf("%d%d",&x,&y); merge(x,y);//合并已有道路 } need=0; for(i=0;i<t;i++) { if(find(num[i].start)!=find(num[i].end))//未连通 { merge(num[i].start,num[i].end); need+=num[i].dis; } } printf("%d\n",need); } return 0;}
prime算法:
#include<stdio.h>#include<string.h>#define INF 0x3f3f3f#define max 100+10int lowcost[max],visit[max],map[max][max];int city;int prime(){ int i,j; int next; int min,mincost=0; memset(visit,0,sizeof(visit)); for(i=1;i<=city;i++) { lowcost[i]=map[1][i]; } visit[1]=1; for(i=1;i<city;i++) { min=INF; for(j=1;j<=city;j++) { if(!visit[j]&&min>lowcost[j]) { min=lowcost[j]; next=j; } } mincost+=min; visit[next]=1; for(j=1;j<=city;j++) { if(!visit[j]&&lowcost[j]>map[next][j]) { lowcost[j]=map[next][j]; } } } return mincost;}int main(){ int i,j,road,x,y; int need; while(scanf("%d",&city)!=EOF) { memset(map,INF,sizeof(map)); for(i=1;i<=city;i++) { for(j=1;j<=city;j++) { scanf("%d",&map[i][j]); } } scanf("%d",&road); while(road--) { scanf("%d%d",&x,&y); map[x][y]=map[y][x]=0; } need=prime(); printf("%d\n",need); } return 0;}
1 0
- HDOJ 1102 Constructing Roads(最小生成树)
- hdoj 1102 Constructing Roads 【最小生成树】
- hdoj-1102-Constructing Roads【最小生成树】
- hdoj 1102 Constructing Roads 【最小生成树】
- HDOJ 1102 Constructing Roads(最小生成树)
- hdoj Constructing Roads(最小生成树)
- HDOJ题目1102Constructing Roads(最小生成树,prim)
- HDOJ 1102 Constructing Roads(最小生成树--prime)
- HDOJ 1102 Constructing Roads 最小生成树 kruskal && Prim
- HDOJ 1102 Constructing Roads (最小生成树)
- hdoj 1102 Constructing Roads 【最小生成树&&prim】
- HDOJ-1102 Constructing Roads(最小生成树)两种做法
- HDOJ 1102 Constructing Roads(最小生成树)
- hdu 1102 Constructing Roads 最小生成树
- Constructing Roads - HDU 1102 最小生成树
- hdu 1102 Constructing Roads 最小生成树
- Constructing Roads(HDU 1102 最小生成树)
- hdu 1102 Constructing Roads 最小生成树
- MongoDB用户认证和权限
- linux下的source命令
- Linux下移植OpenCV + Qt 到ARM(Tiny6410)总结
- servlet处理request和response中文乱码
- C#中Label设置为透明
- hdoj 1102 Constructing Roads 【最小生成树】
- OpenCV数据读写操作
- 1091.Acute Stroke
- apue学习第十八天——从哲学家就餐问题看死锁
- C语言‘指针’笔记
- OpenCV学习笔记 cv.Mat 与 .txt 文件数据的读写操作
- ubuntu的终端命令(收集)
- Mat - 基本图像容器
- JVM 自定义的类加载器的实现和使用