[POJ 2135] Farm Tour

[题目链接]

         http://poj.org/problem?id=2135

[算法]

        费用流

[代码]

       

#include <algorithm>  
#include <bitset>  
#include <cctype>  
#include <cerrno>  
#include <clocale>  
#include <cmath>  
#include <complex>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <ctime>  
#include <deque>  
#include <exception>  
#include <fstream>  
#include <functional>  
#include <limits>  
#include <list>  
#include <map>  
#include <iomanip>  
#include <ios>  
#include <iosfwd>  
#include <iostream>  
#include <istream>  
#include <ostream>  
#include <queue>  
#include <set>  
#include <sstream>  
#include <stdexcept>  
#include <streambuf>  
#include <string>  
#include <utility>  
#include <vector>  
#include <cwchar>  
#include <cwctype>  
#include <stack>  
#include <limits.h>
using namespace std;
#define MAXN 5010
#define MAXM 20010
const int inf = 2e9;

int i,n,m,u,v,w,S,T,ans,tot;
int head[MAXN],pre[MAXN],incf[MAXN],dist[MAXN];

struct edge
{
        int to,w,cost,nxt;
} e[MAXM << 1];

inline void addedge(int u,int v,int w,int cost)
{
        tot++;
        e[tot] = (edge){v,w,cost,head[u]};
        head[u] = tot;
        tot++;
        e[tot] = (edge){u,0,-cost,head[v]};
        head[v] = tot;
}
inline bool spfa()
{
        int i,l,r,u,v,w,cost;
        static int q[MAXN];
        static bool inq[MAXN];
        for (i = 1; i <= T; i++) dist[i] = inf;
        memset(inq,false,sizeof(inq));
        q[l = r = 1] = S;
        inq[S] = true;
        pre[S] = 0;
        dist[S] = 0;
        incf[S] = inf;
        while (l <= r)
        {
                u = q[l];
                l++;
                inq[u] = false;
                for (i = head[u]; i; i = e[i].nxt)
                {
                        v = e[i].to;
                        w = e[i].w;
                        cost = e[i].cost;
                        if (w && dist[u] + cost < dist[v])
                        {
                                dist[v] = dist[u] + cost;
                                incf[v] = min(incf[u],w);
                                pre[v] = i;
                                if (!inq[v])
                                {
                                        inq[v] = true;
                                        q[++r] = v;
                                }        
                        }        
                }        
        }        
        if (dist[T] != inf) return true;
        else return false;
} 
inline void update()
{
        int x = T,pos;
        while (x != S)
        {
                pos = pre[x];
                e[pos].w -= incf[T];
                e[pos ^ 1].w += incf[T];
                x = e[pos ^ 1].to; 
        }
        ans += dist[T] * incf[T];
}
int main() 
{
        
        scanf("%d%d",&n,&m);
        tot = 1;
        for (i = 1; i <= m; i++)
        {
                scanf("%d%d%d",&u,&v,&w);
                addedge(u,v,1,w);
                addedge(v,u,1,w);
        }
        S = n + 1;
        T = n + 2;
        addedge(S,1,2,0);
        addedge(n,T,2,0);
        while (spfa()) update();
        printf("%d\n",ans);
        
        return 0;
    
}

猜你喜欢

转载自www.cnblogs.com/evenbao/p/9426229.html