Highways
来源:互联网 发布:vim c语言配置 编辑:程序博客网 时间:2024/05/21 10:20
Highways
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 54 Accepted Submission(s) : 19
Problem 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. <br>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
最小生成树问题,Prem算法一套下来解决。
代码如下:
#include<iostream>#include<stdio.h>#include<cstring>using namespace std;const int MAX=1e9;int main(){ int t,n; int p[510][510],dis[510],vis[510]; cin>>t; while(t--) { memset(p,0,sizeof(p)); memset(vis,0,sizeof(vis)); cin>>n; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { scanf("%d",&p[i][j]); } } memset(dis,0x7f,sizeof(dis)); dis[1]=0; int maxn=0,k; for(int i=1;i<=n;i++) { k=0; for(int j=1;j<=n;j++) { if(vis[j]==0 && dis[k]>dis[j]) { k=j; } } vis[k]=1;//标记成白点 for(int j=1;j<=n;j++) { if(vis[j]==0 && p[k][j]<dis[j]) { dis[j]=p[k][j]; } } } for(int i=1;i<=n;i++) if(maxn<dis[i]) maxn=dis[i]; cout<<maxn<<endl; } return 0;}
阅读全文
0 0
- Highways
- Highways
- Highways
- Highways
- Highways
- Highways
- Highways
- Highways
- Highways
- Highways
- Highways
- Highways
- Highways
- Highways
- Highways
- Highways
- Highways
- highways
- HDOJ-2066 解题报告
- HDU1863 畅通工程 prim模板
- 笔记本开启wifi,受限的原因
- 使用LinkedList实现栈数据结构特点的容器和队列数据结构特点的容器
- 用python写网络爬虫-下载网页
- Highways
- GPU编程自学6 —— 函数与变量类型限定符
- 算法九
- 给年轻人工作与职业发展的建议
- Java Arrays.sort和Collections.sort的简洁写法
- Python While循环语句 Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为: while 判断条件:
- SQL查询和优化(四)
- <8/5>集训日记
- CART分类与回归树与GBDT(Gradient Boost Decision Tree)