NWERC2015 Elementary Math

#include
#include
#include
#define maxl 10010
#define inf 2000000001

using namespace std;

int n,m,S=0,T=maxl-1,cnt=0,numcnt=0,ans;
int ehead[maxl],q[maxl],dis[maxl],cur[maxl];
long long a[maxl],b[maxl];
struct calcnum{long long x;int ind,opt;} ha[maxl];
struct ed{int w,nxt,to,opt;} e[maxl<<2];

bool cmp(const calcnum &x,const calcnum &y)
{
	return x.x0 && dis[e[i].to]<0)
		{
			dis[e[i].to]=dis[u]+1;
			q[++tail]=e[i].to;
		}
	}
	if(dis[T]>0)
		return 1;
	else
		return 0;
}

int min(int a,int b)
{
	if(a0 && 
	(t=find(e[i].to,min(e[i].w,low))))
	{
		e[i].w-=t;
		if(i&1)
			e[i+1].w+=t;
		else
			e[i-1].w+=t;
		return t;
	}
	return 0;
}

void init()
{
	cur[S]=ehead[S];cur[T]=ehead[T];
		for(int i=1;i<=n+numcnt;i++)
			cur[i]=ehead[i];
}

void mainwork()
{
	int t=0;
	ans=0;
	while(bfs())
	{
		init();
		while(t=find(S,2000000001))
			ans+=t;
	}
}

void print()
{
	int v;
	if(ans!=n)
		printf("impossible");
	else
	{
		for(int u=1;u<=n;u++)
			for(int i=ehead[u];i;i=e[i].nxt)
			{
				v=e[i].to;
				if(v>n && v<=n+numcnt && e[i].w==0)
				{
					if(e[i].opt==1)
						printf("%I64d + %I64d = %I64d\n",a[u],b[u],a[u]+b[u]);
					if(e[i].opt==2)
						printf("%I64d - %I64d = %I64d\n",a[u],b[u],a[u]-b[u]);
					if(e[i].opt==3)
						printf("%I64d * %I64d = %I64d\n",a[u],b[u],a[u]*b[u]);
					break;
				}
			}
	}
}

int main()
{
	prework();
	mainwork();
	print();
	return 0;
}

http://codeforces.com/gym/101485

2500对数,最多7500种结果,想到网络流,先预处理出所有不同的结果,然后全部连流量为1的边,数对到结果的边还要标记是+,-,*那种操作,跑完网络流,残余网络找一找每对数连出去的哪条边用完了。

此题add写错了,然后cf上交题一定要保证所有输入输出都符合变量原来的类型,而且是%I64d,不然WA on test 1。浪费好久时间- -。


你可能感兴趣的:(网络流)