POJ 2305 Out of Hay(Kruscal)
来源:互联网 发布:java.util.zip 加密 编辑:程序博客网 时间:2024/05/29 16:34
Out of Hay
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14555 Accepted: 5654
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 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.
OutputLine 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.
#include<iostream>#include<algorithm>using namespace std;struct EDGE{ int x; int y; int w;}edge[100001];//保存边信息int MFSet[2005];//并查集存储结构bool comp(EDGE &s1,EDGE &s2)//sort比较函数{ return s1.w<s2.w;}int getFather(int t)//并查集查找根节点{ if(t==MFSet[t]) return t; else return MFSet[t]=getFather(MFSet[t]);}int main(){ int n,m; while(cin>>n>>m) { //memset(edge,-1,sizeof(edge)); int ans=-1; for(int i=0;i<n;i++)//初始化并查集 MFSet[i]=i; for(int i=0;i<m;i++) { int a,b,c; cin>>a>>b>>c; edge[i].x=a; edge[i].y=b; edge[i].w=c;//注意重复边保存最小的 for(int j=0;j<m;j++)//Kruscal算法此处较麻烦 { if(edge[i].x==a && edge[i].y==b) { if(edge[i].w > c) edge[i].w=c; } } } sort(edge,edge+m,comp); for(int i=0;i<m;i++)//找边 { int v1=getFather(edge[i].x); int v2=getFather(edge[i].y); if(v1!=v2) { MFSet[v1]=v2;//并查集中两个集合合并 if(ans<edge[i].w) ans=edge[i].w; } } cout<<ans<<endl; }}
- POJ 2305 Out of Hay(Kruscal)
- poj Out of hay 最小生成树 kruscal
- POJ 2395 Out of Hay (Kruscal最小生成树)
- Out of Hay POJ
- POJ Out of Hay
- poj 2395 Out of Hay
- poj 2395 Out of Hay
- poj 2395 Out of Hay
- poj 2395 Out of Hay
- poj -2395-Out of Hay
- POJ-2395-Out of Hay
- POJ 2395 Out of Hay
- POJ 2395 Out of Hay
- POJ 2395 Out of Hay
- Out of Hay.(POJ-2395)
- POJ 2395 Out of Hay
- POJ 2395 Out of Hay
- POJ 2395--Out of Hay
- iOS app打包 -- 生成ipa测试包 步骤详解
- Mybatis_java项目
- 协同开发时, 如何合并不稳定的联调代码?
- 一个90%程序员写不对的算法——二分查找
- 顺序建立链表
- POJ 2305 Out of Hay(Kruscal)
- Visual EmbedLinux Tools 0.2.5
- 计算机组成.人机转换大字典.几种字符编码
- 如何做好自己的工作
- 解决Warning:Ignoring InnerClasses attribute for an anonymous inner class associated EnclosingMethod at
- Qt: test! test!
- Qt:快速设计对话框. C++ GUI
- Qt:亲手写框体 C++ GUI (没课的…
- Linux:Qt的编译和安装配置 (本…