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 Ciocio at 2014-02-19 17:20:18 on Problem 3469
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <deque>

using namespace std;

#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define rrep(i,b,a) for(int i=(b);i>=(a);--i)
#define pf printf
#define sf scanf
#define p_b push_back
#define INF (~0U>>3)
#define ll long long
#define MAXN 20010
#define MAXM 500010

struct edge{
    int v,c;
	edge* next;
	edge* opt;	
}*head[MAXN],Edge[MAXM];
int N,M;
int S,T;
int idx;
int dis[MAXN];
int vd[MAXN];

void Read(int& x){
	char tt=getchar();
	while(tt<'0'||'9'<tt) tt=getchar();
	for(x=0;'0'<=tt&&tt<='9';x=(x<<1)+(x<<3)+tt-'0',tt=getchar());
}

void Addedge(int u,int v,int c1,int c2){
	idx++;
	Edge[idx].v=v;
	Edge[idx].c=c1;
	Edge[idx].next=head[u];
	Edge[idx].opt=Edge+idx+1;
	head[u]=Edge+idx;
	
	idx++;
	Edge[idx].v=u;
	Edge[idx].c=c2;
	Edge[idx].next=head[v];
	Edge[idx].opt=Edge+idx-1;
	head[v]=Edge+idx;
}

void Init(){
	Read(N);
	Read(M);
	S=0;T=N+1;
	int u,v,tmp;
	rep(i,1,N){
		Read(tmp);
		Addedge(S,i,tmp,0);
		Read(tmp);
		Addedge(i,T,tmp,0);
	}
	rep(i,1,M){
		Read(u);
		Read(v);
		Read(tmp);
		Addedge(u,v,tmp,tmp);
	}
}

void Bfs(){
	memset(dis,-1,sizeof dis);
	queue <int> Q;
	Q.push(T);
	dis[T]=0;
	while(!Q.empty()){
		int u=Q.front();
		Q.pop();
		for(edge* p=head[u];p;p=p->next)
		    if(dis[p->v]==-1){
		    	dis[p->v]=dis[u]+1;
		    	vd[dis[p->v]]++;
		    	Q.push(p->v);
		    }
	}
}

int Sap(int u,int flow){
	if(u==T) return flow;
	int delta=0;
	for(edge* p=head[u];p;p=p->next)
	    if(p->c&&dis[u]==dis[p->v]+1){
	    	int tmp=Sap(p->v,min(p->c,flow-delta));
	    	p->c-=tmp;
	    	p->opt->c+=tmp;
	    	delta+=tmp;
	    	if(delta==flow) return delta;
	    }
	if(dis[S]>T) return delta;
	if(!(--vd[dis[u]])) dis[S]=T+1;
	vd[++dis[u]]++;
	return delta; 
}

void Solve(){
	int ans=0;
	Bfs();
	while(dis[S]<=T) ans+=Sap(S,INF);
	cout<<ans<<endl;
}

int main(){
	Init();
	Solve();
	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