hdu 1853 Cyclic Tour (二分图最优匹配:有向环覆盖)

题目链接:https://vjudge.net/problem/HDU-1853

题目:

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. 
       

题意:

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

该有向图中的所有顶点正好被1个或多个不相交的有向环覆盖.

这个值就是 所有这些有向环的权值和. 要求该值越小越好.

分析:
       我们把任意一个顶点i都分成两个,即i和i’. 如果原图存在i->j的边,那么二分图有i->j’的边.
       下面我们要引出几条结论:
       如果原图能由多个不相交的有向环覆盖,那么二分图必然存在完备匹配.(假设原图的有向环为(1->2->3->1) and(6->5->4->6),那么二分图的完备匹配就是1->2’ 2->3’ 3->1’6->5’ 5->4’ 4->6’)
       如果二分图存在完备匹配,那么原图必定能由几个不想交的有向环覆盖.(假设二分图的完备匹配是1->2’ 2->3’ 3->1’ 6->5’ 5->4’ 4->6’那么原图的有向环为(1->2->3->1) and (6->5->4->6))
       如果原图存在权值最大的有向环覆盖,那么二分图的最优匹配一定就是这个值.(因为该有向环覆盖对应了一个二分图的完备匹配,而该完备匹配的权值就等于该有向环覆盖的权值,所以最优匹配不可能丢失该最大权值的匹配)
       现在原题要求的是最小长度匹配,我们把所有已知边的权值都取负数,且那些不存在的边我们取-INF(负无穷). 如果完备匹配存在,那么我们求出的最优匹配权值的绝对值 肯定<INF. 且该绝对值就是最小权值匹配.
       如果完备匹配不存在,那么最优匹配权值的绝对值 肯定>INF.(想想是不是) 或者这么说,如果最终求得的匹配中,有任何一个匹配边用了权值为负无穷的边,那么最优匹配不存在(即完备匹配不存在)
       注意:此题输入可能存在重边.

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
int n,m;
int f[105][105];
const int INF=-1e6;
const int inf=1e6;
int nx[105],ny[105];
bool vis_nx[105],vis_ny[105];
int match[105];
int slack[105]; 
bool dfs(int x)
{
	vis_nx[x]=1;
	for(int i=1;i<=n;i++)
	{
		if(vis_ny[i])continue;
		int gap=nx[x]+ny[i]-f[x][i];
		if(gap==0)
		{
			vis_ny[i]=1;
			if(match[i]==-1||dfs(match[i]))
			{
				match[i]=x;
				return true;
			}
		}
		else {
			slack[i]=min(slack[i],gap);
		}
	}
	return false;
}
int solve()
{
	memset(match,-1,sizeof(match));
	for(int i=1;i<=n;i++)
	{
		ny[i]=0;
		nx[i]=f[i][1];
		for(int j=2;j<=n;j++)
		{
			nx[i]=max(nx[i],f[i][j]);
		}
	}
	for(int i=1;i<=n;i++)
	{
		fill(slack+1,slack+n+1,inf);
		while(1)
		{
			memset(vis_nx,false,sizeof(vis_nx));
			memset(vis_ny,false,sizeof(vis_ny));
			if(dfs(i))break;
			int temp=inf;
			for(int j=1;j<=n;j++)
			{
				if(!vis_ny[j])temp=min(temp,slack[j]);
			}
			for(int j=1;j<=n;j++)
			{
				if(vis_nx[j])nx[j]-=temp;
				if(vis_ny[j])ny[j]+=temp;
				else slack[j]-=temp;
			}
		}
	}
	int ans=0;
	int i;
	for(i=1;i<=n;i++)
	{
		if(match[i]==-1||f[match[i]][i]==INF)break;
		ans+=f[match[i]][i];
	}
	if(i<=n)return -1;
	else return -ans;
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int i,j,k,d;
		for(i=0;i<105;i++)
		{
			for(j=0;j<105;j++)
			{
				f[i][j]=INF;
			}
		}
		for(i=0;i<m;i++)
		{
			scanf("%d%d%d",&j,&k,&d);
			f[j][k]=max(f[j][k],-d);
		}
		printf("%d\n",solve());
	}
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值