HDU1853 Cyclic Tour(有向环最小覆盖)

题意:给你一个N个点M条边的带权有向图,现在要你求这样一个值:

          该有向图中的所有顶点正好被1个或多个不相交的有向环覆盖(每个节点只能被一个有向环包含).这个值就是 所有这些有向环的权值和. 要求该值越小越好.

思路:有向环的最小覆盖问题,首先考虑该图中所有顶点正好被1个或多个不相交的环覆盖的话,就意味着图中的每个顶点出度和入度均为1,那么对于每个顶点就可以把它拆成两个点,来模拟出度和入度,源点就相当于总出度,汇点就相当于总入度,那么如果最大流等于顶点数目,那么就可以说明刚好被1个或多个不相交环覆盖,那么最小费用就是答案了。

建图:源点s编号0, 所有节点编号1-n和n+1-2*n, 汇点t编号2*n+1.

           源点s到第i个点有边 ( s, i, 1, 0)

           如果从i点到j点有权值为w的边,那么有边 (i, j+n, 1, w)

           每个节点到汇点有边 (i+n, t, 1, 0)

           最终如果最大流==n的话,那么最小费用就是我们所求. 否则输-1.

这题搜了网上的题解发现还可以用二分匹配来做,以后学了再做。


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

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];        //Bellman_ford单源最短路径
    int p[maxn];        //p[i]表从s到i的最小费用路径上的最后一条弧编号
    int a[maxn];        //a[i]表示从s到i的最小残量

    //初始化
    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)
    {
        for(int i=0;i<n;++i) d[i]=INF;
        memset(inq,0,sizeof(inq));
        d[s]=0, a[s]=INF, inq[s]=true, p[s]=0;
        queue<int> Q;
        Q.push(s);
        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;
                    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]=true; }
                }
            }
        }
        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 Min_cost(int num)
    {
        int flow=0,cost=0;
        while(BellmanFord(flow,cost));
        return num==flow?cost:-1;
    }
}mc;

int main()
{
	int n,m;
    while (scanf("%d%d",&n,&m)!=EOF)
	{	
        mc.init(n*2+2,0,n*2+1);
		for (int i=1;i<=n;i++)
		{
			mc.AddEdge(0,i,1,0);
			mc.AddEdge(n+i,n*2+1,1,0);
		}
		for (int i = 1;i<=m;i++)
		{
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			mc.AddEdge(u,v+n,1,w);
		}
		printf("%d\n",mc.Min_cost(n));
	}
} 


Description

There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total length of all the tours minimum, but he is too lazy to calculate. Can you help him? 
 

Input

There are several test cases in the input. You should process to the end of file (EOF). 
The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000). 
 

Output

Output one number for each test case, indicating the minimum length of all the tours. If there are no such tours, output -1. 
 

Sample Input

        
        
        
        
6 9 1 2 5 2 3 5 3 1 10 3 4 12 4 1 8 4 6 11 5 4 7 5 6 9 6 5 4 6 5 1 2 1 2 3 1 3 4 1 4 5 1 5 6 1
 

Sample Output

        
        
        
        
42 -1

Hint

 In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42. 
         
 


你可能感兴趣的:(HDU1853 Cyclic Tour(有向环最小覆盖))