POJ 2395Out of Hay(Prim模板题)
来源:互联网 发布:淘宝鹦鹉 编辑:程序博客网 时间:2024/06/05 05:36
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.
题意:贝丝背着她的水壶出门啦。到一个点她水壶里的水就可以补满。当然啦,贝丝要走尽可能短的路走遍每个点。求一下贝丝需要带的水壶的最小容积。
解题思路:求最小生成树中权值最大的边是多少。套用Prim模板,在每次更新最小值min的同时也更新下max(最大边长)的值。
AC代码:
#include<stdio.h>#include<string.h>#define INF 0x3f3f3f3fint mp[2010][2010],dis[2010],book[2010],n,m;void prim(){ int i,j,k,u,maxx=-1,min; for(i=1;i<=n;i++) { dis[i]=mp[1][i]; } book[1]=1; for(i=1;i<=n-1;i++) { min=INF; for(j=1;j<=n;j++) { if(book[j]==0&&dis[j]<min) { min=dis[j]; u=j; } } book[u]=1; if(maxx<min) maxx=min; for(k=1;k<=n;k++) { if(book[k]==0&&mp[u][k]<dis[k]) { dis[k]=mp[u][k]; } } } printf("%d\n",maxx);}int main(){ int i,j,x,y,len; scanf("%d%d",&n,&m); for(i=0;i<=n;i++) { dis[i]=INF; book[i]=0; } for(i=0;i<=n;i++) { for(j=0;j<=n;j++) { if(i!=j) { mp[i][j]=INF; } else { mp[i][j]=0; } } } for(i=1;i<=m;i++) { scanf("%d%d%d",&x,&y,&len); if(len<mp[x][y]) mp[x][y]=mp[y][x]=len; } prim(); return 0;}
同时也可以用Kruskal模板:
#include<stdio.h>#include<algorithm>#include<math.h>using namespace std;int set[2010];struct edge{ int u; int v; int w;} e[11000];bool cmp(edge a,edge b){ return a.w<b.w;}int findx(int x){ int r=x; if(set[r]!=r) r=findx(set[r]); return set[r];}int merge(int x,int y){ int fx,fy; fx=findx(x); fy=findx(y); if(fx!=fy) { set[fx]=fy; return 1; } return 0;}int main(){ int n,m,i; while(~scanf("%d%d",&n,&m)) { int maxx=-1; int sum=0; for(i=1;i<=n;i++) set[i]=i; for(i=1;i<=m;i++) { scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w); } sort(e+1,e+m+1,cmp); int num=0; for(i=1;i<=m;i++) { if(merge(e[i].u,e[i].v)) { if(maxx<e[i].w) maxx=e[i].w; } } printf("%d\n",maxx); } return 0;}
- POJ 2395Out of Hay(Prim模板题)
- POJ Out of Hay(2395 prim最小生成树)
- 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
- POJ-2395 Out of Hay
- POJ 2395 Out of Hay
- poj 2395 Out of Hay
- JavaEE 中DEBUG模式的设计
- 基于Docker搭建ActiveMQ的高可用集群
- oracle取出每个分组的第一条记录
- js格式化小数,保留指定长度是截取,不是四舍五入
- Codeforces #39C: Moon Craters 题解
- POJ 2395Out of Hay(Prim模板题)
- 【Eternallyc】模板题-Floyd算法
- Ueditor与SpringMVC结合配置
- Tensorflow可以使用tensor的name索引tensor,用于sess.run
- hibernate_检索方式
- 点个数
- IO流,文件流
- redis + lua解决抢红包高并发的问题
- Android 多媒体(播放应用内的音乐)