POJ 2485 Highways(Prim 和 Kruskal)
来源:互联网 发布:防范网络诈骗国家措施 编辑:程序博客网 时间:2024/04/20 13:21
Highways
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 22934 Accepted: 10561
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.
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.
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
130 990 692990 0 179692 179 0
Sample Output
692
题目很简单,就是求最小生成树中的一条最大边
Prim算法
//C++ 766MS#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int N = 501;const int MAX = 999999;int a[N][N],n;int b[N],v[N];void prim(){ memset(b,0,sizeof(b)); //memset(v,MAX,sizeof(v)); for(int i=0;i<n;i++) v[i]=a[0][i]; b[0]=1; int max=-1,k; int min=MAX; for(int i=1;i<n;i++) { min=MAX; for(int j=0;j<n;j++) { if(min>v[j] && !b[j]) { k=j; min=v[j]; } } if(max<min) max=min; b[k]=1; for(int j=0;j<n;j++) { if(!b[j] && v[j]>a[k][j]) { v[j]=a[k][j]; } } } cout<<max<<endl;}int main(){ int T; cin>>T; while(T--) { cin>>n; for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>a[i][j]; prim(); } return 0;}
Kruskal算法
//C++ 969MS#include <iostream>#include <cstdio>#include <cstring>#include <stdlib.h>using namespace std;const int N = 501;const int MAX = 999999;int n,s;int b[N];struct node{ int x; int ll,rr;}q[300000];int cmp(void const *a, void const *b){ return (*(struct node *)a).x - (*(struct node *)b).x ;}int findx(int r){ int t=r; while(t!=b[t]) { t=b[t]; } /* int v=r; while(t!=v) { int j=b[v]; b[v]=t; v=j; }*/ //优化完 875MS return t;}int main(){ int T; cin>>T; while(T--) { s=0; cin>>n; for(int i=0;i<n;i++) for(int j=0;j<n;j++) { cin>>q[s].x; q[s].ll=i; q[s++].rr=j; } for(int i=0;i<n;i++) b[i]=i; qsort(q,s,sizeof(q[0]),cmp); int max=-1; for(int i=0;i<s;i++) { int dx1=findx(q[i].ll); int dx2=findx(q[i].rr); if(dx1!=dx2) { if(max<q[i].x) max=q[i].x; b[dx1]=dx2; } } cout<<max<<endl; } return 0;}
0 0
- POJ 2485 Highways(Prim 和 Kruskal)
- POJ 2485 Highways (kruskal+prim)
- poj 1751 Highways (prim和kruskal)
- POJ - 2485 Highways(prim算法,Kruskal算法)
- POJ 2485 Highways 最小生成树 Kruskal && Prim
- POJ 2485-Highways(最小生成树裸题-prim/kruskal)
- POJ 2485 Highways Kruskal
- poj 2485 Highways 【kruskal】
- Highways POJ 2485【Prim】
- poj 2485 Highways ( prim )
- poj 2485 Highways(prim)
- Highways --(Kruskal和prim)C
- POJ 2485 highways解题报告(最小生成树)(应用prim和Kruskal两种方法)(c++)
- poj 2485 Highways(Kruskal!)
- poj 2485 Highways(Kruskal)
- POJ-2485-Highways(kruskal)
- POJ 2485 Highways (MST.Prim)
- POJ 1751 Highways 最小生成树 Kruskal && Prim
- UIScreen
- 响应式布局入门
- R12.2克隆patch fs报错——Rapid Clone completed successfully , but the AutoConfig run recorded some errors
- Java面向对象基础三
- 通过 Azure Media Encoder 降低编码成本
- POJ 2485 Highways(Prim 和 Kruskal)
- java学习2、软件包和访问权限
- 第9周项目6-穷举法解决组合问题a百钱百鸡问题
- 前进
- Linux SVN使用
- 数据结构与算法学习资源
- uva 10404(博弈)
- IBM(上海)面试
- java中import,package的用法