5、在跑一边最大流 maxflow=Dinic(s,t) 最大流 maxflow 各个边的流量输出见代码
Zoj 3229
/*
* this code is made by LinMeiChen
* Problem:
* Type of Problem: 有源汇上下界最大流
* Thinking:
* Feeling:
*/
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<list>
using namespace std;
typedef long long lld;
typedef unsigned int ud;
#define INF_MAX 0x3f3f3f3f
#define eatline() char chch;while((chch=getchar())!='\n')continue;
#define MemsetMax(a) memset(a,0x3f,sizeof a)
#define MemsetZero(a) memset(a,0,sizeof a)
#define MemsetMin(a) memset(a,-1,sizeof a)
#define MemsetFalse(a) MemsetZero(a)
#define PQ priority_queue
#define Q queue
#define maxn 1400
#define maxm 400008
struct Edge
{
int v, f;
int next;
}E[maxm<<2];
int k, cnt;
int head[maxn], h[maxn];
int level[maxn], stack[maxn];
int inout[maxn], low[maxn];
int G[maxn], D[maxn];
bool BFS(int s, int t)
{
memset(level, 0, sizeof level);
level[s] = 1;
Q<int>q;
q.push(s);
while (!q.empty())
{
int u = q.front(); q.pop();
if (u == t)
return true;
for (int i = head[u]; i != -1; i = E[i].next)
{
int v = E[i].v;
if (!level[v] && E[i].f > 0)
{
level[v] = level[u] + 1;
q.push(v);
}
}
}
return false;
}
int Dinic(int s, int t)
{
int maxflow = 0;
while (BFS(s, t))
{
memcpy(h, head, sizeof h);
int top = 0;
int u = s;
while (true)
{
if (u == t)
{
int minflow = INF_MAX, flag = 0;
for (int i = 0; i < top; i++)
{
if (minflow>E[stack[i]].f)
{
minflow = E[stack[i]].f;
flag = i;
}
}
for (int i = 0; i < top; i++)
{
E[stack[i]].f -= minflow;
E[stack[i] ^ 1].f += minflow;
}
top = flag;
maxflow += minflow;
u = E[stack[top] ^ 1].v;
}
for (int i = h[u]; i != -1; i = h[u] = E[i].next)
{
int v = E[i].v;
if (level[v] == level[u] + 1 && E[i].f)
break;
}
if (h[u] != -1)
{
stack[top++] = h[u];
u = E[h[u]].v;
}
else
{
if (top == 0)
break;
level[u] = 0;
u = E[stack[--top] ^ 1].v;
}
}
}
return maxflow;
}
void add_edge(int u, int v, int f1, int f2 = 0)
{
E[k].v = v;
E[k].f = f1;
E[k].next = head[u];
head[u] = k++;
E[k].v = u;
E[k].f = f2;
E[k].next = head[v];
head[v] = k++;
}
int main()
{
int n, m;
int s, t;
int ss, tt;
int C;
while (scanf("%d%d", &n, &m) != EOF)
{
s = n + m + 1;//原图的原点
t = s + 1;//原图的汇点
ss = t + 1;//构造图的原点
tt = ss + 1;//构造图的汇点
k = 0;
cnt = 0;
memset(head, -1, sizeof head);
memset(inout, 0, sizeof inout);
for (int i = 1; i <= m; i++)
scanf("%d", &G[i]);
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &C, &D[i]);
for (int j = 1; j <= C; j++)
{
int u, low_, up;
scanf("%d%d%d", &u, &low_, &up);
u++;
add_edge(i, u + n, up - low_);
inout[i] -= low_;
inout[u + n] += low_;
low[cnt++] = low_;
}
}
for (int i = 1; i <= n; i++)
{
add_edge(s, i, D[i]);
}
for (int i = 1; i <= m; i++)
{
add_edge(i + n, t, INF_MAX);
inout[t] += G[i];
inout[i + n] -= G[i];
}
int sum = 0;
for (int i = 1; i <= t; i++)
{
if (inout[i] > 0)
add_edge(ss, i, inout[i]), sum += inout[i];
else
add_edge(i, tt, -inout[i]);
}
add_edge(t, s, INF_MAX);
if (sum!=Dinic(ss, tt))
printf("-1\n");
else
{
head[ss] = head[tt] = -1;
int ans=Dinic(s, t);
printf("%d\n", ans);
for (int i = 0; i < cnt; i++)
{
printf("%d\n", E[i << 1 | 1].f + low[i]);
}
}
puts("");
}
return 0;
}