POJ #2485 Highways MST中的最大边权

Description



  问题描述:链接

思路



  题目很直接,容易看出建的图是一张完全图,需要求出图中最小生成树的最大边权。由于数据上界已经定死了,那么选择用静态邻接表存建图。由于图是稠密图,那么求MST的话就选择 prim 。

  做完 POJ #2253 Frogger 变种Dijkstra 后再做这个就很有感觉了。其实prim中的核心操作就是记录并更新点v到树的最短距离,然后把所有最短距离之中的最小值选出,将该点其加入树集。这个树集与dijkstra中的点集是异曲同工的。

  能用 scanf 的话尽量用 scanf 吧,比 cin 快很多。

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
#define INF 0x3f3f3f3f
const int MAXN = 510;
const int EDGE_MAXN = 250010;
int n;
//链式前向星
struct Edge {
    int to;
    int w; //2^31-1 > 65536
    int next;
}e[EDGE_MAXN];
int head[MAXN], cnt;

void addEdge (int u, int v, int w) {
    cnt++;
    e[cnt].to = v;
    e[cnt].w = w;
    e[cnt].next = head[u];
    head[u] = cnt;
}

void initG() {
    memset(head, -1, sizeof(head));
    cnt = 0;
    scanf ("%d", &n);
    for (int i = 1; i <= n; i++) {
        int tmp;
        for (int j = 1; j <= i-1; j++) {
            scanf ("%d", &tmp);
            addEdge(i, j , tmp);
        }
        scanf ("%d", &tmp); // i = j 形成自环,对建图并没有用
        for (int j = i+1; j <= n; j++) {
            scanf ("%d", &tmp);
            addEdge(i, j, tmp);
        }
    }
}

//求最小生成树的最大边权
struct Node{
    int id;
    int key;
    Node(int v, int w) : id(v), key(w) {}
    friend bool operator < (const Node& a, const Node& b) {
        return a.key > b.key;
    }
};
int d[MAXN]; //v到树的最短距离估计值
int vis[MAXN];
void prim (const int& s, int& max) {
    for (int i = 1; i <= n; i++) d[i] = INF, vis[i] = false;
    d[s] = 0;
    priority_queue <Node> pq;
    pq.push(Node(s, d[s]));
    while (!pq.empty()) {
        int u = pq.top().id; pq.pop();
        if (vis[u]) continue;
        vis[u] = true;
        if (max < d[u]) max = d[u];
        for (int i = head[u]; i != -1; i = e[i].next) {
            int v = e[i].to;
            if (!vis[v] && d[v] > e[i].w) {
                d[v] = e[i].w; //更新v到树的最短距离
                pq.push (Node(v, d[v]));
            }
        }
    }
}

int main(void) {
    int case_num;
    cin >> case_num;
    while (case_num--) {
        initG();
        int ans = 0;
        prim(1, ans);
        printf ("%d\n", ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Bw98blogs/p/8467422.html

时间: 2024-10-09 16:53:19

POJ #2485 Highways MST中的最大边权的相关文章

poj 2485 求最小生成树中 最大的一个权值

Sample Input 1 //T 3 //n0 990 692 //邻接矩阵990 0 179692 179 0Sample Output 692 prim 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # define LL long long 7 using na

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

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个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要加个推断 比較最小生成树每条边的大小即可 kruskal算法 #include<cstdio> #include<algorithm> using namespace std; int f[510],n,m; struct stu { int a,b,c; }t[20100]; int

POJ 2485 Highways (kruskal 最小生成树)

Highways POJ 2485so 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 ca

poj 1751 输出MST中新加入的边

给出结点的坐标 以及已建好的边 要输出MST中加入的边(已建好的边就不用输出了)结点的编号从1开始注意这题只有一组数据 不能用多组输入 否则就超时(在这被坑惨了Orz) Sample Input 91 50 0 3 24 55 10 45 21 25 331 39 71 2Sample Output 1 63 74 95 78 3 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 #

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

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 i

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 Fl