您的位置:首页 > 其它

poj 2485 Highways(prime算法模板)

2016-08-04 09:16 253 查看
HighwaysTime Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %lluSubmit StatusDescriptionThe 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 planningto 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 townthat 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.InputThe 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]) betweenvillage i and village j. There is an empty line after each test case.OutputFor 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
1

3
0 990 692
990 0 179
692 179 0
Sample Output
692
模板,,,,
代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define inf 0x3f3f3fint map[510][510];int vis[510];int dis[510];int ans;int maxn;int prime(int n){memset(vis,0,sizeof(vis));memset(dis,inf,sizeof(vis));dis[1]=0,maxn=0;for(int i=1;i<=n;i++){int k=-1,tp=inf;for(int j=1;j<=n;j++){if(!vis[j]&&dis[j]<tp){tp=dis[j];k=j;}}maxn=max(maxn,tp);vis[k]=1,ans+=tp;for(int j=1;j<=n;j++){if(!vis[j]&&map[k][j]<dis[j]){dis[j]=map[k][j];}}}return ans;}int main(){int t,n;scanf("%d",&t);while(t--){memset(map,inf,sizeof(map));scanf("%d",&n);for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){int w;scanf("%d",&w);map[i][j]=w;}}prime(n);printf("%d\n",maxn);}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: