裸地EK
#include <iostream>
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <queue>
using namespace std;
#define N 220
#define INF 0x7FFFFFFF;
int path[N];
int flow[N];
int map[N][N];//残留网络
queue<int> que;
int bfs(int sta, int end, int vsta, int vend)
{
while(!que.empty()) que.pop();
memset(path, -1, sizeof(path));
path[sta] = 0; flow[sta] = INF;
que.push(sta);
while(!que.empty())
{
int tem = que.front();
que.pop();
if(tem == end) break;
for( int i = vsta; i <= vend; i++)
if(i != sta && path[i] == -1 && map[tem][i])
{
flow[i] = min(flow[tem], map[tem][i]);
path[i] = tem;
que.push(i);
}
}
if(path[end] == -1)
return -1;
return flow[end];
}
int ek(int sta,int end,int vsta,int vend)
//vsta vend 节点编号范围,sta end 源点 汇点
{
int val;
int max_flow = 0, now;
while((val = bfs(sta , end, vsta,vend)) != -1)
{
max_flow += val;
now = end;
while(now != sta)
{
map[path[now]][now] -= val;
map[now][path[now]] += val;
now = path[now];
}
}
return max_flow;
}
int main()
{
int n, m;
while(scanf("%d %d",&n, &m) != EOF)
{
memset(map, 0, sizeof(map));
int s, e, w;
for( int i = 0; i < n; i++)
{
scanf("%d %d %d", &s, &e, &w);
map[s][e] += w;
}
int ans = 0;
ans = ek(1, m, 1, m);
printf("%d\n",ans);
}
}