http://poj.org/problem?id=1273
题意很清楚,典型的最大流求解问题,采用EK解(目前只会这个)。只不过这个题目的数据得注意重边的情况,开始的时候没考虑重边,被坑了,若干wrong之后,才想到这一可能性。代码如下:
/*
ID: csuchenan
PROG: POJ1237
LANG: C++
*/
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std ;
#define INF 100000000
const int maxn = 205 ;
int cap[maxn][maxn] ;
int flow[maxn][maxn] ;
int pre[maxn] ;
int vis[maxn] ;
void bfs(int s , int t) ;
int num ;
int mto ;
int main()
{
while(scanf("%d %d" , &mto , &num)!= EOF)
{
int i = 0 ;
int p ;
int q ;
int w ;
memset(cap , 0 , sizeof(cap)) ;
memset(flow , 0 , sizeof(flow)) ;
for(i = 0 ; i < mto ; i ++)
{
scanf("%d %d %d" , &p , &q , &w) ;
cap[p][q] += w ;
}
bfs(1 , num) ;
}
return 0 ;
}
void bfs(int s , int t)
{
int i ;
int u ;
long long f ;
f = 0 ;
queue<int> Q ;
while(1)
{
memset(vis , 0 , sizeof(vis)) ;
memset(pre , 0 , sizeof(pre)) ;
vis[s] = INF ;
Q.push(s) ;
while(!Q.empty())
{
u = Q.front() ;
Q.pop() ;
for(i = 1 ; i <= num ; i ++)
{
if(!vis[i] && flow[u][i] < cap[u][i])
{
vis[i] = min(vis[u] , cap[u][i] - flow[u][i]) ;
Q.push(i) ;
pre[i] = u ;
}
}
}
if(vis[t] == 0)
break ;
for(u = t ; u != s ; )
{
flow[pre[u]][u] += vis[t] ;
flow[u][pre[u]] -= vis[t] ;
u = pre[u] ;
}
f += vis[t] ;
}
printf("%lld\n" , f) ;
}