HDU 3488 Tour(有向环最小权覆盖问题)

Tour

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 4374    Accepted Submission(s): 2088


 

Problem Description

In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one-way roads connecting them. You are lucky enough to have a chance to have a tour in the kingdom. The route should be designed as: The route should contain one or more loops. (A loop is a route like: A->B->……->P->A.)
Every city should be just in one route.
A loop should have at least two cities. In one route, each city should be visited just once. (The only exception is that the first and the last city should be the same and this city is visited twice.)
The total distance the N roads you have chosen should be minimized.

 

 

Input

An integer T in the first line indicates the number of the test cases.
In each test case, the first line contains two integers N and M, indicating the number of the cities and the one-way roads. Then M lines followed, each line has three integers U, V and W (0 < W <= 10000), indicating that there is a road from U to V, with the distance of W.
It is guaranteed that at least one valid arrangement of the tour is existed.
A blank line is followed after each test case.

 

 

Output

For each test case, output a line with exactly one integer, which is the minimum total distance.

 

 

Sample Input

 

1 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

 

 

Sample Output

 

42

题意:

给出n(n<=200)个点m(m<=30000)条单向边以及每条边的费用,让你求出走过一个哈密顿环(除起点外,每个点只能走一次)的最小费用。

解析:

经典的有向环最小权覆盖问题,用费用流来解。

由于题目中要求每个点最多走一次,为了防止走多次的发生,我们要把每个点 i 拆成左点i 和 右点i + n两个点。

如果从 i 点到 j 点有权值为 c 的边,那么有边  (i,  j+n,  1,  c),即左点和右点建边, 确保了每个点只走一次。

源点S到第i个点有边 (S, i, 1, 0),即源点和左点建边。

每个节点到汇点有边 (i+n, T,  1,  0), 即右点和汇点建边。

我们可以设S=0,T=2*n+1。

如果求出的最大流为n,则最小费用就是答案,否则答案不存在(即不存在满足条件的环)。

为什么这样建边,见饶齐的经典讲解:传送门

本题的边比较多,而且经过TLE检测,本题有重边。由于点比较少,我们先用邻接矩阵保存最小费用再进行建边。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn =405;
#define inf 1e9
#define eps 0
struct Edge
{
    int from,to,cap,flow;
	int 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 add(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]=1,p[s]=0;
		queue<int>q;
		q.push(s);
		while(!q.empty())
		{
		    int u = q.front();
			q.pop();
			inq[u]=0;
			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+eps)
				{
				    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+=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 work(int &flow,int &cost)
	{
	    flow=0,cost=0;
		while(BellmanFord(flow,cost));
		return cost;
	}
}mm;
int a[maxn][maxn];
int main(){
    int Tc,cas=1,S,T,n,m;
    int cst;
    scanf("%d",&Tc);
    while(Tc--)
    {
        scanf("%d%d",&n,&m);
        S=0;T=2*n+1;
        mm.init(2*n+1,S,T);
        for(int i=1;i<=2*n+1;i++)
        for(int j=1;j<=2*n+1;j++)
        a[i][j]=inf;
        for(int i=1;i<=m;i++)
        {
            int x,y,c;
            scanf("%d%d%d",&x,&y,&c);
            a[x][y]=min(a[x][y],c);
        }
        for(int i=1;i<=n;i++)
        {
            mm.add(S,i,1,0);
            mm.add(i+n,T,1,0);
        }
        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        if(a[i][j]!=inf)
        {
            mm.add(i,j+n,1,a[i][j]);
        }
        int mf=0,cst=0;
        mm.work(mf,cst);
        if(mf!=n) puts("-1");
        else printf("%d\n",cst);
    }
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值