poj 2485 Highways 最小生成树
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 19004 | Accepted: 8815 |
Description
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 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
Sample Input
1 3
0 990 692
990 0 179
692 179 0
Sample Output
692
Hint
题目大意是要在多个村庄之间修公路,给出公路之间的距离,求总花费最小的方案中费用最高的那条公路,很裸的最小生成树问题
这题我用了prim + priority_queue实现的
#include<stdio.h>
#include<string.h>
#include<queue>
#include<utility>
using namespace std;
int map[501][501];
int n; int prime()
{
bool flag[501] = {0};
priority_queue< pair<int, int> , vector<pair<int, int > > , greater<pair<int ,int > > > q;
pair<int, int > p, new_p;
p.first = 0;
p.second = 1;
q.push(p);
int max = 0;
int t = 1;
int j;
for(j = 1; j <= n ;j++)
{
p = q.top();
q.pop();
if(flag[p.second] == 1)
{
j--;
continue;
}
if(max < p.first)
max = p.first;
flag[p.second] = 1;
int i;
for(i = 1 ; i <= n; i++)
{
if(map[p.second][i] != 0 && flag[i] == 0)
{
new_p.first = map[p.second][i];
new_p.second = i;
q.push(new_p);
}
}
}
return max;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
memset(map, 0, sizeof(map));
scanf("%d", &n);
int i, j;
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
scanf("%d", &map[i][j]);
}
}
int f = prime();
printf("%d\n", f);
} return 0;
}
poj 2485 Highways 最小生成树的更多相关文章
- POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)
...
- POJ 2485 Highways 最小生成树 (Kruskal)
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- poj 2485 Highways (最小生成树)
链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要 ...
- poj 2485 Highways
题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...
- POJ 2485 Highways【最小生成树最大权——简单模板】
链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 2485 Highways( 最小生成树)
题目链接 Description The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publ ...
- POJ 2485 Highways (求最小生成树中最大的边)
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- POJ 2485 Highways (prim最小生成树)
对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的 ...
- poj 2485 Highways(最小生成树,基础,最大边权)
题目 //听说听木看懂之后,数据很水,我看看能不能水过 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stri ...
随机推荐
- 【转】object标签和embed标签
我们现在大部分人做网页,都是直接用DW插入flash,而且DW也是所见即所得,直接生成了相应的flash显示代码.可是我们又有多少人了解这些直接由DW生成的代码呢?其实我接触flash player标 ...
- Tkinter单选框及滚动条
界面:左侧是单选框,右侧是信息显示框,下方是按扭 功能:点击开始爬取按扭,则会自动执行函数,显示在文本框中 indicatoron = 0 改变单选框按扭样式 效果图一: 效果图二: 效果图三: 示例 ...
- ubuntu 安装 boost
1.tar -zxvf boost_1_43_0.tar.gz 2.cd boost_1_43_0, 执行: sudo ./bootstrap.sh sudo ./bjam install 检验安装 ...
- JavaScript兼容性问题
Firefox浏览器不支持js的innerText属性:
- 通过改变计算机策略来解决“只能通过Chrome网上应用商店安装该程序”的方法及模版文件下载
通过改变计算机策略来解决“只能通过Chrome网上应用商店安装该程序”的方法及模版文件下载 操作步骤 1.开始 -> 运行 -> 输入gpedit.msc -> 回车确定打开计算机本 ...
- android电视安装爱奇艺等不能看视频问题
下载地址: http://pan.baidu.com/s/1dDoxA29
- LinkedHashMap和HashMap区别
import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.uti ...
- WCF 遇到 由于线程退出或应用程序请求,已放弃 I/O 操作 ListenerContextInputStream
异常类型:IOException 异常消息:An exception has been thrown when reading the stream. 异常信息: at System.ServiceM ...
- Lucene.Net 站内搜索
Lucene.Net 站内搜索 一 全文检索: like查询是全表扫描(为性能杀手)Lucene.Net搜索引擎,开源,而sql搜索引擎是收费的Lucene.Net只是一个全文检索开发包(只是帮我们 ...
- FastReport使用一——简介
一:FastReport Designer用法简介 1.使用FastReport.Net4文件夹下的Designer.exe进行模版的创建工作 图1 图1中,右键删除除==>数据区 之外的其他区 ...