Highways(POJ_2485)
2015-07-31 09:17
357 查看
Description
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They’re planning to build some 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. Each highway connects exactly two towns. All highways follow straight lines. 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 length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.
Input
The first line of input is an integer T, which tells how many test cases followed.The first line of each case is an integer N (3 <= N <= 500), 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, 65536]) between village i and village j. There is an empty line after each test case.
Output
For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.Sample Input
13
0 990 692
990 0 179
692 179 0
Sample Output
692Hint
Huge input,scanf is recommended.Source
POJ Contest,Author:Mathematica@ZSU代码
做的第一道最小生成树,具有里程碑式的意义,哈哈~用了并查集,感觉这种做法应该是kruscal吧。。。#include <iostream> #include <cstdio> #include <algorithm> using namespace std; struct node { int u,v,w; bool operator <(const node x)const { return w<x.w; } } a[30000]; int bin[1100]; int ans[30000],cnt; int find(int x) { return x==bin[x]?x:find(bin[x]); } void merge(int a,int b,int w) { int fa=find(a),fb=find(b); if(fa!=fb) { bin[fa]=fb; ans[cnt++]=w; } } int main() { int t,n; cin>>t; while(t--) { cin>>n; for(int i=0; i<=n; i++) bin[i]=i; int k=0,c; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { scanf("%d",&c); if(i<j) { a[k].u=i; a[k].v=j; a[k++].w=c; } } } sort(a,a+k); cnt=0; for(int i=0; i<k; i++) { merge(a[i].u,a[i].v,a[i].w); } sort(ans,ans+cnt); cout<<ans[cnt-1]<<endl; } return 0; }
这种题一般好像都用prim做,附上prim算法的代码
#include <cstdio> #include <cstring> #define INF 0x3f3f3f3f #define N 1001 using namespace std; int map ; bool vis ; int dis ; int n,sum,max; void prim() { sum=0; max=-9999; int mm; int i,j; memset(vis,0,sizeof(vis)); for(i=0; i<n; i++) dis[i]=map[0][i]; vis[0]=true; int pos; for(i=0; i<n-1; i++) { mm=INF; for(j=0; j<n; j++) { if(vis[j]==false&&dis[j]<mm) { mm=dis[j]; pos=j; } } sum+=mm; if(mm>max) max=mm; vis[pos]=true; for(j=0; j<n; j++) { if(vis[j]==false&&dis[j]>map[j][pos]) dis[j]=map[j][pos]; } } printf("%d\n",max); } int main() { int i,j,t; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=0; i<n; i++) for(j=0; j<n; j++) scanf("%d",&map[i][j]); prim(); } return 0; }
相关文章推荐
- 使用 CSS 媒体查询创建响应式网站
- Object-C Basis
- scikit-learn:External Resources, Videos and Talks
- Windows Server 2008 R2开启新端口远程桌面
- C语言常见函数(备忘录)
- 递推DP HDOJ 5328 Problem Killer
- MySQL提示:The server quit without updating PID file(…)失败的一些解决方法
- POJ 1236 Network of Schools(强联通分量)
- JDBC连接与增删查改的封装
- MyBatis开发实战
- 异常处理
- Android系统中解码器
- IOS 真机测试 Xcode出现"The identity used to sign the executable is no longer valid"
- nyoj 79拦截导弹
- DT时代,手机厂商更在意大数据,京东能满足么?
- Copy On Write(写时复制)
- C# 装箱与拆箱
- Python 实用命令
- [MySQL FAQ]系列 — 怎么计算打开文件数
- UI设计:如何设计移动应用的导航