Pku oj 2485 Highways(MST)

Highways

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 27506   Accepted: 12562

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.

Source

POJ Contest,Author:[email protected]

读完题之后就会发现本质是求最小生成树中边最大权值,因为题中给的是邻接矩阵中的存图方法,所以我用Prim算法实现MST,加一个Max初始化为-1,每次往MST加入边的时候与Max比较就可以

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 505;
const int Inf = 0x3f3f3f;
int map[maxn][maxn];
int dis[maxn];
int vis[maxn];
int n,m;
int t;

void init()
{
    for(int i=0;i<n;i++)
    {
        dis[i] = map[i][0];
        vis[i] = 0;
    }
}

int prim()
{
    init();
    dis[0] = 0;
    vis[0] = 1;
    int ans = 0;
    int max = -1;
    for(int i=0;i<n-1;i++)
    {
        int min = Inf;
        int temp;
        for(int j=0;j<n;j++)
        {
            if(!vis[j] && dis[j] < min)
            {
                temp = j;
                min = dis[j];
            }
        }
        vis[temp] = 1;
        ans += dis[temp];
        if(dis[temp] > max)
            max = dis[temp];
        for(int j=0;j<n;j++)
        {
            if(!vis[j] && dis[j] > map[temp][j])
                dis[j] = map[temp][j];
        }
    }
    return max;
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
                scanf("%d",&map[i][j]);
        }
        int ans = prim();
        printf("%d\n",ans);
    }
    return 0;
}

/*
1
3
0 990 692
990 0 179
692 179 0
*/
时间: 2024-10-09 16:53:25

Pku oj 2485 Highways(MST)的相关文章

POJ #2485 Highways MST中的最大边权

Description 问题描述:链接 思路 题目很直接,容易看出建的图是一张完全图,需要求出图中最小生成树的最大边权.由于数据上界已经定死了,那么选择用静态邻接表存建图.由于图是稠密图,那么求MST的话就选择 prim . 做完 POJ #2253 Frogger 变种Dijkstra 后再做这个就很有感觉了.其实prim中的核心操作就是记录并更新点v到树的最短距离,然后把所有最短距离之中的最小值选出,将该点其加入树集.这个树集与dijkstra中的点集是异曲同工的. 能用 scanf 的话尽

poj 2485 Highways(最小生成树)

题目链接:http://poj.org/problem?id=2485 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 plann

POJ 2485 Highways &amp;&amp; HDU1102(20/200)

题目链接:Highways 没看题,看了输入输出,就有种似曾相识的感觉,果然和HDU1102 题相似度99%,但是也遇到一坑 cin输入竟然TLE,cin的缓存不至于这么狠吧,题目很水,矩阵已经告诉你了,就敲个模板就是了,5分钟,1A 题意就是打印,最小生成树的最大边权,改了改输入,水过 这个题完了,我的个人POJ计划进度以完成 20/200,这其中主要是图论的题,等下周把POJ计划图论的题目打完,就回头打模拟!我就不信还能比图论难 #include <iostream> #include &

poj 2485 Highways

链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,只不过要求的不是最小价值,而是最小生成树中的最大权值,只需要加个判断 比较最小生成树每条边的大小就行 #include<cstdio> #include<algorithm> using namespace std; int f[510],n,m; struct stu { int a,b,c; }t[20100]; int cmp(struct

hdu 2485 Highways

题意:Flatopia岛要修路,这个岛上有n个城市,要求修完路后,各城市之间可以相互到达,且修的总路程最短.求所修路中的最长的路段 最小生成树的一道题,很裸的一道题,不知道为什么就是编译过不了. #include<iostream> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; int w[200000],u[200000],v[200000],p[5

STP 4 - MST 和 PVST 对比 (侧重于MST)

802.1w = Rapid STP (uplinkFast & backbonefast 的功能被嵌入了,相当于自动生效) 802.1s = PVST/PVST+ (per VLAN spanning-tree) 两者都用VLAN来区分不同生成树进程 PVST中每个VLAN是一个进程 MST自定义进程 MST路径选择与PVST方法相同 ==== 相关命令与执行 更改MST根桥选择 手动更改BID优先级 - spanning-tree mst [instance id]priority  (lo

月球美容计划之最小生成树(MST)

寒假学的两个算法,普里姆,克鲁斯卡尔终于弄明白了,可以发总结了 先说说普里姆,它的本质就是贪心,先从任意一个点开始,找到最短边,然后不断更新更新len数组,然后再选取最短边并标记经过的点,直到所有的点被标记,或者说已经选好了n-1条边. 拿SDUTOJ2144为例,代码如下,可做模板 #include <stdio.h> #include <string.h> #define INF 1000000 //最小生成树 //普里姆 int G[200][200]; int len[20

COJ 0500 杨老师的路径规划(MST)最小生成树

杨老师的路径规划(MST) 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 为满足同学们需求,杨老师在实验楼4层新建了好多个计算机教室供同学们使用.可是这样的话,由于路径很长,杨老师发现越来越难亲自走到每一个机房看看同学们有没有在玩游戏了.请你现在帮杨老师设计一个程序,给你每个教室间的路径长,设计出一条路线使每两个教室间都能联通且总长度最小,你只需要输出这个最小值即可.(裸MST) 输入 测试用例的第1行给出教室数目N ( 

UVA 1494 - Qin Shi Huang&#39;s National Road System(MST)

UVA 1494 - Qin Shi Huang's National Road System 题目链接 题意:秦始皇修路,要求所有道路连通,现在道士徐福可以用法术修一条路,问现在用法术修路的两边的人口数A,除以总修路长度B的最大值A/B是多少 思路:先求出最小生成树,然后利用dfs找出每两点之间的最大权的边的权值,然后在枚举哪两个城市需要法术修路,这样就可以记录下答案最大值 代码: #include <cstdio> #include <cstring> #include <