【prim】poj 2485

来源:互联网 发布:淘宝旺铺有什么用 编辑:程序博客网 时间:2024/06/06 15:49
Highways
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 20214 Accepted: 9377

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

130 990 692990 0 179692 179 0

Sample Output

692

Hint

Huge input,scanf is recommended.

第一次用prim解题,于是乎搜了一道prim水题
题目大意:
政府在n个城市间建造公路,每两个城市都有一个距离,求能连接所有城市的最短消耗建造资源的路线里,最长的两个城市间的距离是多少。
方法就是用最小生成树 prim   列出一个数组储存起始点到各点的距离,搜出距离最短的城市,标记,然后更新数组,更新方法是遍历数组取min(原值,新城市到改城市的距离)。以此类推直到经过所有城市。
#include <cstdio>#include <iostream>#include <cmath>#include <string>#include <cstring>#include <cstdlib>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <queue>#define ll long longusing namespace std;const int INF = 0x3f3f3f3f;int t,n;const int maxn=500+2;int dis[maxn][maxn]; //储存距离bool vis[maxn];int lowcost[maxn]; //prim数组int prim(){memset(vis,false,sizeof(vis));int minn=INF; //最小生成树int maxx=-INF; //最大的那条路vis[1]=true; //标记1for(int i=2;i<=n;i++)lowcost[i]=dis[1][i];//初始化prim队列for(int i=2;i<=n;i++){minn=INF;int k;for(int j=2;j<=n;j++) //搜出队列中最短的路if(!vis[j]&&minn>lowcost[j])                minn=lowcost[j],k=j;        vis[k]=true; //标记该点        for(int j=2;j<=n;j++) //更新队列            if(!vis[j]&&dis[k][j]<lowcost[j])                lowcost[j]=dis[k][j];        maxx=max(maxx,minn); //取最大路}    return maxx;}void solve() //呵呵- -{int sum=prim();cout<<sum<<endl;}int main(){cin>>t;while(t--){scanf("%d",&n);for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)scanf("%d",&dis[i][j]);//你敢用cin吗- -solve();}return 0;}




0 0
原创粉丝点击