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 ...
随机推荐
- mongodb 最佳实践
MongoDB功能预览:http://pan.baidu.com/s/1k2UfW MongoDB在赶集网的应用:http://pan.baidu.com/s/1bngxgLp MongoDB在京东的 ...
- .Net分布式缓存应用实例:Couchbase
转自:http://www.cnblogs.com/wu-jian Couchbase概述 Couchbase最早叫Membase,是由Memcached项目组的一些头目另立的山头. 2011年与Co ...
- HTML5跨浏览器表单及HTML5表单的渐进增强
HTML5跨浏览器表单 http://net.tutsplus.com/tutorials/html-css-techniques/how-to-build-cross-browser-html5-f ...
- Javascript 图片左右滑动与切换
Html代码 : <div class="v_out v_out_p"> <div class="prev"> <a href=& ...
- ubuntu ipv6网络电视(avplay)
首先在ubuntu下安装好ipv6 (话说是已经装好了的,不过最好检查以下) 网上有很多资源,我不写了. 测试一下 :ping ipv6.scau.edu.cn 另外,关于ipv6 网络播放器很多人推 ...
- activiti自定义流程之自定义表单(一):环境配置
先补充说一下自定义流程整个的思路,自定义流程的目的就是为了让一套代码解决多种业务流程,比如请假单.报销单.采购单.协作单等等,用户自己来设计流程图. 这里要涉及到这样几个基本问题,一是不同的业务需求, ...
- 20个超实用的JavaScript技巧及最佳实践
1.第一次给变量赋值时,别忘记var关键字 给一个未声明的变量赋值,该变量会被自动创建为全局变量,在JS开发中,应该避免使用全局变量. 2.使用===替换== 并且永远不要使用=或!=. ...
- JavaScript权威指南 第二章 词法结构
这章主要把保留字说一下 JavaScript 把一些标识符拿出来用做自己的关键字.因此,就不能再在程序中把这些关键字用做标识符了: break delete function return typeo ...
- 黄聪:get_posts 函数 | wordpress
get_posts 函数,简单的来讲是 get_post 的复数新形势,但因为是文章多篇提取,所以使用方法上却略有不同,支持众多参数选择需要提取的文章,在 CMS 主题中经常被用到,当然如果你对 Wo ...
- Excel 操作类
转载:http://www.cnblogs.com/fellowcheng/archive/2010/08/21/1805158.html ExcelHelper(Excel2007) Code hi ...