1011 - 最小路径可重复点覆盖 - Treasure Exploration(POJ 2594)

传送门

 

分析

做了几道这样的题,有没有惊奇的发现,题目中一般都会非常明显的告诉你这是一个有向无环图(“And some points are connected by one-way road, which means that, through the road, a robot can only move from one end to the other end, but cannot move back. For some unknown reasons, there is no circle in this graph”)

也会非常友善的告诉你限制:这是一个可重复点覆盖(“You should notice that the roads of two different robots may contain some same point. ”)

那就用Floyd传递闭包,让本会经过重复的点的两条链,变成不会重复的路径,然后拆点,二分图匹配即可

 

代码

 

RE代码

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<queue>
#define in read()
#define M 2000000
#define inf 0x3f3f3f3f
#define N 21009
using namespace std;
inline int read(){
	char ch;int f=1,res=0;
	while((ch=getchar())<'0'||ch>'9') if(ch=='-') f=-1;
	while(ch>='0'&&ch<='9'){
		res=(res<<3)+(res<<1)+ch-'0';
		ch=getchar();
	}
	return f==1?res:-res;
}

int S,T;
int nxt[M],to[M],cap[M],head[N],cur[N],cnt=1,lev[N];
int f[505][505];
void add(int x,int y,int z){
	nxt[++cnt]=head[x];head[x]=cnt;to[cnt]=y;cap[cnt]=z;
	nxt[++cnt]=head[y];head[y]=cnt;to[cnt]=x;cap[cnt]=0;
}
bool bfs(){
	for(int i=S;i<=T;++i){	cur[i]=head[i];lev[i]=-1;} 
	queue<int > q;
	q.push(S);lev[S]=0;
	while(!q.empty()){
		int u=q.front();q.pop();
		for(int e=head[u];e;e=nxt[e]){
			int v=to[e];
			if(lev[v]!=-1||cap[e]<=0) continue;
			lev[v]=lev[u]+1;
			if(v==T) return true;
			q.push(v);
 		}
	}
	return false;
}
int dinic(int u,int flow){
	if(u==T)	return flow;
	int delta,res=0;
	for(int &e=cur[u];e;e=nxt[e]){
		int v=to[e];
		if(lev[v]>lev[u]&&cap[e]>0){
			delta=dinic(v,min(cap[e],flow-res));
			if(delta){
				res+=delta;cap[e]-=delta;
				cap[e^1]+=delta;if(res==flow) return flow;
			}
		}
	}
	return res;
}
int tt,m,n;
int main(){
	while(1){
		n=in;m=in;
		if(n==0&&m==0) break;
		cnt=1;memset(head,0,sizeof(head));
		memset(f,0,sizeof(f));
		S=0;T=2*n+1;
		int i,j,k;
		for(i=1;i<=m;++i){
			j=in;k=in;
			f[j][k]=1;
		}
		for(k=1;k<=n;++k)
		for(i=1;i<=n;++i)
			for(j=1;j<=n;++j)
				if(f[i][j]==1||(f[i][k]==1&&f[k][j]==1))
					add(i,j+n,1);
		for(i=1;i<=n;++i) add(S,i,1),add(i+n,T,1);
		int maxflow=0;
		while(bfs()) maxflow+=dinic(S,inf);
		printf("%d\n",n-maxflow);
	}
	return 0;
}
	

为什么会RE呢???不解

一开始以为网络流出锅,看网上一大片Hungary算法,以为再无网络流 ……oh

但……

此时,!!!dzyo出山啦!!!大佬在我代码面前一走,啊,瞬间发现问题

这个传递闭包的时候加边,显然是不正确的啊,这样不知道会多加多少次

RE简直妥妥的

我们先传递闭包,把图的连通性弄出来,再来枚举建一下边,就A啦

 

AC代码请收下

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<queue>
#define in read()
#define M 2000000
#define inf 0x3f3f3f3f
#define N 21009
using namespace std;
inline int read(){
	char ch;int f=1,res=0;
	while((ch=getchar())<'0'||ch>'9') if(ch=='-') f=-1;
	while(ch>='0'&&ch<='9'){
		res=(res<<3)+(res<<1)+ch-'0';
		ch=getchar();
	}
	return f==1?res:-res;
}

int S,T;
int nxt[M],to[M],cap[M],head[N],cur[N],cnt=1,lev[N];
int f[505][505];
void add(int x,int y,int z){
	nxt[++cnt]=head[x];head[x]=cnt;to[cnt]=y;cap[cnt]=z;
	nxt[++cnt]=head[y];head[y]=cnt;to[cnt]=x;cap[cnt]=0;
}
bool bfs(){
	for(int i=S;i<=T;++i){	cur[i]=head[i];lev[i]=-1;} 
	queue<int > q;
	q.push(S);lev[S]=0;
	while(!q.empty()){
		int u=q.front();q.pop();
		for(int e=head[u];e;e=nxt[e]){
			int v=to[e];
			if(lev[v]!=-1||cap[e]<=0) continue;
			lev[v]=lev[u]+1;
			if(v==T) return true;
			q.push(v);
 		}
	}
	return false;
}
int dinic(int u,int flow){
	if(u==T)	return flow;
	int delta,res=0;
	for(int &e=cur[u];e;e=nxt[e]){
		int v=to[e];
		if(lev[v]>lev[u]&&cap[e]>0){
			delta=dinic(v,min(cap[e],flow-res));
			if(delta){
				res+=delta;cap[e]-=delta;
				cap[e^1]+=delta;if(res==flow) return flow;
			}
		}
	}
	return res;
}
int tt,m,n;
int main(){
	while(1){
		n=in;m=in;
		if(n==0&&m==0) break;
		cnt=1;memset(head,0,sizeof(head));
		memset(f,0,sizeof(f));
		S=0;T=2*n+1;
		int i,j,k;
		for(i=1;i<=m;++i){
			j=in;k=in;
			f[j][k]=1;
		}
		for(k=1;k<=n;++k)
		for(i=1;i<=n;++i)
			for(j=1;j<=n;++j)
				if(f[i][j]==1||(f[i][k]==1&&f[k][j]==1))
					f[i][j]=1;
		for(i=1;i<=n;++i)
			for(j=1;j<=n;++j)
				if(f[i][j]==1) add(i,j+n,1);
		for(i=1;i<=n;++i) add(S,i,1),add(i+n,T,1);
		int maxflow=0;
		while(bfs()) maxflow+=dinic(S,inf);
		printf("%d\n",n-maxflow);
	}
	return 0;
}
	

 

(天呐!!!!!!秒变dzyo的小迷妹!!!!!!!太强了啊!!!!!!!!!疯狂打call!!!!!!真的是什么奇葩的代码问题都可以被dzyo揪出来)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值