#include <iostream>
#include <queue>
using namespace std;
#define MAXN 15
#define MAXC 1005
int f[ MAXN ][ MAXN ];
queue<int> q;
int previous[ MAXN ];
int T, N, M, X, Y, C;
void init()
{
for(int i = 0; i < N; i ++)
{
for(int j = 0; j < N; j ++)
{
f[ i ][ j ] = 0;
f[ j ][ i ] = 0;
}
}
}
bool BFS(int src, int dst)
{
int u;
int *v = new int[ N ];
for(int i = 0; i < N; i ++)
{
v[ i ] = 0;
}
while(!q.empty())
{
q.pop();
}
previous[ src ] = -1;
q.push(src);
while(!q.empty())
{
u = q.front();
q.pop();
v[ u ] = 1;
if(dst == u)
{
break;
}
for(int i = 0; i < N; i ++)
{
if(f[ u ][ i ] != 0 && v[ i ] == 0)
{
previous[ i ] = u;
q.push(i);
}
}
}
delete v;
if(u == dst)
{
return true;
}
else
{
return false;
}
}
int EK(int src, int dst)
{
int t, minflow;
int ans = 0;
while(BFS(src, dst))
{
t = dst;
minflow = MAXC;
while(t != src)
{
if(f[ previous[ t ] ][ t ] < minflow)
{
minflow = f[ previous[ t ]][ t ];
}
t = previous[ t ];
}
ans += minflow;
t = dst;
while(t != src)
{
f[ previous[ t ] ][ t ] -= minflow;
f[ t ][ previous[ t ] ] += minflow;
t = previous[ t ];
}
}
return ans;
}
int main()
{
scanf("%d", &T);
int K = 0;
while(K < T)
{
scanf("%d %d", &N, &M);
init();
while(M > 0)
{
scanf("%d %d %d", &X, &Y, &C);
f[ X - 1 ][ Y - 1 ] += C;
M --;
}
printf("Case %d: %d\n", (K + 1), EK(0, N - 1));
K ++;
}
return 0;
}
杭电ACM3549(最大流)
最新推荐文章于 2022-03-09 23:37:11 发布