记录编号 146179 评测结果 AAAAAAAAAA
题目名称 运输问题1 最终得分 100
用户昵称 Gravatarztx 是否通过 通过
代码语言 C++ 运行时间 0.002 s
提交时间 2015-01-13 08:42:30 内存使用 0.52 MiB
显示代码纯文本
/****************************************\
* Author : hzoi_ztx
* Title  : cogs 11. 运输问题1
* ALG    : 网络流 最大流 ISAP算法
* CMT    :
* Time   :
\****************************************/

#include <cstdio>
#include <cstring>

int CH , NEG ;
inline void read(int& ret) {
    ret = NEG = 0 ; while (CH=getchar() , CH<'!') ;
    if (CH == '-') NEG = true , CH = getchar() ;
    while (ret = ret*10+CH-'0' , CH=getchar() , CH>'!') ;
    if (NEG) ret = -ret ;
}

#define  maxn  110LL
#define  maxm  10010LL

struct FST { int to , next , flow ; } e[maxm<<1] ;
int star[maxn] , tote ;
inline void FST_init() { memset(star , 0 , sizeof star) ; tote = 1 ; }
inline void AddEdge(int u , int v , int cap) {
	e[++tote].to = v ; e[tote].flow = cap ; e[tote].next = star[u] ; star[u] = tote ;
	e[++tote].to = u ; e[tote].flow = 0 ; e[tote].next = star[v] ; star[v] = tote ;
}

int n ;

#define  infi     0x3f3f3f3fLL
#define  min(x,y) ((x)<(y)?(x):(y))
int N , S , T ;
int h[maxn] , vh[maxn] ;

int dfs(int u , int flowu) {
int p , tmp = h[u]+1 , flow = 0 , flowv ;
	if (u == T) return flowu ;
	for (p = star[u] ; p ; p = e[p].next) {
		if (e[p].flow && (h[e[p].to]+1==h[u])) {
			flowv = dfs(e[p].to , min(flowu-flow , e[p].flow)) ;
			flow += flowv ; e[p].flow -= flowv ; e[p^1].flow += flowv ;
			if (flow==flowu || h[S]==N) return flow ;
		}
	}
	for (p = star[u] ; p ; p = e[p].next)
		if (e[p].flow) tmp = min(tmp , h[e[p].to]) ;
	if (--vh[h[u]] == 0) h[S] = N ;
	else ++ vh[h[u]=tmp+1] ;
	return flow ;
}

int SAP() {
	int ret = 0 ;
	memset(vh , 0 , sizeof vh) ;
	memset(h , 0 , sizeof h) ;
	vh[S] = N ;
	while (h[S] < N) ret += dfs(S , infi) ;
	return ret ;
}

int u , v , w ;
inline void BuildGraph() {
	FST_init() ;
	for (u = 1 ; u <= n ; u ++ )
		for (v = 1 ; v <= n ; v ++ ) {
			read(w) ;
			if (w > 0) AddEdge(u , v , w) ;
		}
	N = n , S = 1 , T = n ;
}

int main() {
	#define READ
	#ifdef  READ
		freopen("maxflowa.in" ,"r",stdin ) ;
		freopen("maxflowa.out","w",stdout) ;
	#endif
	read(n) ;
	BuildGraph() ;
	printf("%d\n", SAP() ) ;
	#ifdef  READ
		fclose(stdin) ; fclose(stdout) ;
	#else
		getchar() ; getchar() ;
	#endif
	return 0 ;
}