#HDU1853#Cyclic Tour(Km经典模型+环)

6 篇文章 0 订阅

Cyclic Tour

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 2503    Accepted Submission(s): 1285


Problem 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.

题目大意:

图中有n个点和m条有向边

现在要将该图分成若干环,每个环中至少有两个点。环与环不能有交点。问所有环的总长度最小为多少?
n<=100


这是一道例题,很经典的思路,也是一个很重要的模型

如果一直在纠结那个环应该怎么保证的话,这将是很难想出来的,

从问题入手(判定)不可以,不妨从条件(性质)开始想

一个环一定有一个性质:环上的每一个点都一定入度为1且出度为1

所以这就为这题提供了很好的解决办法:

将每个点看做两个,然后每个点有一条入边,有一条出边,

这就形成了两个集合,且互不相交。

因为要求最小总长度,所以我们将边权值取反为负数,然后再做完备匹配。


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;

const int Max=105;

int N,M,minz;
bool visx[Max],visy[Max];
int linky[Max],wx[Max],wy[Max];
int map[Max][Max];

bool Dfs(int x){
	visx[x]=1;
	for(int j=1; j<=N; ++j)	if(!visy[j]){
		int t=wx[x]+wy[j]-map[x][j];
		if(!t){
			visy[j]=1;
			if(!linky[j] || Dfs(linky[j])){
				linky[j]=x;
				return 1;
			}
		}
		else minz=min(minz,t);
	}
	return 0;
}

int Km(){
	memset(linky,0,sizeof(linky));
	memset(wy,0,sizeof(wy));
	for(int i=1; i<=N; ++i){
		wx[i]=-0x3f3f3f3f;
		for(int j=1; j<=N; ++j)
			wx[i]=max(wx[i],map[i][j]);
	}
	for(int i=1; i<=N; ++i)
		while(1){
			minz=0x3f3f3f3f;
			for(int j=1; j<=N; ++j)visx[j]=visy[j]=0;
			if(Dfs(i))	break;
			for(int i=1; i<=N; ++i){
				if(visx[i])	wx[i]-=minz;
				if(visy[i])	wy[i]+=minz;
			}
		}
	int Ans=0,cnt=0;
	for(int i=1; i<=N; ++i)if(linky[i]&&map[linky[i]][i]!=-1044266559)
		Ans+=wy[i]+wx[linky[i]],++cnt;
	return cnt==N?-Ans:-1;
}

int main(){
	int A,B,C;
	while(~scanf("%d%d",&N,&M)){
		memset(map,-0x3f,sizeof(map));
		for(int i=1; i<=M; ++i){
			scanf("%d%d%d",&A,&B,&C);
			if(A!=B&&-C>map[A][B])	map[A][B]=-C;
		}
		printf("%d\n",Km());
	}
	return 0;
}






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值