POJ 2395 Out of Hay【最小生成树】
2016-04-14 13:09
393 查看
Out of Hay
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.
Output
* Line 1: A single integer that is the length of the longest road required to be traversed.
Sample Input
Sample Output
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.
Source
USACO 2005 March Silver
题意:
给出若干个顶点,以及他们之间的某些道路的长度,规划一个方案使得所有点能相互到达,而且总的边权值最小,求其中所有的边中最长的边的权值
题解:
最小生成树的变形,只需要在生成树的时候进行边的最大值的更新就行了
/* http://blog.csdn.net/liuke19950717 */
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=2005;
int pre[maxn];
struct node
{
int a,b,len;
}edge[maxn*5];
void init(int n)
{
for(int i=1;i<=n;++i)
{
pre[i]=i;
}
}
int find(int x)
{
int r=x;
while(r!=pre[r])
{
r=pre[r];
}
int i=x,j;
while(i!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
int join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy)
{
pre[fx]=fy;
return 1;
}
return 0;
}
int cmp(node a,node b)
{
return a.len<b.len;
}
int kruscal(int n,int m)
{
sort(edge,edge+m,cmp);
int cnt=0,ans=0;
for(int i=0;i<m&&cnt<n-1;++i)
{
if(join(edge[i].a,edge[i].b))
{
ans=max(ans,edge[i].len);
++cnt;
}
}
return ans;
}
int main()
{
int n,m;
//freopen("shuju.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
init(n);
for(int i=0;i<m;++i)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
node tp={a,b,c};
edge[i]=tp;
}
printf("%d\n",kruscal(n,m));
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14141 | Accepted: 5489 |
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.
Source
USACO 2005 March Silver
题意:
给出若干个顶点,以及他们之间的某些道路的长度,规划一个方案使得所有点能相互到达,而且总的边权值最小,求其中所有的边中最长的边的权值
题解:
最小生成树的变形,只需要在生成树的时候进行边的最大值的更新就行了
/* http://blog.csdn.net/liuke19950717 */
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=2005;
int pre[maxn];
struct node
{
int a,b,len;
}edge[maxn*5];
void init(int n)
{
for(int i=1;i<=n;++i)
{
pre[i]=i;
}
}
int find(int x)
{
int r=x;
while(r!=pre[r])
{
r=pre[r];
}
int i=x,j;
while(i!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
int join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy)
{
pre[fx]=fy;
return 1;
}
return 0;
}
int cmp(node a,node b)
{
return a.len<b.len;
}
int kruscal(int n,int m)
{
sort(edge,edge+m,cmp);
int cnt=0,ans=0;
for(int i=0;i<m&&cnt<n-1;++i)
{
if(join(edge[i].a,edge[i].b))
{
ans=max(ans,edge[i].len);
++cnt;
}
}
return ans;
}
int main()
{
int n,m;
//freopen("shuju.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
init(n);
for(int i=0;i<m;++i)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
node tp={a,b,c};
edge[i]=tp;
}
printf("%d\n",kruscal(n,m));
}
return 0;
}
相关文章推荐
- Linux IO Scheduler(Linux IO 调度器)
- 二叉树层次遍历(借助队列实现)
- 《Linux内核设计与实现》第4章读书整理
- 全局编码过滤器
- mybatis错误 Mapped Statements collection does not contain value for
- log4j.properties学习
- JAVA写Excel文件
- 更新Python以及随后的nose,easy_install,pip,numpy,scipy和theano
- a标签的url带参页面跳转遇到的问题
- 程序员必知的8大排序
- 资深首席架构师眼中的架构应该是怎样的?
- osgeo default passwords
- 在 Visual Studio 2013 中使用 Grunt, Bower 和 NPM
- 【bzoj1503】[NOI2004]郁闷的出纳员
- Aizu 2224 Save your cats【最大生成树】
- OpenCV学习笔记(十一)(十二)(十三)(十四)(十五)
- 205315Java实验二实验报告
- andriod
- 数论练习
- win7下利用mingw32编译gtest