ZOJ 2048 :Highways 【Prim】
2015-08-13 17:52
369 查看
Highways
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)Total Submission(s) : 11 Accepted Submission(s) : 7
Special Judge
[align=left]Problem Description[/align]
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some
of the most important towns. However, there are still some towns that you can't reach via a highway. It is necessary to build more highways so that it will be possible to drive between any pair of towns without leaving the highway system.
Flatopian towns are numbered from 1 to N and town i has a position given by the Cartesian coordinates (xi, yi). Each highway connects exaclty two towns. All highways (both the original ones and the ones that are to be built) follow straight lines, and thus
their length is equal to Cartesian distance between towns. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.
The Flatopian government wants to minimize the cost of building new highways. However, they want to guarantee that every town is highway-reachable from every other town. Since Flatopia is so flat, the cost of a highway is always proportional to its length.
Thus, the least expensive highway system will be the one that minimizes the total highways length.
[align=left]Input[/align]
The input consists of two parts. The first part describes all towns in the country, and the second part describes all of the highways that have already been built.
The first line of the input file contains a single integer N (1 <= N <= 750), representing the number of towns. The next N lines each contain two integers, xi and yi separated by a space. These values give the coordinates of ith
town (for i from 1 to N). Coordinates will have an absolute value no greater than 10000. Every town has a unique location.
The next line contains a single integer M (0 <= M <= 1000), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These two integers give a pair of town numbers which are already connected by a
highway. Each pair of towns is connected by at most one highway.
[align=left]Output[/align]
Write to the output a single line for each new highway that should be built in order to connect all towns with minimal possible total length of new highways. Each highway should be presented by printing town numbers that this highway
connects, separated by a space.
If no new highways need to be built (all towns are already connected), then the output file should be created but it should be empty.
[align=left]Sample Input[/align]
9
1 5
0 0
3 2
4 5
5 1
0 4
5 2
1 2
5 3
3
1 3
9 7
1 2
[align=left]Sample Output[/align]
1 6
3 7
4 9
5 7
8 3
本来我以为我过不了的。。。毕竟输出的顺序不一样,,,但没想到,,,HDU竟然让我过了~~哈哈~~~
AC-code:
#include<cstdio> #include<cstring> #include<cmath> #define max 0x3f3f3f3f int n,s[800][800]; void prim() { int near[800],dis[800],i,j; for(i=2;i<=n;i++) { dis[i]=s[1][i]; near[i]=1; } near[1]=0; dis[1]=0; for(j=2;j<=n;j++) { int k=0; int min=max; for(i=2;i<=n;i++) if(near[i]!=0&&dis[i]<min) { k=i; min=dis[i]; } if(k!=-1) { if(dis[k]!=0) printf("%d %d\n",k,near[k]); near[k]=0; for(i=2;i<=n;i++) if(near[i]!=0&&dis[i]>s[k][i]) { dis[i]=s[k][i]; near[i]=k; } } } } int main() { int i,j,m,a,b; int x[800],y[800]; scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]); for(i=1;i<=n;i++) for(j=1;j<=n;j++) s[i][j]=s[i][j]=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]); scanf("%d",&m); while(m--) { scanf("%d%d",&a,&b); s[a][b]=s[b][a]=0; } prim(); return 0; }
相关文章推荐
- SQL Server 内置函数、临时对象、流程控制
- Linux系统常用命令大全
- Android 表情功能的完整处理方案
- linux drm 架构
- OSG教程汇总
- Android的shape使用
- acid-事务的原子性、一致性、隔离性、持久性
- zoj2048Highways
- View覆盖status bar
- web项目中各种路径的获取
- 斯坦福ML公开课笔记13A——混合高斯模型、混合贝叶斯模型
- 【转载】利用MAVEN打包时,如何包含更多的资源文件
- SQL Server 常用高级语法笔记
- VS中的路径宏 vc++中OutDir、ProjectDir、SolutionDir各种路径
- java中数据类型转换
- web前端-移动端小技巧
- Android动画之仿美团加载数据等待时,小人奔跑进度动画对话框(附顺丰快递员奔跑效果)
- 【高德API】如何利用MapKit开发全英文检索的iOS地图
- FFmpeg音频编解码处理
- java 使用cookie记录用户上一次访问的时间 记住 用户的 登录名