HDU 3488 Tour(最小费用流:有向环覆盖)

HDU 3488 Tour(最小费用流:有向环覆盖)

http://acm.hdu.edu.cn/showproblem.php?pid=3488

题意:

       给你一个N个顶点M条边的带权有向图,要你把该图分成1个或多个不相交的有向环.且所有定点都只被一个有向环覆盖.问你该有向环所有权值的总和最小是多少?(保证有解)

分析:

       之前本题我的是二分图最优匹配做的,现在用费用流来做.

       建图如下:

       源点s编号0, 图中原始节点拆点成i和i+n(编号1到n和编号n+1到2*n) , 汇点t编号2*n+1.

       如果原图i节点到j节点有权值w的边,那么新图有边 (i, j+n, 1, w)

       且源点s到任意节点i有边 (s, i, 1, 0)

       且任意节点i到汇点t有边 (i+n,t,1,0)

       如果最大流==n,那么最小费用即是解.

       关于上面解法的论证,请见HDU1853:

http://blog.csdn.net/u013480600/article/details/39159407

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#define INF 1e9
using namespace std;
const int maxn=400+5;

struct Edge
{
    int from,to,cap,flow,cost;
    Edge(){}
    Edge(int f,int t,int c,int fl,int co):from(f),to(t),cap(c),flow(fl),cost(co){}
};

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

    void init(int n,int s,int t)
    {
        this->n=n, this->s=s, this->t=t;
        edges.clear();
        for(int i=0;i<n;++i) G[i].clear();
    }

    void AddEdge(int from,int to,int cap,int cost)
    {
        edges.push_back(Edge(from,to,cap,0,cost));
        edges.push_back(Edge(to,from,0,0,-cost));
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool BellmanFord(int &flow,int &cost)
    {
        queue<int> Q;
        for(int i=0;i<n;++i) d[i]=INF;
        memset(inq,0,sizeof(inq));
        Q.push(s),inq[s]=true,d[s]=0,a[s]=INF,p[s]=0;

        while(!Q.empty())
        {
            int u=Q.front(); Q.pop();
            inq[u]=false;
            for(int i=0;i<G[u].size();++i)
            {
                Edge &e=edges[G[u][i]];
                if(e.cap>e.flow && d[e.to]>d[u]+e.cost)
                {
                    d[e.to]=d[u]+e.cost;
                    a[e.to]=min(a[u],e.cap-e.flow);
                    p[e.to]=G[u][i];
                    if(!inq[e.to]){inq[e.to]=true; Q.push(e.to);}
                }
            }
        }
        if(d[t]==INF) return false;
        flow += a[t];
        cost += a[t]*d[t];
        int u=t;
        while(u!=s)
        {
            edges[p[u]].flow +=a[t];
            edges[p[u]^1].flow -=a[t];
            u=edges[p[u]].from;
        }
        return true;
    }

    int solve(int num)
    {
        int flow=0,cost=0;
        while(BellmanFord(flow,cost));
        return flow==num?cost:-1;
    }
}MM;

int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        int src=0,dst=2*n+1;
        MM.init(2*n+2,src,dst);
        for(int i=1;i<=n;++i)
        {
            MM.AddEdge(src,i,1,0);
            MM.AddEdge(i+n,dst,1,0);
        }
        while(m--)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            MM.AddEdge(u,v+n,1,w);
        }
        printf("%d\n",MM.solve(n));
    }
    return 0;
}

你可能感兴趣的:(Algorithm,算法,ACM,poj,网络流)