题意:n 个点,np个起点, nc个终点,m条边,求所有起点到终点的最大流
建个超源超汇,+sscanf读入就是道isap的模板题了。(一直没用过sscanf突然发现好神奇0 0
p:这里还有一个是边因为建双向,而终点往起点也有连边,一共就是N*N*2条边了
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define ls rt << 1
#define rs rt << 1 | 1
#define pi acos(-1.0)
#define eps 1e-8
#define asd puts("sdfsdfsdfsdfsdfsdf");
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 120;
const int st = N-10, ed = N-9;
struct node{
int v, w, nxt;
}e[N*N*2];
int head[N];
int gap[N];
int cur[N];
int dep[N];
int s[N], top;
int cnt, tot;
queue <int> q;
int n, m1, m2, m;
void init()
{
cnt = 0;
memset( head, -1, sizeof( head ) );
}
void add( int u, int v, int w )
{
e[cnt].v = v;
e[cnt].w = w;
e[cnt].nxt = head[u];
head[u] = cnt++;
e[cnt].w = 0;
e[cnt].v = u;
e[cnt].nxt = head[v];
head[v] = cnt++;
}
void rev_bfs()
{
memset( gap, 0, sizeof( gap ) );
memset( dep, -1, sizeof( dep ) );
while( !q.empty() ) q.pop();
q.push(ed);
dep[ed] = 0;
gap[0] = 1;
while( !q.empty() ) {
int u = q.front();
q.pop();
for( int i = head[u]; ~i; i = e[i].nxt ) {
int v = e[i].v;
if( ~dep[v] )
continue;
dep[v] = dep[u] + 1;
gap[dep[v]]++;
q.push(v);
}
}
}
int isap()
{
int flow = 0, u = st, i;
memcpy( cur, head, sizeof head );
rev_bfs();
top = 0;
int nv = ed + 1;
while( dep[st] < nv ) {
if( u == ed ) {
int tmp, minn = inf;
for( i = 0; i < top; ++i ) {
if( minn > e[s[i]].w ) {
minn = e[s[i]].w;
tmp = i;
}
}
for( i = 0; i < top; ++i ) {
e[s[i]].w -= minn;
e[s[i]^1].w += minn;
}
flow += minn;
top = tmp;
u = e[s[top]^1].v;
}
for( i = cur[u]; ~i; i = e[i].nxt ) {
if( e[i].w > 0 && dep[u] == dep[e[i].v] + 1 ) {
cur[u] = i;
break;
}
}
if( ~i ) {
s[top++] = cur[u];
u = e[i].v;
}
else {
if( 0 == gap[dep[u]] )
break;
int minn = nv;
for( i = head[u]; ~i; i = e[i].nxt ) {
int v = e[i].v;
if( e[i].w > 0 && minn > dep[v] ) {
minn = dep[v];
cur[u] = i;
}
}
dep[u] = minn + 1;
gap[dep[u]]++;
if( u != st ) {
u = e[ s[--top]^1 ].v;
}
}
}
return flow;
}
char a[N*N];
int main()
{
while( ~scanf("%d%d%d%d%", &n, &m1, &m2, &m) ) {
init();
int u, v, w;
while( m-- ) {
scanf("%s", a);
sscanf( a, "(%d,%d)%d", &u, &v, &w);
add( u, v, w );
}
while( m1-- ) {
scanf("%s", a);
sscanf( a, "(%d)%d", &u, &w );
add( st, u, w );
}
while( m2-- ) {
scanf("%s", a);
sscanf( a, "(%d)%d", &u, &w );
add( u, ed, w );
}
int ans = isap();
printf("%d\n", ans);
}
return 0;
}