POJ1422 Air Raid(最小路径覆盖)

传送门

【题目分析】

补一补老早以前的坑。。。。

题意很简单,给出一个DAG,问用最少路径覆盖DAG数量。

直接拆点跑最大流,一个匹配表示可以减少一条路径,最后用n-最大匹配即可。

【代码~】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=1e3+10;
const int MAXM=1e5+10;
const int INF=0x3f3f3f3f;

int n,m,cnt,s,t;
int head[MAXN],cur[MAXN],depth[MAXN];
int nxt[MAXM],to[MAXM],w[MAXM];

int Read(){
	int i=0,f=1;
	char c;
	for(c=getchar();(c>'9'||c<'0')&&c!='-';c=getchar());
	if(c=='-')
	  f=-1,c=getchar();
	for(;c>='0'&&c<='9';c=getchar())
	  i=(i<<3)+(i<<1)+c-'0';
	return i*f;
}

void Add(int x,int y,int z){
	nxt[cnt]=head[x];
	head[x]=cnt;
	to[cnt]=y;
	w[cnt]=z;
	cnt++;
}

void add(int x,int y,int z){
	Add(x,y,z);
	Add(y,x,0);
}

int bfs(){
	queue<int> q;
	memset(depth,0,sizeof(depth));
	depth[s]=1;
	q.push(s);
	while(!q.empty()){
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=nxt[i]){
			int v=to[i];
			if(!depth[v]&&w[i]){
				depth[v]=depth[u]+1;
				q.push(v);
				if(v==t)
				  return 1;
			}
		}
	}
	return 0;
}

int dfs(int u,int flow){
	if(u==t)
	  return flow;
	for(int &i=cur[u];i!=-1;i=nxt[i]){
		int v=to[i];
		if(depth[v]==depth[u]+1&&w[i]){
			int di=dfs(v,min(w[i],flow));
			if(di){
				w[i]-=di;
				w[i^1]+=di;
				return di;
			}
		}
	}
	return 0;
}

int dinic(){
	int ret=0;
	while(bfs()){
		memcpy(cur,head,sizeof(head));
		while(int d=dfs(s,INF))
		  ret+=d;
	}
	return ret;
}

int main(){
	int T=Read();
	while(T--){
		memset(head,-1,sizeof(head));
		cnt=0;
		n=Read();
		s=0,t=n+n+1;
		for(int i=1;i<=n;++i){
			add(s,i,1),add(i+n,t,1);
		}
		m=Read();
		for(int i=1;i<=m;++i){
			int x=Read(),y=Read();
			add(x,y+n,1);
		}
		cout<<n-dinic()<<'\n';
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值