POJ 2455-Secret Milking Machine(网络流_最大流+二分查找)
来源:互联网 发布:土耳其历史 知乎 编辑:程序博客网 时间:2024/05/22 18:22
Secret Milking Machine
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10119 Accepted: 2973
Description
Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within his farm and needs to be able to get to the machine without being detected. He must make a total of T (1 <= T <= 200) trips to the machine during its construction. He has a secret tunnel that he uses only for the return trips.
The farm comprises N (2 <= N <= 200) landmarks (numbered 1..N) connected by P (1 <= P <= 40,000) bidirectional trails (numbered 1..P) and with a positive length that does not exceed 1,000,000. Multiple trails might join a pair of landmarks.
To minimize his chances of detection, FJ knows he cannot use any trail on the farm more than once and that he should try to use the shortest trails.
Help FJ get from the barn (landmark 1) to the secret milking machine (landmark N) a total of T times. Find the minimum possible length of the longest single trail that he will have to use, subject to the constraint that he use no trail more than once. (Note well: The goal is to minimize the length of the longest trail, not the sum of the trail lengths.)
It is guaranteed that FJ can make all T trips without reusing a trail.
The farm comprises N (2 <= N <= 200) landmarks (numbered 1..N) connected by P (1 <= P <= 40,000) bidirectional trails (numbered 1..P) and with a positive length that does not exceed 1,000,000. Multiple trails might join a pair of landmarks.
To minimize his chances of detection, FJ knows he cannot use any trail on the farm more than once and that he should try to use the shortest trails.
Help FJ get from the barn (landmark 1) to the secret milking machine (landmark N) a total of T times. Find the minimum possible length of the longest single trail that he will have to use, subject to the constraint that he use no trail more than once. (Note well: The goal is to minimize the length of the longest trail, not the sum of the trail lengths.)
It is guaranteed that FJ can make all T trips without reusing a trail.
Input
* Line 1: Three space-separated integers: N, P, and T
* Lines 2..P+1: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, indicating that a trail connects landmark A_i to landmark B_i with length L_i.
* Lines 2..P+1: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, indicating that a trail connects landmark A_i to landmark B_i with length L_i.
Output
* Line 1: A single integer that is the minimum possible length of the longest segment of Farmer John's route.
Sample Input
7 9 21 2 22 3 53 7 51 4 14 3 14 5 75 7 11 6 36 7 3
Sample Output
5
题意:一张无向图中有 n 个点,P 条边,每条边都有一个权值,且每条边只能用一次,要求找出 sum 条从 1 到 n 的路径,使这 sum 条路径所经过的边中,权值的最大值最小。(不是路径长度和,是路径中相邻两点的距离)
思路:二分路径最长的一段,根据二分值构图。
构图方法:
如果两点路径长度小于x,则两点之间连接一条边,权值为1(如果已经连接了,权值加1)。
最大流既是从1到n不重复的路径条数,判断是否大于规定的t条即可。
重边要当两条边来用。
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>#include <queue>#include <set>#include <map>using namespace std;const int inf=0x3f3f3f3f;int head[510],num[510],d[510],pre[510],cur[510],q[510];int n,s,t,nv,cnt,sum;int maxint=inf;struct node { int u,v,cap; int next;} edge[4000010];struct node1 { int u,v; int w;} p[4000010];void add(int u,int v,int cap){ edge[cnt].v=v; edge[cnt].cap=cap; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].cap=cap; edge[cnt].next=head[v]; head[v]=cnt++;}void bfs(){ memset(num,0,sizeof(num)); memset(d,-1,sizeof(d)); int f1=0,f2=0,i; q[f1++]=t; num[0]=1; d[t]++; while(f1>=f2) { int u=q[f2++]; for(i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(d[v]!=-1) continue; d[v]=d[u]+1; num[d[v]]++; q[f1++]=v; } }}int isap(){ memcpy(cur,head,sizeof(cur)); int flow=0, u=pre[s]=s, i; bfs(); while(d[s]<nv) { if(u==t) { int f=maxint, pos; for(i=s; i!=t; i=edge[cur[i]].v) { if(f>edge[cur[i]].cap) { f=edge[cur[i]].cap; pos=i; } } for(i=s; i!=t; i=edge[cur[i]].v) { edge[cur[i]].cap-=f; edge[cur[i]^1].cap+=f; } flow+=f; if(flow>=sum) return flow; u=pos; } for(i=cur[u]; i!=-1; i=edge[i].next) { if(d[edge[i].v]+1==d[u]&&edge[i].cap) { break; } } if(i!=-1) { cur[u]=i; pre[edge[i].v]=u; u=edge[i].v; } else { if(--num[d[u]]==0) break; int mind=nv; for(i=head[u]; i!=-1; i=edge[i].next) { if(mind>d[edge[i].v]&&edge[i].cap) { mind=d[edge[i].v]; cur[u]=i; } } d[u]=mind+1; num[d[u]]++; u=pre[u]; } } return flow;}int main(){ int n, P, i, j; while(~scanf("%d %d %d",&n,&P,&sum)) { for(i=0;i<P;i++) scanf("%d %d %d",&p[i].u,&p[i].v,&p[i].w); int low=1,mid,high=1000001; int ans=-1, x; while(low<=high) { mid=(high+low)/2; s=1; t=n; nv=t+1; cnt=0; memset(head,-1,sizeof(head)); for(i=0; i<P; i++) { if(p[i].w<=mid) { add(p[i].u,p[i].v,1); } } x=isap(); if(x>=sum) { ans=mid; high=mid-1; } else low=mid+1; } printf("%d\n",ans); } return 0;}
0 0
- POJ 2455-Secret Milking Machine(网络流_最大流+二分查找)
- poj 2455 Secret Milking Machine 最大流+二分查找
- POJ Secret Milking Machine 【网络流+二分】
- POJ 2455 Secret Milking Machine(二分答案+最大流)
- 【最大流+dinic+二分】北大 poj 2455 Secret Milking Machine
- poj - 2455 - Secret Milking Machine(最大流+二分)
- POJ 2455 Secret Milking Machine(二分+最大流)
- poj 2455 Secret Milking Machine 二分+最大流
- POJ 2455--Secret Milking Machine【二分枚举 && 最大流 && 经典】
- POJ 2455 Secret Milking Machine(二分+最大流)
- POJ 2455 Secret Milking Machine && 二分枚举 + 最大流
- POJ 2455 Secret Milking Machine 最大流 二分答案
- poj 2455 Secret Milking Machine(二分枚举+最大流)
- Poj 2455 Secret Milking Machine【二分+最大流】
- POJ 2455 Secret Milking Machine (二分答案+最大流)
- POJ 2455 — Secret Milking Machine 网络流+二分
- POJ 2455Secret Milking Machine(二分+网络流之最大流)
- POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)
- MFC中 TRUE FALSE NULL定义
- 敏敏笔试面试分享
- gerrit
- 解决ecshop退出登录会清空购物车的bug优化最完美方法
- NSBundle介绍
- POJ 2455-Secret Milking Machine(网络流_最大流+二分查找)
- 通过新浪IP服务器获得的当前客户端IP地址对应的国家、省份或直辖市、城市信息
- 物聯網WiFi
- Content Provider的启动过程源代码分析
- jQuery学习笔记-1
- MAT 内存使用检测分析工具
- 杭电第一周1004 绝对值排序
- android打包时出现Conversion to Dalvik format failed with error 1错误
- 由于设计页面需要,要把两个并排显示的 div 实现一样高的效果, n 行 n 列布局,每列高度(事先并不能确定哪列的高度)相同,每个设计师追求的目标。方法有以下几种: 1 JS 实现(判断 2 个 d