POJ 2135(Farm Tour-费用流)[Template:费用流 V2]

Language: Default
Farm Tour
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11916   Accepted: 4454

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

Source

USACO 2003 February Green

直接求从S到T,容量为2的最小费用流。




#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (1000+10)
#define MAXM (10000*4+10)
#define MAXAi (35000)
#define eps (1e-3)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;

class Cost_Flow  
{  
public:  
    int n,s,t;  
    int q[10000];  
    int edge[MAXM],next[MAXM],pre[MAXN],weight[MAXM],size;  
    double cost[MAXM];  
    void addedge(int u,int v,int w,double c)    
    {    
        edge[++size]=v;    
        weight[size]=w;    
        cost[size]=c;    
        next[size]=pre[u];    
        pre[u]=size;    
    }    
    void addedge2(int u,int v,int w,double c){addedge(u,v,w,c),addedge(v,u,0,-c);}   
    bool b[MAXN];  
    double d[MAXN];  
    int pr[MAXN],ed[MAXN];  
    bool SPFA(int s,int t)    
    {    
        For(i,n) d[i]=INF;  
        MEM(b)  
        d[q[1]=s]=0;b[s]=1;    
        int head=1,tail=1;    
        while (head<=tail)    
        {    
            int now=q[head++];    
            Forp(now)    
            {    
                int &v=edge[p];    
                if (weight[p]&&d[now]+cost[p]<d[v])    
                {    
                    d[v]=d[now]+cost[p];    
                    if (!b[v]) b[v]=1,q[++tail]=v;    
                    pr[v]=now,ed[v]=p;    
                }    
            }    
            b[now]=0;    
        }    
        return fabs(d[t]-INF)>eps;    
    }   
    double totcost;    
          
    double CostFlow(int s,int t)    
    {    
        while (SPFA(s,t))    
        {    
            int flow=INF;    
            for(int x=t;x^s;x=pr[x]) flow=min(flow,weight[ed[x]]);      
            totcost+=(double)flow*d[t];    
            for(int x=t;x^s;x=pr[x]) weight[ed[x]]-=flow,weight[ed[x]^1]+=flow;         
        }    
        return totcost;    
    }    
    void mem(int n,int t)  
    {  
        (*this).n=n;  
        size=1;  
        totcost=0;  
        MEM(pre) MEM(next)   
    }  
}S;  
int n,m;
int main()
{
//	freopen("poj2135.in","r",stdin);
//	freopen(".out","w",stdout);
	cin>>n>>m; 
	S.mem(n+1,n);
	int s=n+1;
	S.addedge2(s,1,2,0);
	For(i,m)
	{
		int u,v,c;
		scanf("%d%d%d",&u,&v,&c);
		S.addedge2(u,v,1,c);
		S.addedge2(v,u,1,c);
	}
	
	cout<<(int)round(S.CostFlow(n+1,n))<<endl;
	
	return 0;
}



你可能感兴趣的:(POJ 2135(Farm Tour-费用流)[Template:费用流 V2])