poj 2455 Secret Milking Machine

来源:互联网 发布:微屏软件 排行 编辑:程序博客网 时间:2024/05/20 21:20

(http://www.elijahqi.win/2017/12/25/poj2455-secret-milking-machine/)
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.

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.

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 2
1 2 2
2 3 5
3 7 5
1 4 1
4 3 1
4 5 7
5 7 1
1 6 3
6 7 3

Sample Output

5

Hint
Farmer John can travel trails 1 - 2 - 3 - 7 and 1 - 6 - 7. None of the trails travelled exceeds 5 units in length. It is impossible for Farmer John to travel from 1 to 7 twice without using at least one trail of length 5.

Huge input data,scanf is recommended.

Source
USACO 2005 February Gold
二分一下 我路径上最大的值是多少 然后每次只把<=这个权值的边建出来 然后跑权值为1的最大流 看这样的路径一共有几条 输出即可
注意这题有个坑点 就是路径条数>=t就可以 所以判定的时候要注意点

#include<queue>#include<cstdio>#include<cstring>#include<algorithm>#define N 220#define inf 0x3f3f3f3fusing namespace std;inline char gc(){    static char now[1<<16],*S,*T;    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}    return *S++;}inline int read(){    int x=0;char ch=gc();    while(ch<'0'||ch>'9') ch=gc();    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}    return x; }struct node{    int x,y,z,next;}data1[88000],data[88000];inline bool cmp(node a,node b){return a.z<b.z;}int n,p,t,num=1,h[N],level[N];inline void insert1(int x,int y,int z){    data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;    data[++num].y=x;data[num].z=z;data[num].next=h[y];h[y]=num;}inline bool bfs(){    memset(level,0,sizeof(level));queue<int>q;level[1]=1;q.push(1);    while(!q.empty()){        int x=q.front();q.pop();        for (int i=h[x];i;i=data[i].next){            int y=data[i].y,z=data[i].z;            if (level[y]||!z) continue;level[y]=level[x]+1;q.push(y);if (y==n) return 1;        }    }return 0;}inline int dfs(int x,int s){    if (x==n) return s;int ss=s;    for (int i=h[x];i;i=data[i].next){        int y=data[i].y,z=data[i].z;        if (level[x]+1==level[y]&&z){            int xx=dfs(y,min(s,z));if (!xx) level[y]=0;            s-=xx;data[i].z-=xx;data[i^1].z+=xx;if (!s) return ss;         }    }return ss-s;}inline bool check(int mid){    memset(h,0,sizeof(h));num=1;    for (int i=1;i<=p;++i){        if (data1[i].z>mid) break;        insert1(data1[i].x,data1[i].y,1);    }int ans=0;while(bfs()) ans+=dfs(1,inf);if (ans>=t) return 1;else return 0;}int main(){    freopen("poj2455.in","r",stdin);    n=read();p=read();t=read();int max1=0;    for (int i=1;i<=p;++i) data1[i].x=read(),data1[i].y=read(),data1[i].z=read(),max1=max(max1,data1[i].z);    sort(data1+1,data1+p+1,cmp);int l=1,r=max1;    while(l<=r){        int mid=l+r>>1;        if (check(mid)) r=mid-1;else l=mid+1;    }    printf("%d",l);    return 0;}
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 静电 静电消除 衣服静电 静电贴 防静电 消除静电 静电计 静电场 静电现象 静电手环 静电危害 怎么除静电 去静电 静电特点 去除静电 防静电方法 裙子静电 人体静电 静电的危害 怎么去静电 静电图片 什么是静电 静电球 除静电设备 静电电压 静电的产生 除静电 静电贴价格 防静电剂 如何防静电 防静电产品 静电带 防静电胶带 防静电箱 防静电接地 静电放电 静电喷枪 衣服有静电 静电粉末 静电喷雾器 静电纺丝机