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 some or all of the M (1 <= M <= 10,000) two-way roads whose length does not exceed 1,000,000,000 that connect the farms. Some farms may be multiply connected with different length roads. All farms are connected one way or another to Farm 1.

Bessie is trying to decide how large a waterskin she will need. She knows that she needs one ounce of water for each unit of length of a road. Since she can get more water at each farm, she‘s only concerned about the length of the longest road. Of course, she plans her route between farms such that she minimizes the amount of water she must carry.

Help Bessie know the largest amount of water she will ever have to carry: what is the length of longest road she‘ll have to travel between any two farms, presuming she chooses routes that minimize that number? This means, of course, that she might backtrack over a road in order to minimize the length of the longest road she‘ll have to traverse.Input

* Line 1: Two space-separated integers, N and M.

* Lines 2..1+M: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, describing a road from A_i to B_i of length L_i.

Output

* Line 1: A single integer that is the length of the longest road required to be traversed.

Sample Input

3 3
1 2 23
2 3 1000
1 3 43

Sample Output

43

Hint

OUTPUT DETAILS:

In order to reach farm 2, Bessie travels along a road of length 23. To reach farm 3, Bessie travels along a road of length 43. With capacity 43, she can travel along these roads provided that she refills her tank to maximum capacity before she starts down a road.

题意:有n个农场,贝西在1号农场,要访问其他n-1个农场,给出m条路,a  b  c表示a农场到b农场路程为c(两个农场间可能有多条路)。贝西会挑选最短的路径来访问完这n-1个农场。 问在此过程中贝西会经过的最大边是多大?

题解:题目意思太蛋疼了,找最小生成树上最长的边。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 const int maxn=10005;
 8
 9 struct edge{
10     int u,v,cost;
11     bool operator<(const edge& i)const{
12         return cost<i.cost;
13     }
14 }es[maxn];
15
16 int n,m;
17 int F[2*maxn];
18
19 int Find(int a){
20     if(a!=F[a]) F[a]=Find(F[a]);
21     return F[a];
22 }
23
24 bool unite(int a,int b){
25     int x=Find(a),y=Find(b);
26     if(x==y) return false;
27     else { F[x]=y; return true; }
28 }
29
30 int Kruskal(){
31     sort(es,es+m);
32     int ans=0;
33     for(int i=0;i<m;i++) if(unite(es[i].u,es[i].v)) ans=max(ans,es[i].cost);
34     return ans;
35 }
36
37 int main()
38 {   cin>>n>>m;
39     for(int i=1;i<=n;i++) F[i]=i;
40     for(int i=0;i<m;i++) scanf("%d%d%d",&es[i].u,&es[i].v,&es[i].cost);
41     int ans=Kruskal();
42     cout<<ans<<endl;
43 }
时间: 2024-10-08 18:17:10

Out of Hay POJ - 2395的相关文章

POJ 2395 Out of Hay

http://poj.org/problem?id=2395 裸最小生成树 输出树中最大cost的边值 直接prim 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <queue> 5 #include <algorithm> 6 #define READ() freopen("in.txt", "r",

POJ 2395 Out of Hay 草荒 (MST,Kruscal)

题意:Bessie要从牧场1到达各大牧场去,他从不关心他要走多远,他只关心他的水袋够不够水,他可以在任意牧场补给水,问他走完各大牧场,最多的一次需要多少带多少单位的水? 思路:其实就是要让所带的水尽量少,即所选的每条路都要尽量短,即最小瓶颈生成树.其实也就是最小生成树.再求生成树上权值最大的边即可. 1 //#include <bits/stdc++.h> 2 #include <cstdio> 3 #include <iostream> 4 #include <

挑战程序设计竞赛 2.5 它们其实都是“图”

[Summarize] 1.注意对图是否连通的判定 2.灵活运用边权取负的技巧 AOJ 0189:Convenient Location /* 给出一张无向图,现在求一个点,使得这个点到所有点的距离和最小 输出这个点的编号和距离和 */ #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int N=10,INF=0x3f3f3f3f; int n,d[

《挑战程序设计竞赛》2.5 它们其实都是图

poj 2139 Six Degrees of Cowvin Baconfloyd的模板题. 建图的时候记得i==j的时候ma[i][j]=0;其他情况是inf 1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 5 int N,M; 6 const int maxn=305; 7 const int INF=0x3f3f3f3f; 8 int d[maxn][maxn],a[maxn]; 9

《挑战程序设计竞赛》课后练习题解集——2.5 它们其实都是“图”

2.5 它们其实都是“图” 最短路 AOJ 0189  求图上一点,到所有其他点的距离之和最小 Floyd算法 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int inf = 1e8; 5 int d[11][11]; 6 7 int main() { 8 int n; 9 while (cin >> n) { 10 if (n == 0) break; 11 12 for (int i = 0; i &l

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京

转载:poj题目分类(侵删)

转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K–0.50K:中短代码:0.51K–1.00K:中等代码量:1.01K–2.00K:长代码:2.01K以上. 短:1147.1163.1922.2211.2215.2229.2232.2234.2242.2245.2262.2301.2309.2313.2334.2346.2348

poj题库分类

初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,hea

POJ题目(转)

http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (