【POJ

来源:互联网 发布:mac双系统时间不一致 编辑:程序博客网 时间:2024/05/31 19:35

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.
题意 :n个点m条边的无向图,从点1到点n 的不同路线最少为t条时候,现在想让所有路径中的最大边权最小。

分析: 最小化最大值,二分没跑,然后这种不同路线数的题,可以转化为最大流,而在网络流中 常用的二分操作,就两种,一个是将一些边权变为mid,然后枚举。还有一种就是将小于mid的边添加进新图中。显然这个题目就是第二种,我们二分枚举所有路径中的边权最大值,如果最后最大路径数>=t这个时候我们减少ri,否则减少le。

代码

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int MAXN =  1000+11;const int MAXM =  1600000+10 ;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;struct Edge {    int form,to,cap,flow,nexts;}edge[MAXM];struct Node{    int from,to,val;}node[MAXM];int head[MAXN],top;void init(){    memset(head,-1,sizeof(head));    top=0;}void addedge(int a,int b,int c){    Edge e={a,b,c,0,head[a]};    edge[top]=e;head[a]=top++;    Edge ee={b,a,0,0,head[b]};    edge[top]=ee;head[b]=top++;}int n,m,t;int S,T;int MAX;void getmap(int mid){    S=1,T=n;    for(int i=0;i<m;i++){        if(node[i].val<=mid){             // 容量都设为1。            // 容量为1,这样跑下来的最大流就是最小割的容量,同时也是隔断的数目 即不同的路径数目。              addedge(node[i].from,node[i].to,1);             addedge(node[i].to,node[i].from,1);         }     }  } int vis[MAXN],dis[MAXN];int cur[MAXN];bool bfs(int st,int ed){    queue<int>Q;    memset(vis,0,sizeof(vis));    memset(dis,-1,sizeof(dis));    Q.push(st);vis[st]=1;dis[st]=1;    while(!Q.empty()){        int now=Q.front();Q.pop();        for(int i=head[now];i!=-1;i=edge[i].nexts){            Edge e=edge[i];            if(!vis[e.to]&&e.cap-e.flow>0){                vis[e.to]=1;                dis[e.to]=dis[now]+1;                if(e.to==ed) return 1;                Q.push(e.to);            }        }    }    return 0;}int dfs(int now,int a,int ed){    if(a==0||now==ed) return a;    int flow=0,f;    for(int &i=cur[now];i!=-1;i=edge[i].nexts){        Edge &e=edge[i];        if(dis[e.to]==dis[now]+1&&(f=dfs(e.to,min(e.cap-e.flow,a),ed))>0){            e.flow+=f;            flow+=f;            edge[i^1].flow-=f;            a-=f;            if(a==0) break;        }     }    return flow;}int max_flow(int st ,int ed){    int flow=0;    while(bfs(st,ed)){        memcpy(cur,head,sizeof(head));        flow+=dfs(st,inf,ed);    }    return flow;}void solve(){    int le=0,ri=MAX;  // 以后一定好好弄上界     int ans=MAX;     while(le<=ri){        init();        int mid=(le+ri)>>1;        getmap(mid);        if(max_flow(S,T)>=t) {            ri=mid-1;ans=mid;        }else le=mid+1;    }      printf("%d\n",ans);}int main(){      while(scanf("%d%d%d",&n,&m,&t)!=EOF){        int a,b,c; MAX=0;         for(int i=0;i<m;i++) {            scanf("%d%d%d",&node[i].from,&node[i].to,&node[i].val);            MAX=max(MAX,node[i].val);        }        solve();    }     return 0;}
原创粉丝点击