Farm Tour (minimum cost maximum flow)

Description

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
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;

const int inf=0x3f3f3f3f;
const int maxn=1010;

struct Edge{
    int from,to,cap,flow,cost;
    Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w){}
};

/*
Being able to abstract the picture is the core
*/

struct MCMF{
    int n,m;
    vector<Edge> edge;
    vector<int> G[maxn];
    int inq[maxn];
    int d[maxn];
    int p[maxn];
    int a[maxn];

    void init(int n){
        this->n=n;
        for(int i=0;i<n;i++) G[i].clear();
        edge.clear();
    }
    void add_edge(int u,int v,int cap,int cost){
        edge.push_back(Edge(u,v,cap,0,cost));
        edge.push_back(Edge(v,u,0,0,-cost));
        m=edge.size();
        G[u].push_back(m-2);
        G[v].push_back(m-1);
    }

    bool spfa(int s,int t,int& flow,ll &cost){
        for(int i=0;i<n;i++) d[i]=inf;
        memset(inq,0,sizeof(inq));
        d[s]=0;inq[s]=1;p[s]=0;a[s]=inf;
        queue<int> q;
        q.push(s);
        while(!q.empty()){
            int u=q.front();
            q.pop();
            inq[u]=0;
            int sz=G[u].size();
            for(int i=0;i<sz;i++){
                Edge e=edge[G[u][i]];
                if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){
                    d[e.to]=d[u]+e.cost;
                    p[e.to]=G[u][i];
                    a[e.to]=min(a[u],e.cap-e.flow);
                    if(!inq[e.to]){
                        q.push(e.to);inq[e.to]=1;
                    }
                }
            }
        }
        if(d[t]==inf)return false;
        flow+=a[t];
        cost+=(ll)d[t]*(ll)a[t];
        for(int u=t;u!=s;u=edge[p[u]].from){
            edge[p[u]].flow+=a[t];
            edge[p[u]^1].flow-=a[t];
        }
        return true;
    }

    void MM(int s,int t,ll& cost){
        int flow=0;cost=0;
        while(spfa(s,t,flow,cost));
    }
}mm;

int main()
{
    int n,m;
    scanf("%d %d",&n,&m);
    int st=0,ed=n+1;//建立超级源点,超级汇点
    mm.init(n+2);
    for(int i=1;i<=m;i++){
        int u,v,w;
        scanf("%d %d %d",&u,&v,&w);
        mm.add_edge(u,v,1,w);
        mm.add_edge(v,u,1,w);
    }
    mm.add_edge(st,1,2,0);
    mm.add_edge(n,ed,2,0);
    ll ans;
    mm.MM(st,ed,ans);
    printf("%I64d\n",ans);
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325823248&siteId=291194637