Farm Tour(费用流模板题)

本文介绍了一个关于寻找农场中从家到谷仓再返回的最短路径问题,并提供了一种解决该问题的具体算法实现。该问题涉及图论中的路径搜索与优化,通过建立图模型并利用特定算法求解最短路径。

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

题目描述

When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M <= 10000) paths that connect the fields in various ways. Each path connects two different fields and has a nonzero length smaller than 35,000.

To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. Later, he returns (potentially through some fields) back to his house again.

He wants his tour to be as short as possible, however he doesn't want to walk on any given path more than once. Calculate the shortest tour possible. FJ is sure that some tour exists for any given farm.

输入

* Line 1: Two space-separated integers: N and M.

* Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length.

输出

A single line containing the length of the shortest tour.

 

样例输入

4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

样例输出

6
#include<cstdio>  
#include<cstring>  
struct edge{  
    int to,cap,rev,nx,we;  
}G[40050];  
int n,m,p;  
int h[2050],q[40050],d[2050];  
bool mark[2050];  
int Min(int a,int b){return a<b?a:b;}  
void ae(int s,int e,int c,int w){  
    G[++p]=(edge){e,c,p+1,h[s],w};h[s]=p;  
    G[++p]=(edge){s,0,p-1,h[e],-w};h[e]=p;  
}  
bool spfa(){  
    memset(d,127/3,sizeof(d));  
    memset(mark,0,sizeof(mark));  
    d[n+1]=0;mark[n+1]=1;  
    int head=0,tail=0;  
    int inf=d[0];  
    q[tail++]=n+1;  
    while(head!=tail){  
        int fr=q[head++];  
        for(int i=h[fr];i;i=G[i].nx){  
            if(G[G[i].rev].cap>0&&d[G[i].to]>d[fr]+G[G[i].rev].we){  
                d[G[i].to]=d[fr]+G[G[i].rev].we;  
                if(!mark[G[i].to])mark[G[i].to]=1,q[tail++]=G[i].to;  
            }  
        }  
        mark[fr]=0;  
    }  
    return !(d[0]==inf);  
}  
int dfs(int s,int t,int f){  
    mark[s]=1;  
    if(s==t) return f;  
    int sum=0;  
    for(int i=h[s];i;i=G[i].nx){  
        if(G[i].cap>0&&!mark[G[i].to]&&d[s]-G[i].we==d[G[i].to]){  
            int d=dfs(G[i].to,t,Min(G[i].cap,f));  
            if(d)sum+=d,f-=d,G[i].cap-=d,G[G[i].rev].cap+=d;  
            if(f==0)return sum;  
        }  
    }  
    return sum;  
}  
int m__f(){  
    int sum=0;  
    while(spfa()){  
        mark[n+1]=1;  
        while(mark[n+1]){  
            memset(mark,0,sizeof(mark));  
            sum+=dfs(0,n+1,99999999)*d[0];  
        }  
    }  
    return sum;  
}  
int main(){  
    scanf("%d%d",&n,&m);  
    for(int i=1;i<=m;i++){  
        int x,y,z;  
        scanf("%d%d%d",&x,&y,&z);  
        ae(x,y,1,z);ae(y,x,1,z);  
    }  
    ae(0,1,2,0);ae(n,n+1,2,0);  
    printf("%d\n",m__f());  
    return 0;  
}  

 

转载于:https://www.cnblogs.com/nzher/p/6581005.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值