【BZOJ2625】[Neerc2009]Inspection 最小流

【BZOJ2625】[Neerc2009]Inspection

Description

You are in charge of a team that inspects a new ski resort. A ski resort is situated on several mountains and consists of a number of slopes. Slopes are connected with each other, forking and joining. A map of the ski resort is represented as an acyclic directed graph. Nodes of the graph represent different points in ski resort and edges of the graph represent slopes between the points, with the direction of edges going downwards. 
Your team has to inspect each slope of the ski resort. Ski lifts on this resort are not open yet, but you have a helicopter. In one fiight the helicopter can drop one person into any point of the resort. From the drop off point the person can ski down the slopes, inspecting each slope as they ski. It is fine to inspect the same slope multiple times, but you have to minimize the usage of the helicopter. So, you have to figure out how to inspect all the slopes with the fewest number of helicopter flights.
给张有向无环图,问至少多少条路径能够覆盖所有的边(可以重复

Input

The first line of the input file contains a single integer number n (2 <= n <= 100) - the number of points in the ski resort. The following n lines of the input file describe each point of the ski resort numbered from 1 to n. Each line starts with a single integer number mi (0 <= mi < n for i from 1 to n) and is followed by mi integer numbers aij separated by spaces. All aij are distinct for each i and each aij (1 <= aij <= n, aij  \ne i) represents a slope going downwards from point i to point aij . Each point in the resort has at least one slope connected to it.

Output

On the first line of the output file write a single integer number k - the minimal number of helicopter flights that are needed to inspect all slopes. Then write k lines that describe inspection routes for each helicopter flight. Each route shall start with single integer number from 1 to n - the number of the drop off point for the helicopter flight, followed by the numbers of points that will be visited during inspection in the corresponding order as the slopes are inspected going downwards. Numbers on a line shall be separated by spaces. You can write routes in any order.

Sample Input

8
1 3
1 7
2 4 5
1 8
1 8
0
2 6 5
0

Sample Output

4

题解:经典的最小链覆盖问题。

采用有上下界的网络流的思路,将每个点拆成两个,从出点向入点连一条(0,inf)的边,对于每条边(a,b)从a的出点向b的入点连一条(1,inf)的边。然后先跑可行流再反着跑最大流。但是发现一个性质,第一遍跑可行流时一定能够满流,所以我们直接跑第二遍即可,具体连边方法:

1.S->每个点的出点,每个点的入点->T 容量inf
2.每个点的入点->出点 容量inf
3.对于(a,b),a的出点->b的入点 容量inf,a的出点->S,b的入点->T 容量1

ans=m-从T到S的最大流

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int inf=1<<30;
int n,m,S,T,cnt,ans;
int to[100000],next[100000],val[100000],head[1000],d[1000],m1[1000],m2[1000];
queue<int> q;
int rd()
{
	int ret=0,f=1;	char gc=getchar();
	while(gc<'0'||gc>'9')	{if(gc=='-')	f=-f;	gc=getchar();}
	while(gc>='0'&&gc<='9')	ret=ret*10+gc-'0',gc=getchar();
	return ret*f;
}
void add(int a,int b,int c,int d)
{
	to[cnt]=b,val[cnt]=c,next[cnt]=head[a],head[a]=cnt++;
	to[cnt]=a,val[cnt]=d,next[cnt]=head[b],head[b]=cnt++;
}
int dfs(int x,int mf)
{
	if(x==T)	return mf;
	int i,k,temp=mf;
	for(i=head[x];i!=-1;i=next[i])
	{
		if(d[to[i]]==d[x]+1&&val[i])
		{
			k=dfs(to[i],min(temp,val[i]));
			if(!k)	d[to[i]]=0;
			val[i]-=k,val[i^1]+=k,temp-=k;
			if(!temp)	break;
		}
	}
	return mf-temp;
}
int bfs()
{
	while(!q.empty())	q.pop();
	memset(d,0,sizeof(d));
	int i,u;
	q.push(S),d[S]=1;
	while(!q.empty())
	{
		u=q.front(),q.pop();
		for(i=head[u];i!=-1;i=next[i])
		{
			if(!d[to[i]]&&val[i])
			{
				d[to[i]]=d[u]+1;
				if(to[i]==T)	return 1;
				q.push(to[i]);
			}
		}
	}
	return 0;
}
int main()
{
	n=rd(),S=0,T=2*n+1;
	int i,a,b;
	memset(head,-1,sizeof(head));
	for(i=1;i<=n;i++)
	{
		a=rd(),m1[i]=a,ans+=a;
		while(a--)	b=rd(),add(i,b+n,inf,0),m2[b]++;
	}
	for(i=1;i<=n;i++)	add(S,i,inf,m1[i]),add(i+n,T,inf,m2[i]),add(i+n,i,inf,0);
	swap(S,T);
	while(bfs())	ans-=dfs(S,inf);
	printf("%d",ans);
	return 0;
}

转载于:https://www.cnblogs.com/CQzhangyu/p/7297651.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值