现在的位置: 首页 > 综合 > 正文

[网络流24题] 太空飞行计划

2018年04月24日 ⁄ 综合 ⁄ 共 1880字 ⁄ 字号 评论关闭
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#define inf 0x7fffffff
using namespace std;
inline int Read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline bool read(int &x){
	x=0;
	char ch=getchar();
	//printf("test\n");
	while(!(ch>='0' && ch<='9'))ch=getchar();
	while(ch>='0' && ch<='9'){
		x*=10;x+=ch-'0';
		ch=getchar();
	}
	//printf("test:%d\n",x);
	if(int(ch)==13)return true;
	else return false;
}
struct edge{int to,next,v,bef;}e[200001];
int n,m,cnt,T,ans,head[1001],h[1001];
bool N[1001],M[1001],vis[1001];
inline void ins(int u,int v,int w){
	e[++cnt]=(edge){v,head[u],w,w};head[u]=cnt;
	e[++cnt]=(edge){u,head[v],0,0};head[v]=cnt;
}
inline bool bfs(){
	int t=0,w=0,now,q[20001];
	memset(h,-1,sizeof(h));
	h[0]=0;
	while(t<=w){
		now=q[t++];
		for(int i=head[now];i;i=e[i].next){
			if(e[i].v&&h[e[i].to]==-1){
				h[e[i].to]=h[now]+1;
				q[++w]=e[i].to;
			}
		}
	}
	if(h[T]==-1)return 0;
	else return 1;
}
inline int dfs(int x,int f){
	if(x==T)return f;
	int used=0,rest;
	for(int i=head[x];i;i=e[i].next){
		if(e[i].v&&h[e[i].to]==h[x]+1){
			rest=f-used;
			rest=dfs(e[i].to,min(e[i].v,rest));
			e[i].v-=rest;
			e[i+1].v+=rest;
			used+=rest;
			if(used==f)return f;
		}
	}
	if(!used)h[x]=-1;
	return used;
}
inline void DFS(int x){
	if(vis[x])return;
	vis[x]=1;
	if(x<=m)M[x]=1;
	if(x>m)N[x-m]=1;
	for(int i=head[x];i;i=e[i].next){
		if(e[i].v==e[i].bef)continue;
		if(e[i].to!=0&&e[i].to!=T&&!vis[e[i].to])DFS(e[i].to);
	}
}
int main(){
	//freopen("shuttle.in","r",stdin);
	//freopen("shuttle.out","w",stdout);
	m=Read();n=Read();T=m+n+1;
	for(int i=1;i<=m;i++){
		int x=Read();
		ans+=x;
		ins(0,i,x);
		//bool end=false;
		while(1){
			
			x=Read();
			if(x==0)break;
			ins(i,x+m,inf);
			
			//end=read(x);
			//ins(i,x+m,inf);
			//if(end)break;
		}
	}
	for(int i=m+1;i<=m+n;i++){
		int x=Read();
		ins(i,T,x);
	}
	while(bfs())ans-=dfs(0,inf);
	DFS(0);
	for(int i=1;i<=m;i++)if(M[i])printf("%d ",i);
	printf("\n");
	for(int i=1;i<=n;i++)if(N[i])printf("%d ",i);
	printf("\n%d",ans);
	return 0;
}


抱歉!评论已关闭.