无题 (最小生成树+prim)
2015-08-21 19:13
302 查看
问题
Many new buildings are under construction on the campus of the University of Waterloo. The university has hired bricklayers, electricians, plumbers, and a computer programmer. A computer programmer? Yes, you have been hired to ensure that each building is
connected to every other building (directly or indirectly) through the campus network of communication cables. We will treat each building as a point specified by an x-coordinate and a y-coordinate. Each communication cable connects exactly two buildings,
following a straight line between the buildings. Information travels along a cable in both directions. Cables can freely cross each other, but they are only connected together at their endpoints (at buildings). You have been given a campus map which shows
the locations of all buildings and existing communication cables. You must not alter the existing cables. Determine where to install new communication cables so that all buildings are connected. Of course, the university wants you to minimize the amount of
new cable that you use.
Input
The input file describes several test cases. The description of each test case is given below: The first line of each test case contains the number of buildings N (1 ≤ N ≤ 750). The buildings are labeled from 1 to N. The next N lines give the x and y coordinates
of the buildings. These coordinates are integers with absolute values at most 10000. No two buildings occupy the same point. After that there is a line containing the number of existing cables M (0 ≤ M ≤ 1000) followed by M lines describing the existing cables.
Each cable is represented by two integers: the building numbers which are directly connected by the cable. There is at most one cable directly connecting each pair of buildings.
Output
For each set of input, output in a single line the total length of the new cables that you plan to use rounded to two decimal places.
Sample Input
4
103 104
104 100
104 103
100 100
1
4 2
Sample Output
4.41
Many new buildings are under construction on the campus of the University of Waterloo. The university has hired bricklayers, electricians, plumbers, and a computer programmer. A computer programmer? Yes, you have been hired to ensure that each building is
connected to every other building (directly or indirectly) through the campus network of communication cables. We will treat each building as a point specified by an x-coordinate and a y-coordinate. Each communication cable connects exactly two buildings,
following a straight line between the buildings. Information travels along a cable in both directions. Cables can freely cross each other, but they are only connected together at their endpoints (at buildings). You have been given a campus map which shows
the locations of all buildings and existing communication cables. You must not alter the existing cables. Determine where to install new communication cables so that all buildings are connected. Of course, the university wants you to minimize the amount of
new cable that you use.
Input
The input file describes several test cases. The description of each test case is given below: The first line of each test case contains the number of buildings N (1 ≤ N ≤ 750). The buildings are labeled from 1 to N. The next N lines give the x and y coordinates
of the buildings. These coordinates are integers with absolute values at most 10000. No two buildings occupy the same point. After that there is a line containing the number of existing cables M (0 ≤ M ≤ 1000) followed by M lines describing the existing cables.
Each cable is represented by two integers: the building numbers which are directly connected by the cable. There is at most one cable directly connecting each pair of buildings.
Output
For each set of input, output in a single line the total length of the new cables that you plan to use rounded to two decimal places.
Sample Input
4
103 104
104 100
104 103
100 100
1
4 2
Sample Output
4.41
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #define INF 0x3f3f3f3f using namespace std; double map[1010][1010],dis[1010]; int vis[1010]; int n,m; void prim(int x) { int i,j,k; double min,sum=0; memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++) { dis[i]=map[1][i]; } dis[x]=0; vis[x]=1; for(j=1;j<n;j++) { k=1;min=INF; for(i=1;i<=n;i++) { if(!vis[i]&&dis[i]<min) { min=dis[i]; k=i; } } vis[k]=1; sum+=min; for(i=1;i<=n;i++) { if(!vis[i]&&dis[i]>map[k][i]) dis[i]=map[k][i]; } } printf("%.2lf\n",sum); } double x[1010],y[1010]; int main(){ while(scanf("%d",&n)!=EOF) { int i,j,m; memset(map,INF,sizeof(map)); for(i=1;i<=n;i++) { scanf("%lf%lf",&x[i],&y[i]); } for(i=1;i<n;i++) for(j=i+1;j<=n;j++) { map[i][j]=map[j][i]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); } scanf("%d",&m); while(m--) { scanf("%d%d",&i,&j); map[i][j]=map[j][i]=0; } prim(1); } return 0; }
相关文章推荐
- SQL 中利用游标(cursor)循环
- swift 自定义TextField 的边框颜色和 左右 视图
- C和C++混合编译
- 静态变量能否被外部访问?
- day10: 内存管理高级:属性的内部实现原理、dealloc内释放实例变量、便利构造器方法的实现原理、collection的内存管理
- 大龄屌丝自学笔记--Java零基础到菜鸟--007
- 读Effective Objective C总结(二)
- 八大排序算法
- java第一课 helloword
- IOS开发之动态获取模型的属性值
- 自己看的
- HDU 1850 Being a Good Boy in Spring Festival
- 上传文件 | 下载文件
- 基于网页可信特征的信息可信度评估方法(IEEE2011)
- 队列
- Access restriction: The method createJPEGEncoder(OutputStream) from the type JPEGCodec is not access
- 【LeetCode】(263)Ugly Number(Easy)
- BS_OWNERDRAW风格的作用和例子,值得研究~
- 洛谷1177 快速排序 解题报告
- FIREDAC FDConnection 连接池