POJ3469 & 最小割(最大流)模板

就是一个求最小割.

 

sol:

  数据比较大,n有20000,内部相连的边有20w,这么算算就要存八九十万的边,空间显然降不下来...然而打了dinic并不觉得快很多...最快跑到3800+ms

  然后跪一大爷2000ms出头,他只开了50w的边这是怎么做到的qwq...然后并没有什么显著不同啊他封在一个class里(我根本不知道这玩意儿只知道跟struct差不多)...难道是读入优化打丑了...

  附上他的代码地址:http://acm.hust.edu.cn/vjudge/problem/viewSource.action?id=4433085

Code:

  

/*==========================================================================
# Last modified: 2016-03-07 19:49
# Filename: poj3469.cpp
# Description: 
==========================================================================*/
#define me AcrossTheSky 
#include <cstdio> 
#include <cmath> 
#include <ctime> 
#include <string> 
#include <cstring> 
#include <cstdlib> 
#include <iostream> 
#include <algorithm> 
  
#include <set> 
#include <map> 
#include <stack> 
#include <queue> 
#include <vector> 
 
#define lowbit(x) (x)&(-x) 
#define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++) 
#define FORP(i,a,b) for(int i=(a);i<=(b);i++) 
#define FORM(i,a,b) for(int i=(a);i>=(b);i--) 
#define ls(a,b) (((a)+(b)) << 1) 
#define rs(a,b) (((a)+(b)) >> 1) 
#define getlc(a) ch[(a)][0] 
#define getrc(a) ch[(a)][1] 
 
#define maxn 20015
#define maxm 1000500 
#define pi 3.1415926535898 
#define _e 2.718281828459 
#define INF 1070000000 
using namespace std; 
typedef long long ll; 
typedef unsigned long long ull; 
 
template<class T> inline 
void read(T& num) { 
    bool start=false,neg=false; 
    char c; 
    num=0; 
    while((c=getchar())!=EOF) { 
        if(c=='-') start=neg=true; 
        else if(c>='0' && c<='9') { 
            start=true; 
            num=num*10+c-'0'; 
        } else if(start) break; 
    } 
    if(neg) num=-num; 
} 
/*==================split line==================*/ 
int S,T,n,m;
int sume=1;
struct Edge{
	int from,to,cap;
}e[maxm];
int first[maxn],d[maxn],next[maxm],cur[maxn];
bool vis[maxn];
queue<int> q;
void addedge(int x,int y,int cap){
	sume++; e[sume].from=x; e[sume].to=y; e[sume].cap=cap;
	next[sume]=first[x]; first[x]=sume;
	sume++; e[sume].from=y; e[sume].to=x; e[sume].cap=0;
	next[sume]=first[y]; first[y]=sume;
}
int bfs(){
	for(int i=S;i<=T;i++) vis[i]=false;
	q.push(0); d[0]=0; vis[0]=true;
	while (!q.empty()){
		int now=q.front(); q.pop();
		for (int i=first[now];i;i=next[i])
			if (!vis[e[i].to] && e[i].cap){
				d[e[i].to]=d[now]+1;
				vis[e[i].to]=true;
				q.push(e[i].to);
			}
	}
	return vis[T];
}
int dfs(int now,int a){
	if (now==T || !a) return a;
	int f,flow=0;
	for (int & i=cur[now];i;i=next[i])
		if (d[now]+1==d[e[i].to] && (f=dfs(e[i].to,min(a,e[i].cap)))>0){
			flow+=f; a-=f; e[i].cap-=f; e[i^1].cap+=f;
			if (!a) break;
		}
	return flow;
	
}
int dinic(){
	int flow=0;
	while (bfs()){
		FORP(i,0,n) cur[i]=first[i];
		flow+=dfs(S,INF);
	}
	return flow;
}
int main(){ 
	read(n); read(m);
	FORP(i,1,n) {
		int x;
		read(x); addedge(0,i,x);
		read(x); addedge(i,n+1,x);
	}
	FORP(i,1,m){
		int x,y,z; read(x); read(y); read(z);
		addedge(x,y,z); addedge(y,x,z);
	}
	S=0,T=n+1;
	printf("%d",dinic());
}

 

posted @ 2016-03-07 21:27  YCuangWhen  阅读(148)  评论(0编辑  收藏  举报