Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

发一份代码。

Posted by pmxm at 2015-10-23 22:27:26 on Problem 2987
如果要看题解的话最好看论文。Amber大牛《最小割模型在信息学竞赛中的应用》。。

注意long long..然后统计被删去的点的时候直接把与源点相连的点(和与他们相连的点 统计出来即可。

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>

#define REP(I,A,B) for(int I=A,_END_=B;I<=_END_;I++)
#define REPD(I,A,B) for(int I=A,_END_=B;I>=_END_;I--)
#define RI(X) X=Readint()
#define RII(X,Y) RI(X),RI(Y)
#define RIII(X,Y,Z) RI(X),RI(Y),RI(Z)
#define RS(X) scanf("%s",X)
#define RD(X) scanf("%lf",&X)
#define GCH getchar()
#define PCH(X) putchar(X)
#define MS(X,Y) memset(X,Y,sizeof(X))
#define MC(X,Y,var,len) memcpy(X,Y,sizeof(var)*(len))
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define pb(X) push_back(X)
#define mp(A,B) make_pair(A,B)
#define fr first
#define sc second
#define lch(p) (p+p)
#define rch(p) (p+p+1)
#define lowbit(X) ((X)&(-(X)))

using namespace std;

typedef pair<int,int> poi;

inline int Readint()
{
	int ret=0;
	int f=1;
	char ch;
	do
	{
		ch=GCH;
		if (ch=='-') f*=-1;
	}while(ch>=0 && (ch<'0' || ch>'9'));

	while ('0'<=ch && ch<='9')
	{
		ret=ret*10+ch-'0';
		ch=GCH;
	}
	return ret*f;
}

void open()
{
	freopen("2987.in","r",stdin);
	freopen("2987.out","w",stdout);
}
void close()
{
	fclose(stdin);
	fclose(stdout);
}

const int MAXN = 5E3+10;
const int MAXM = 6e4*2+MAXN+30;

int n;
int m;
int src,snk;
int w[MAXN]={0};

int tot;
int to[MAXM]={0};
int nxt[MAXM]={0};
long long f[MAXM]={0};
int hd[MAXN]={0};

long long ans=0;

inline void add(const int &x,const int &y,const long long &flow)
{
	tot++;
	to[tot]=y;
	nxt[tot]=hd[x];
	f[tot]=flow;
	hd[x]=tot;
}

void init()
{
	RII(n,m);
	src=n+1;
	snk=n+2;
	tot=-1;
	MS(hd,-1);
	REP(i,1,n)
	{
		RI(w[i]);
		if (w[i]>0)
		{
			ans+=w[i];
			add(src,i,w[i]);
			add(i,src,0);
		}
		else
		{
			add(i,snk,-w[i]);
			add(snk,i,0);
		}
	}
	int u,v;
	REP(i,1,m)
	{
		RII(u,v);
		add(u,v,ans+1);
		add(v,u,0);
	}
}

const int MAXQ = MAXN + 10;

int dis[MAXN];
int vis[MAXN];
int in[MAXN];
int que[MAXQ+1];

int SPFA()
{
	int l=0,r=0;
	MS(vis,0);
	MS(dis,0);
	que[r++]=src;
	vis[src]=1;
	int now,j;
	while (l!=r)
	{
		now=que[l];
		in[now]=0;
		if (l==MAXQ) l=0; else l++;
		for (int i=hd[now];i!=-1;i=nxt[i])
			if (f[i])
			{
				j=to[i];
				if (!vis[j])
				{
					vis[j]=1;
					dis[j]=dis[now]+1;
					in[j]=1;
					que[r]=j;
					if (r==MAXQ) r=0; else r++;
				}
				else if (dis[j]>dis[now]+1)
				{
					dis[j]=dis[now]+1;
					if (!in[j])
					{
						in[j]=1;
						que[r]=j;
						if (r==MAXQ) r=0;
						else r++;
					}
				}
			}
	}
	return vis[snk];
}

long long  dfs(int p,long long flow)
{
	long long ret=0,tmp;
	if (p==snk) return flow;
	for (int i=hd[p];i!=-1;i=nxt[i])
		if (f[i] && dis[to[i]]==dis[p]+1)
		{
			tmp=dfs(to[i],min(flow-ret,f[i]));

			f[i]-=tmp;
			f[i^1]+=tmp;

			ret+=tmp;
			if (ret==flow) return ret;
		}
	if (!ret)
		dis[p]=0;
	return ret;
}

long long maxflow()
{
	long long ret=0;
	while (SPFA())
		ret+=dfs(src,ans+1);
	return ret;
}

int cnt=0;

void dfs(int p)
{
	vis[p]=1; cnt++;
	for (int i=hd[p];i!=-1;i=nxt[i])
		if (f[i] && !vis[to[i]])
			dfs(to[i]);
}

int main()
{
	open();
	int _=0;
	init();
	ans-=maxflow();
	MS(vis,0);
	cnt=-1;
	dfs(src);
	printf("%d %I64d\n",cnt,ans);
	close();
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator