poj2395 Out of Hay

题意就是给你一张无向连通图,试问对于图上所有点对(u,v)从u到v的所有路径中边权最大值的最小值的最大值。

定义f(u,v)表示从u到v所有路径中边权最大值的最小值,对所有点对取其最大。

实际上就是求图G的最小生成树的最大边权。

考虑kruskal算法流程,每次选取边权最小的且不产生圈的边加入mst。

至算法结束,图恰好连通,并且选取的边权都是最小的。

对于那些产生回路的边加入到mst中是没有意义的,因为之前保持图连通时选取的边权更小。

注意考虑重边。

http://poj.org/problem?id=2395

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <map>
 5 #include <queue>
 6 using namespace std;
 7 typedef __int64 LL;
 8 const int maxn = 2e3 + 10;
 9 const int maxm = 1e4 + 10;
10 const int inf = 1e9 + 1e8;
11 struct Edge{
12     int from, to, next, c;
13     bool operator < (const Edge& rhs) const{
14         return c < rhs.c;
15     }
16 }edge[maxm << 1];
17 struct Point{
18     int x, y, c;
19     bool operator < (const Point& rhs) const{
20         return x < rhs.x || (x == rhs.x && y < rhs.y) || (x == rhs.x && y == rhs.y && c < rhs.c);
21     }
22 };
23 int head[maxn], N;
24 Point a[maxm];
25 int n, m, k;
26 int ans;
27 bool vis[maxn];
28
29 void addEdge(int u, int v, int c){
30     edge[N].next = head[u];
31     edge[N].from = u;
32     edge[N].to = v;
33     edge[N].c = c;
34     head[u] = N++;
35 }
36
37 int fa[maxn];
38
39 int find(int u){
40     if(fa[u] == -1 || fa[u] == u) return u;
41     return fa[u] = find(fa[u]);
42 }
43 int main(){
44     //freopen("in.txt", "r", stdin);
45     while(~scanf("%d%d", &n, &m)){
46         N = 0;
47         memset(head, -1, sizeof head);
48         for(int i = 0; i < m; i++){
49             scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].c);
50             if(a[i].x > a[i].y) swap(a[i].x, a[i].y);
51         }
52         sort(a, a + m);
53         k = 0;
54         for(int i = 0; i < m; i++){
55             a[k++] = a[i];
56             while(i < m - 1 && a[i].x == a[i + 1].x && a[i].y == a[i + 1].y) ++i;
57         }
58         for(int i = 0; i < k; i++) addEdge(a[i].x, a[i].y, a[i].c);
59         sort(edge, edge + N);
60         int ans = -1;
61         memset(fa, -1, sizeof fa);
62         for(int i = 0; i < N; i++){
63             int u = edge[i].from, v = edge[i].to;
64             int fu = find(u), fv = find(v);
65             if(fu != fv) fa[fv] = fu, ans = max(ans, edge[i].c);
66         }
67         printf("%d\n", ans);
68     }
69     return 0;
70 }

时间: 2024-11-05 13:36:46

poj2395 Out of Hay的相关文章

poj2395 Out of Hay , 求MST的最长边

点击打开链接 求MST的最长边~ prim #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #define Min(a,b) (a)<(b)?(a):(b) using namespace std; const int INF = 1000000000; const int maxn = 2000 + 5; struct pto { int v, l

POJ2395 Out of Hay【Kruskal】

Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11656Accepted: 4562 Description The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situat

POJ2395 Out of Hay 【Dijkstra】

Out of Hay Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11610   Accepted: 4535 Description The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay s

Out of Hay(poj2395)(并查集)

Out of Hay Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11580   Accepted: 4515 Description The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay s

洛谷——P1547 Out of Hay

P1547 Out of Hay 题目背景 奶牛爱干草 题目描述 Bessie 计划调查N (2 <= N <= 2,000)个农场的干草情况,它从1号农场出发.农场之间总共有M (1 <= M <= 10,000)条双向道路,所有道路的总长度不超过1,000,000,000.有些农场之间存在着多条道路,所有的农场之间都是连通的. Bessie希望计算出该图中最小生成树中的最长边的长度. 输入输出格式 输入格式: 两个整数N和M. 接下来M行,每行三个用空格隔开的整数A_i, B_

[BZOJ] 1618: [Usaco2008 Nov]Buying Hay 购买干草

1618: [Usaco2008 Nov]Buying Hay 购买干草 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1216  Solved: 633[Submit][Status][Discuss] Description 约翰的干草库存已经告罄,他打算为奶牛们采购H(1≤H≤50000)磅干草,他知道N(1≤N≤100)个干草公司,现在用1到 N给它们编号.第i个公司卖的干草包重量为Pi(1≤Pi≤5000)磅,需要的开销为Ci(l≤Ci≤5

Out of Hay POJ - 2395

The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situation. There are N (2 <= N <= 2,000) farms (numbered 1..N); Bessie starts at Farm 1. She'll traverse so

Poj2395--Out of Hay(最小生成树)

Out of Hay Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13588   Accepted: 5259 Description The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay s

洛谷P1547 Out of Hay 最小生成树 并查集

洛谷P1547 Out of Hay 最小生成树 并查集 路径压缩 #include <cstdio> #include <cmath> #include <cstdlib> #include <cstring> #include <string> #include <algorithm> #include <iostream> #include <iomanip> using namespace std ;