poj 2485 Highways
2015-07-30 21:35
239 查看
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
Sample Output
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int Map[5100][5010];
int eveco[5100];
int vis[5100];
int n;
int Prim()
{
memset(vis,false,sizeof(vis));
int cost = 0;
for(int i=1; i<=n; i++)
{
eveco[i] = Map[1][i];
}
eveco[1] = 0;
vis[1] = true;
for(int i=1; i<n; i++)
{
int pos;
int m = INF;
for(int j=1; j<=n; j++)
{
if(!vis[j] && m > eveco[j])
{
pos = j;
m = eveco[j];
}
}
if(m > cost)
cost = m;
vis[pos] = true;
for(int j=1; j<=n; j++)
{
if(!vis[j] && eveco[j] > Map[pos][j])
{
eveco[j] = Map[pos][j];
}
}
}
return cost;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
scanf("%d",&Map[i][j]);
}
}
printf("%d\n",Prim());
}
return 0;
}
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int Map[5100][5010];
int eveco[5100];
int vis[5100];
int n;
int Prim()
{
memset(vis,false,sizeof(vis));
int cost = 0;
for(int i=1; i<=n; i++)
{
eveco[i] = Map[1][i];
}
eveco[1] = 0;
vis[1] = true;
for(int i=1; i<n; i++)
{
int pos;
int m = INF;
for(int j=1; j<=n; j++)
{
if(!vis[j] && m > eveco[j])
{
pos = j;
m = eveco[j];
}
}
if(m > cost)
cost = m;
vis[pos] = true;
for(int j=1; j<=n; j++)
{
if(!vis[j] && eveco[j] > Map[pos][j])
{
eveco[j] = Map[pos][j];
}
}
}
return cost;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
scanf("%d",&Map[i][j]);
}
}
printf("%d\n",Prim());
}
return 0;
}
相关文章推荐
- jquery
- poj 1390 dp(方盒游戏)
- 拓扑排序
- Symmetric Tree
- 在 eclipse 上安装 genymotion 插件不成功的解决办法
- Mac OSX 上 Android Studio 编译使用 JAVA8 及 retrolambda 的项目出错解决方案
- 黑马程序员----java基础面向对象总结
- Openssl摘要算法接口函数使用
- 黑马程序员---iOS基础---C语言中的预处理命令
- FTP服务器访问方法
- 66. Plus One
- 重复登录脚本修改02
- 我的第一篇博文
- Leetcode Q11:Container With Most Water
- c++ 控制台流和字符串流
- 嵌入式软件开发——汇顶 笔试 一面 二面
- IOS--UI--LessonGCD
- POJ 2513--Colored Sticks【字典树编号 && 并查集判连通 && 无向图欧拉路】
- WinEdt和LaTeX的简介
- c++ 控制台流和字符串流