题意:n条关系,m个节点,每条关系由三个字母c,s,e组成代表c节点到s节点的流量为e,问流到节点n最大流量是多少
链接:hdu - 1532
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 405;
struct Edge {
int v, nxt, w;
};
struct Node {
int v, id;
};
int n, m, ecnt;
bool vis[maxn];
int head[maxn];
Node pre[maxn];
Edge edge[maxn];
void init() {
ecnt = 0;
memset(edge, 0, sizeof(edge));
memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w) {
edge[ecnt].v = v;
edge[ecnt].w = w;
edge[ecnt].nxt = head[u];
head[u] = ecnt++;
}
bool bfs(int s, int t) {
queue <int> que;
memset(vis, 0, sizeof(vis));
memset(pre, -1, sizeof(pre));
pre[s].v = s;
vis[s] = true;
que.push(s);
while(!que.empty()) {
int u = que.front();
que.pop();
for(int i = head[u]; i + 1; i = edge[i].nxt) {
int v = edge[i].v;
if(!vis[v] && edge[i].w) {
pre[v].v = u;
pre[v].id = i;
vis[v] = true;
if(v == t) return true;
que.push(v);
}
}
}
return false;
}
int EK(int s, int t) {
int ans = 0;
while(bfs(s, t)) {
int mi = INF;
for(int i = t; i != s; i = pre[i].v) {
mi = min(mi, edge[pre[i].id].w);
}
for(int i = t; i != s; i = pre[i].v) {
edge[pre[i].id].w -= mi;
edge[pre[i].id ^ 1].w += mi;
}
ans += mi;
}
return ans;
}
int main ()
{
int t, kcase = 0;
int N, M, E, k;
char ch;
while(~scanf("%d %d", &N, &M)) {
init();
int a, b, c;
while(N--) {
scanf("%d %d %d", &a, &b, &c);
addEdge(a, b, c);
addEdge(b, a, 0);
};
int ans = EK(1, M);
printf("%d\n", ans);
}
return 0;
}