【POJ - 245】Secret Milking Machine 【网络流+二分】

本文探讨了在一个无向图中寻找从起点到终点的多条不同路径的问题,旨在使这些路径上的最大边权尽可能小。通过使用二分查找与最大流算法相结合的方法,有效地解决了该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值