POJ 2125 Destroying The Graph(最小割-最小点权覆盖)

题目链接:http://poj.org/problem?id=2125

题意:

POJ 2125 Destroying The Graph(最小割-最小点权覆盖)

思路:将每个点i拆成两个点xi,yi,分别表示两种操作,每条有向边<u,v>连接<xu,yv,INF>,s向每个点连边<s,xi,ai>,每个点向t连边<yi,t,bi>。

#include <iostream>

#include <stdio.h>

#include <string.h>

#include <algorithm>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <stack>

#include <string>

#include <map>





#define max(x,y) ((x)>(y)?(x):(y))

#define min(x,y) ((x)<(y)?(x):(y))

#define abs(x) ((x)>=0?(x):-(x))

#define i64 long long

#define u32 unsigned int

#define u64 unsigned long long

#define clr(x,y) memset(x,y,sizeof(x))

#define CLR(x) x.clear()

#define ph(x) push(x)

#define pb(x) push_back(x)

#define Len(x) x.length()

#define SZ(x) x.size()

#define PI acos(-1.0)

#define sqr(x) ((x)*(x))

#define MP(x,y) make_pair(x,y)

#define EPS 1e-9



#define FOR0(i,x) for(i=0;i<x;i++)

#define FOR1(i,x) for(i=1;i<=x;i++)

#define FOR(i,a,b) for(i=a;i<=b;i++)

#define FORL0(i,a) for(i=a;i>=0;i--)

#define FORL1(i,a) for(i=a;i>=1;i--)

#define FORL(i,a,b)for(i=a;i>=b;i--)

using namespace std;





void RD(int &x){scanf("%d",&x);}

void RD(u32 &x){scanf("%u",&x);}

void RD(double &x){scanf("%lf",&x);}

void RD(int &x,int &y){scanf("%d%d",&x,&y);}

void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}

void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}

void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}

void RD(int &x,int &y,int &z,int &t){scanf("%d%d%d%d",&x,&y,&z,&t);}

void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}

void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}

void RD(char &x){x=getchar();}

void RD(char *s){scanf("%s",s);}

void RD(string &s){cin>>s;}





void PR(int x) {printf("%d\n",x);}

void PR(int x,int y) {printf("%d %d\n",x,y);}

void PR(i64 x) {printf("%lld\n",x);}

void PR(u32 x) {printf("%u\n",x);}

void PR(double x) {printf("%.5lf\n",x);}

void PR(char x) {printf("%c\n",x);}

void PR(char *x) {printf("%s\n",x);}

void PR(string x) {cout<<x<<endl;}





struct Node

{

	int u,v,cap,flow,next;

};





const int INF=1000000000;

Node edges[120005];

int n,m,s,t,head[1005],e;

int num[1005],h[1005],curedge[1005],pre[1005];



void add(int u,int v,double cap)

{

	edges[e].u=u;

	edges[e].v=v;

	edges[e].cap=cap;

	edges[e].flow=0.0;

	edges[e].next=head[u];

	head[u]=e++;

}



void Add(int u,int v,double cap)

{

	add(u,v,cap);

	add(v,u,0.0);

}







int Maxflow(int s,int t,int n)

{

	int ans=0,i,k,x,d,u;



	memset(num,0,sizeof(num));

	memset(h,0,sizeof(h));

	for(i=0;i<=n;i++) curedge[i]=head[i];

	num[n]=n;u=s;

	while(h[u]<n)

	{

		if(u==t)

		{

			d=INF+1;

			for(i=s;i!=t;i=edges[curedge[i]].v) if(d>edges[curedge[i]].cap)

				k=i,d=edges[curedge[i]].cap;

			for(i=s;i!=t;i=edges[curedge[i]].v)

			{

				x=curedge[i];

				edges[x].cap-=d;

				edges[x].flow+=d;

				edges[x^1].cap+=d;

				edges[x^1].flow-=d;

			}

			ans+=d;u=k;

		}

		for(i=curedge[u];i!=-1;i=edges[i].next) if(edges[i].cap>0&&h[u]==h[edges[i].v]+1)

			break;

		if(i!=-1)

		{

			curedge[u]=i;

			pre[edges[i].v]=u;

			u=edges[i].v;

		}

		else

		{

			if(--num[h[u]]==0) break;

			curedge[u]=head[u];

			for(x=n,i=head[u];i!=-1;i=edges[i].next) if(edges[i].cap>0&&h[edges[i].v]<x)

				x=h[edges[i].v];

			h[u]=x+1;num[h[u]]++;

			if(u!=s) u=pre[u];

		}

	}

	return ans;

}



int visit[205];





void DFS(int u)

{

	visit[u]=1;

	int i;

	for(i=head[u];i!=-1;i=edges[i].next)

	{

	    if(!visit[edges[i].v]&&edges[i].cap>0)

		{

		    DFS(edges[i].v);

		}

	}

}



int main()

{

    RD(n,m);clr(head,-1); e=0; s=0;t=n+n+1;

	int i,u,v,x;

	FOR1(i,n) RD(x),Add(i+n,t,x);

	FOR1(i,n) RD(x),Add(s,i,x);

	FOR1(i,m) RD(u,v),Add(u,v+n,INF);

	PR(Maxflow(s,t,t+1));

	clr(visit,0); DFS(s);

	vector<int> ans;

	FOR1(i,2*n) if(visit[i]&&i>n||!visit[i]&&i<=n)

    {

        ans.push_back(i);

    }

    PR(SZ(ans));

    FOR0(i,SZ(ans))

	{

		if(ans[i]<=n) printf("%d -\n",ans[i]);

		else printf("%d +\n",ans[i]-n);

	}

	return 0;

}

  

你可能感兴趣的:(Graph)