POJ 2485 Highways
2014-08-28 09:44
309 查看
Highways
Time Limit: 1000msMemory Limit: 65536KB
This problem will be judged on PKU. Original ID: 2485
64-bit integer IO format: %lld Java class name: Main
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
1 3 0 990 692 990 0 179 692 179 0
Sample Output
692
Hint
Huge input,scanf is recommended.Source
POJ Contest,Author:Mathematica@ZSU解题:求最小生成树中的最大边。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <vector> #include <queue> #include <cstdlib> #include <string> #include <set> #include <stack> #define LL long long #define pii pair<int,int> #define INF 0x3f3f3f3f using namespace std; const int maxn = 510; struct arc { int to,w; arc(int x = 0,int y = 0):to(x),w(y) {} }; vector<arc>g[maxn]; int n,d[maxn]; bool done[maxn]; priority_queue< pii,vector< pii >,greater< pii > >q; int prim() { while(!q.empty()) q.pop(); d[1] = 0; q.push(make_pair(d[1],1)); int ans = 0; while(!q.empty()){ int u = q.top().second; int w = q.top().first; q.pop(); if(done[u]) continue; done[u] = true; ans = max(w,ans); for(int i = 0; i < g[u].size(); i++){ if(d[g[u][i].to] > g[u][i].w){ d[g[u][i].to] = g[u][i].w; q.push(make_pair(d[g[u][i].to],g[u][i].to)); } } } return ans; } int main() { int t,i,j,w; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i = 0; i <= n; i++) { g[i].clear(); d[i] = INF; done[i] = false; } for(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) { scanf("%d",&w); if(i != j) g[i].push_back(arc(j,w)); } } printf("%d\n",prim()); } return 0; }
View Code
相关文章推荐
- POJ 2485 Highways
- POJ 2485 Highways(最小生成树)
- poj 2485 Highways (最小生成树)
- POJ 2485 Highways
- poj 2395 Out of Hay、2485 Highways(求最小生成树的最长边)
- poj 2485 Highways
- 最小生成树Kruskal算法【模板】 与 并查集 例题:简单 poj 2485 Highways
- POJ 2485 Highways (kruskal 最小生成树)
- POJ2485——Highways
- POJ-2485-Highways
- 【POJ】-2485-Highways(最小生成树)
- POJ 2485 Highways(Prim中最大边)
- poj 1258 Agri-Net poj 2485 Highways
- 【二分+广搜】poj 2485 Highways
- 每日一题(5):poj2485, Highways
- poj 2485 Highways 【kruskal】
- POJ 2485 Highways 图论 prim算法 最小生成树
- POJ 2485 Highways
- POJ_2485 Highways
- POJ 2485-Highways(并查集)