POJ-2485 Highways---最小生成树中最大边

题目链接:

https://vjudge.net/problem/POJ-2485

题目大意:

求最小生成树中的最大边

思路:

是稠密图,用prim更好,但是规模不大,kruskal也可以过

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<queue>
  7 #include<stack>
  8 #include<map>
  9 #include<sstream>
 10 using namespace std;
 11 typedef long long ll;
 12 const int maxn = 3e5 + 10;
 13 const int INF = 1 << 30;
 14 int dir[4][2] = {1,0,0,1,-1,0,0,-1};
 15 int T, n, m, x;
 16 struct edge
 17 {
 18     int u, v, w;
 19     bool operator <(const edge& a)const
 20     {
 21         return w < a.w;
 22     }
 23 };
 24 edge a[maxn];
 25 int par[600], high[600];
 26 //初始化n个元素
 27 void init(int n)
 28 {
 29     for(int i = 0; i < n; i++)
 30     {
 31         par[i] = i;
 32         high[i] = 0;
 33     }
 34 }
 35 //查询树的根
 36 int Find(int x)
 37 {
 38     return par[x] == x ? x : par[x] = Find(par[x]);//路径压缩
 39 }
 40 void unite(int x, int y)
 41 {
 42     x = Find(x);
 43     y = Find(y);
 44     if(x == y)return;
 45     if(high[x] < high[y])par[x] = y;//y的高度高,将x的父节点设置成y
 46     else
 47     {
 48         par[y] = x;
 49         if(high[x] == high[y])high[x]++;
 50     }
 51 }
 52 bool same(int x, int y)
 53 {
 54     return Find(x) == Find(y);
 55 }
 56 void kruskal(int n, int m)//点数n,边数m
 57 {
 58     int ans = 0;//mst权值
 59     int num= 0;//已经选择的边的边数
 60     sort(a, a + m);//边进行排序
 61     init(n);//初始化并查集
 62     for(int i = 0; i < m; i++)
 63     {
 64         int u = a[i].u;
 65         int v = a[i].v;
 66         if(Find(u - 1) != Find(v - 1))//图最开始的下标是1,并查集是0
 67         {
 68             //printf("%d %d %d\n", u, v, a[i].w);
 69             //sum_mst += a[i].w;
 70             ans = max(ans, a[i].w);
 71             num++;
 72             unite(u - 1, v - 1);
 73         }
 74         if(num >= n - 1)break;
 75     }
 76     //printf("weight of mst is %d\n", sum_mst);
 77     printf("%d\n", ans);
 78 }
 79 int main()
 80 {
 81     cin >> T;
 82     while(T--)
 83     {
 84         scanf("%d", &n);
 85         int x;
 86         int tot = 0;
 87         for(int i = 1; i <= n; i++)
 88         {
 89             for(int j = 1; j <= n; j++)
 90             {
 91                 scanf("%d", &x);
 92                 if(i == j)continue;
 93                 a[tot].u = i;
 94                 a[tot].v = j;
 95                 a[tot++].w = x;
 96             }
 97         }
 98         kruskal(n, tot);
 99     }
100     return 0;
101 }

原文地址:https://www.cnblogs.com/fzl194/p/8723408.html

时间: 2024-10-13 11:28:11

POJ-2485 Highways---最小生成树中最大边的相关文章

POJ #2485 Highways MST中的最大边权

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

POJ 2485 Highways 最小生成树 (Kruskal)

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

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

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23426   Accepted: 10829 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Fl

POJ 2485 Highways (最小生成树prim)

题目大意:    有一个岛国,没有高速公路,交通非常困难.政府意识到了这一问题,他们计划建造一些高速公路,这样可以使任何一对城镇都不离开公路系统.所有公路都是沿直线建造并且是双向的.政府想降低花费,所以想找到一种建造方案,能连接所有的城镇,并且公路的总长度最小.注意题目求的是最小生成树最长的那条边. #include <cstdio> #include <cstring> #include <algorithm> #define N 600 #define INF 0x

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(最小生成树)

题目链接: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

链接: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 &amp;&amp; HDU1102(20/200)

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

poj 2485(prim最小生成树)

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

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