Farm Tour (最小费用最大流)

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.
Input
* 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.
Output
A single line containing the length of the shortest tour.
Sample Input
4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2
Sample Output
6
题意:FJ要带他的盆友从自己的房子(标号为1)(起点)去到一个大粮仓(标号为n)(终点),总共有n个点,然后现在要求从1走到n然后再走回1,要求一条边只能走一次。

题解:这个题目要做一下转变,将到达终点折回看成两次从起点出发到达终点,然后建图,取一个超级汇点和一个超级源点。然后连接原来存在的边,将其路长做为费用,每条边流量为1。然后跑最小费用最大流,保证流量为2(即来是1,去是1)。就得到所要求的答案了。

代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <cstring>
#include <vector>
#include <bitset>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn = 1e6+7;
const int INF = 0x3f3f3f3f;
int n,m;
struct edge{
    int to,cap,cost,rev;
};
vector<edge> g[1006];

int d[1005];
int prevv[1005],preve[1005];
void add_line(int a,int b,int cost){
    g[a].push_back((edge){b,1,cost,g[b].size()});
    g[b].push_back((edge){a,1,cost,g[a].size()-1});
}

int solve(){
    int res = 0;
    int flow = 2;
    while(flow>0){
    int update = 1;
    for(int i = 0;i <= n;i++){
        d[i] = INF;
    }
    d[1] = 0;
    while (update){
        update = 0;
        for(int i = 1;i <= n;i++){
            if(d[i]==INF) continue;
            for(int j = 0;j < g[i].size();j++){
                edge &e = g[i][j];
                if(e.cap>0&&d[e.to]>d[i]+e.cost){
                    d[e.to] = d[i]+e.cost;
                    prevv[e.to] = i;
                    preve[e.to] = j;
                    update = 1;
                }
            }
        }
    }
    if(d[n]==INF) return -1;
    int q = flow;
    for(int i = n;i!=1;i = prevv[i]){
        q = min(q,g[prevv[i]][preve[i]].cap);
    }
    flow-=q;
    res+=d[n];
    for(int i = n;i != 1;i = prevv[i]){
        edge &e = g[prevv[i]][preve[i]];
        e.cap-=q;
        g[e.to][e.rev].cost = -e.cost;
    }
    }
    return res;
}
int main()
{
    while (scanf("%d %d",&n,&m)!=EOF){
        for(int i = 0;i < m;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            add_line(a,b,c);
        }
        cout<<solve()<<endl;
    }
    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值