POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)

深度探讨网络流算法在远距离路径规划中的应用
本文详细介绍了如何运用网络流算法解决从起点到终点的多路径规划问题,确保路径中每段路程长度最优化,同时避免重复使用同一路段。通过实例分析,展示了算法在复杂农场环境下的实际应用,旨在最小化最长路径长度,提高效率并降低被发现的风险。

Secret Milking Machine
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9658 Accepted: 2859

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



题目大意:

FJ有N块地,这些地之间有P条双向路,每条路的都有固定的长度l。现在要你找出从第1块地到第n块地的T条不同路径,每条路径上的路不能与先前的路径重复,问这些路径中的最长路的最小是多少。


解题思路:

二分+网络流。


解题代码:


代码一:DINIC算法

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

const int INF=(1<<30);
const int maxn=210,maxm=201000;

struct edge{
    int u,v,f,next;
    edge(int u0=0,int v0=0,int f0=0){
        u=u0;v=v0;f=f0;
    }
}e[maxm];

int src,sink,cnt,head[maxn];

void adde(int u,int v,int f){
    e[cnt].u=u,e[cnt].v=v,e[cnt].f=f,e[cnt].next=head[u],head[u]=cnt++;
    e[cnt].u=v,e[cnt].v=u,e[cnt].f=0,e[cnt].next=head[v],head[v]=cnt++;
}

void init(){
    cnt=0;
    memset(head,-1,sizeof(head));
}

queue <int> q;
bool visited[maxn];
int dist[maxn];

void bfs(){
    memset(dist,0,sizeof(dist));
    while(!q.empty()) q.pop();
    visited[src]=true;
    q.push(src);
    while(!q.empty()){
        int s=q.front();
        q.pop();
        for(int i=head[s];i!=-1;i=e[i].next){
            int d=e[i].v;
            if(e[i].f>0 && !visited[d]){
                q.push(d);
                dist[d]=dist[s]+1;
                visited[d]=true;
            }
        }
    }
}

int dfs(int u,int delta){
    if(u==sink) return delta;
    else{
        int ret=0;
        for(int i=head[u];delta && i!=-1;i=e[i].next){
            if(e[i].f>0 && dist[e[i].v]==dist[u]+1){
                int d=dfs(e[i].v,min(e[i].f,delta));
                e[i].f-=d;
                e[i^1].f+=d;
                delta-=d;
                ret+=d;
            }
        }
        if(!ret) dist[u]=-2;
        return ret;
    }
}

int maxflow(){
    int ret=0;
    while(true){
        memset(visited,false,sizeof(visited));
        bfs();
        if(!visited[sink]) return ret;
        ret+=dfs(src,INF);
    }
    return ret;
}

int n,m,num,maxr,minr;
vector <edge> vec;

void input(){
    maxr=0;
    minr=INF;
    src=1,sink=n;
    vec.clear();
    int u,v,w;
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&u,&v,&w);
        vec.push_back(edge(u,v,w));
        vec.push_back(edge(v,u,w));
        if(w>maxr) maxr=w;
        if(w<minr) minr=w;
    }
}

void build(int dis0){
    init();
    for(int i=0;i<vec.size();i++){
        if(vec[i].f<=dis0){
            adde(vec[i].u,vec[i].v,1);
        }
    }
}

void solve(){
    int l=minr,r=maxr;
    while(l<r){
        int mid=(l+r)/2;
        build(mid);
        if(maxflow()>=num) r=mid;
        else l=mid+1;
    }
    printf("%d\n",r);
}

int main(){
    while(scanf("%d%d%d",&n,&m,&num)!=EOF){
        input();
        solve();
    }
    return 0;
}


代码二:sap算法

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

#define INF 2000000000
#define N 100010
typedef long long ll;

const int maxn=210;
struct edge{
    int u,v,next,cap;
    edge(int u0=0,int v0=0,int f0=0){
        u=u0;v=v0;cap=f0;
    }
}e[210000];

int n,head[N],tol,top,st[N];
int src,des,dep[N],gap[N];

void adde(int u,int v,int c){
    e[tol].u=u,e[tol].v=v,e[tol].next=head[u],e[tol].cap=c,head[u]=tol++;
    e[tol].u=v,e[tol].v=u,e[tol].next=head[v],e[tol].cap=0,head[v]=tol++;
}

void bfs(){//对于反边计算层次
    for(int i=0;i<N;i++) dep[i]=N-1;
    memset(gap,0,sizeof gap);
    gap[0]=1,dep[des]=0;
    int q[N],l=0,r=0,u,v;
    q[r++]=des;
    while(l!=r){
        u=q[l++];
        l=l%N;
        for(int i=head[u];i!=-1;i=e[i].next){
            v=e[i].v;
            if(e[i].cap!=0||dep[v]!=N-1) continue;
            q[r++]=v;
            r=r%N;
            ++gap[dep[v]=dep[u]+1];
        }
    }
}

int sap(){
    bfs();
    int u=src,s[N],top=0,res=0,ii;
    int cur[N];
    memcpy(cur,head,sizeof head);
    while(dep[src]<n){
        if(u==des){//求得一条增广路
           int minf=INF,pos=n;
           for(int i=0;i<top;i++){
              if(minf>e[s[i]].cap){
                  minf=e[s[i]].cap;
                  pos=i;
              }
           }
           for(int i=0;i<top;i++){
              e[s[i]].cap-=minf;
              e[s[i]^1].cap+=minf;
           }
           top=pos;
           res+=minf;
           u=e[s[top]].u;//优化1
        }
        if(dep[u]!=0&&gap[dep[u]-1]==0) break;//出现断层
        ii=-1;
        for(int i=cur[u];i!=-1;i=e[i].next){
             if(dep[e[i].v]==N-1) continue;
             if(e[i].cap!=0&&dep[u]==dep[e[i].v]+1){ii=i;break;}
        }
        if(ii!=-1){//有允许弧
            cur[u]=ii;
            s[top++]=ii;
            u=e[ii].v;
        }else{//不断回退找增光路
            int mind=n;
            for(int i=head[u];i!=-1;i=e[i].next){
                if(e[i].cap==0) continue;
                if(dep[e[i].v]<mind) mind=dep[e[i].v],cur[u]=i;
            }
            --gap[dep[u]];
            ++gap[dep[u]=mind+1];//优化2
            if(u!=src) u=e[s[--top]].u;
        }
    }
    return res;
}

int m,num,maxr,minr;
vector <edge> vec;

void input(){
    maxr=0;
    minr=INF;
    vec.clear();
    int u,v,w;
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&u,&v,&w);
        vec.push_back(edge(u,v,w));
        vec.push_back(edge(v,u,w));
        if(w>maxr) maxr=w;
        if(w<minr) minr=w;
    }
}

void build(int dis0){
    tol=0;
    memset(head,-1,sizeof head);
    src=1,des=n,n;
    int vsize=vec.size();
    for(int i=0;i<vsize;i++){
        if(vec[i].cap<=dis0) adde(vec[i].u,vec[i].v,1);
    }
}

void solve(){
    int l=minr,r=maxr;
    while(l<r){
        int mid=(l+r)/2;
        build(mid);
        if(sap()>=num) r=mid;
        else l=mid+1;
    }
    printf("%d\n",r);
}

int main(){
    while(scanf("%d%d%d",&n,&m,&num)!=EOF){
        input();
        solve();
    }
    return 0;
}


版权声明:欢迎关注我的博客,本文为博主toyking原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/toyking/p/4951010.html

乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值