点击打开链接

Highways
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19004   Accepted: 8815

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

1

3
0 990 692
990 0 179
692 179 0

Sample Output

692

Hint

Huge input,scanf is recommended.

题目大意是要在多个村庄之间修公路,给出公路之间的距离,求总花费最小的方案中费用最高的那条公路,很裸的最小生成树问题

这题我用了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 最小生成树的更多相关文章

  1. POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)

                                                                                                         ...

  2. POJ 2485 Highways 最小生成树 (Kruskal)

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

  3. poj 2485 Highways (最小生成树)

    链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要 ...

  4. poj 2485 Highways

    题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...

  5. POJ 2485 Highways【最小生成树最大权——简单模板】

    链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  6. POJ 2485 Highways( 最小生成树)

    题目链接 Description The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publ ...

  7. POJ 2485 Highways (求最小生成树中最大的边)

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

  8. POJ 2485 Highways (prim最小生成树)

    对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的 ...

  9. poj 2485 Highways(最小生成树,基础,最大边权)

    题目 //听说听木看懂之后,数据很水,我看看能不能水过 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stri ...

随机推荐

  1. maven学习笔记(定制普通Java一个项目)

    创建一个新项目: mvn archetype:generate -DgroupId=cn.net.comsys.ut4.simpleweather -DartifactId=simple-weathe ...

  2. eclipse解压后启动弹出A Java Runtime Evironment(JRE) or Java Development Kit(JDK)....

    系统环境:win7 64bit JDK:jdk-7u79-windows-x64 Eclipse:eclipse-jee-helios-win32 启动eclipse:弹出A Java Runtime ...

  3. Android 隐藏Fragment

    1.隐藏Fragment FragmentManager fManager = getFragmentManager(); fManager.beginTransaction() .setCustom ...

  4. PHPCMS-首页的二级导航、轮播效果

    导航栏:(header.html) <div id="menu"> <a href="{siteurl($siteid)}"><d ...

  5. ajax简单案例:字符串返回类型

    小知识点: implode("^",$v) 拼数组为字符串, split/explode("|")   拆字符串为数组; "你好"-> ...

  6. ios外包公司——技术分享:IOS开发教程

        iOS入门培训,适合已经有C/C++/Java/C#基础的人学习.   本大仙主讲,总共4讲(第4讲尚在制作中),这仅仅是iOS开发的入门而已.学完本教程,应该已经足够你自学并开发app了. ...

  7. oracle学习笔记(一)配置监听

    服务器端: 监听器   lsnrctl 启动监听 lsnrctl start [LISTENER] 查看监听 lsnrctl  status  或者  lsnrctl  service C:\Docu ...

  8. (VS TFS) Adding existing project to solution in TFS.

    正常的情况话,直接加入project,然后选择"Source control" -> “Add selected projects to source control.... ...

  9. javascript世界一等公民—函数

    简介 在很多传统语言(C/C++/Java/C#等)中,函数都是作为一个二等公民存在,你只能用语言的关键字声明一个函数然后调用它,如果需要把函数作为参数传给另一个函数,或是赋值给一个本地变量,又或是作 ...

  10. C# STUDY

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...