題目大意:有N個點,M條有向邊,每條邊都有相應的容量。現在要求你從點1運送東西到點N,運送的量為C
如果可以運送,輸出possible
如果不能運送,看能不能只增大一條弧,使得運送成功,如果可以的話,輸出所有可增大的弧
如果都不行,另外輸出
解題思路:先跑一次最大流,最大流如果大於等於C,就不用括弧了
如果不行的話,就進行括弧。
擴大哪些弧的容量呢。答案是割邊的容量,因為最小割==最大流
我們先找出所有的割邊,如何找割邊呢,如果是dinic算法的話,就找到vis[u] == ture 而vis[v] == false 且該邊容量大於0的邊,這些邊就是割邊了
接著將割邊一條一條的擴容,只需要擴大到C的容量就可以了,然後在殘余網絡上跑最大流就可以了
還有一個問題是怎麼求殘余網絡,只需要將所有邊的容量減去流量就是殘余網絡了
#include
#include
#include
#include
#include
using namespace std;
#define N 1010
#define INF 0x3f3f3f3f
struct Edge{
int from, to, cap, flow;
Edge() {}
Edge(int from, int to, int cap, int flow) : from(from), to(to), cap(cap), flow(flow) {}
};
struct Dinic{
int n, m, s, t;
vector edges;
vector G[N];
bool vis[N];
int d[N], cur[N];
void init(int n) {
this->n = n;
for (int i = 0; i <= n; i++) {
G[i].clear();
}
edges.clear();
}
void AddEdge(int from, int to, int cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
int m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool BFS() {
memset(vis, 0, sizeof(vis));
queue Q;
Q.push(s);
vis[s] = 1;
d[s] = 0;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int i = 0; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = true;
d[e.to] = d[u] + 1;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a) {
if (x == t || a == 0)
return a;
int flow = 0, f;
for (int i = cur[x]; i < G[x].size(); i++) {
Edge &e = edges[G[x][i]];
if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[G[x][i] ^ 1].flow -= f;
flow += f;
a -= f;
if (a == 0)
break;
}
}
return flow;
}
int Maxflow(int s, int t) {
this->s = s; this->t = t;
int flow = 0;
while (BFS()) {
memset(cur, 0, sizeof(cur));
flow += DFS(s, INF);
}
return flow;
}
void clear() {
for (int i = 0; i < edges.size(); i++)
edges[i].flow = 0;
}
void remmant() {
for (int i = 0; i < edges.size(); i++)
edges[i].cap -= edges[i].flow;
}
vector MinCut() {
vector tmp;
for (int i = 0; i < edges.size(); i++)
if (vis[edges[i].from] && !vis[edges[i].to] && edges[i].cap > 0) tmp.push_back(i);
return tmp;
}
};
Dinic dinic;
#define M 10010
int n, m, c;
int cas = 1;
struct Cut{
int u, v;
bool operator <(const Cut &a) const {
if (u == a.u)
return v < a.v;
return u < a.u;
}
}cut[M];
int cmp(const Cut &a, const Cut &b) {
if (a.u == b.u)
return a.v < b.v;
return a.u < b.u;
}
void solve() {
dinic.init(n);
int u, v, cost;
for (int i = 0; i < m; i++) {
scanf(%d%d%d, &u, &v, &cost);
dinic.AddEdge(u, v, cost);
}
printf(Case %d: , cas++);
int Maxflow = dinic.Maxflow(1, n);
if (Maxflow >= c) printf(possible
);
else {
int cnt = 0;
vector tmp = dinic.MinCut();
dinic.remmant();
for (int i = 0; i < tmp.size(); i++) {
Edge &e = dinic.edges[tmp[i]];
dinic.clear();
e.cap = c;
int ans = dinic.Maxflow(1,n);
if (ans + Maxflow >= c) {
cut[cnt].u = dinic.edges[tmp[i]].from;
cut[cnt++].v = dinic.edges[tmp[i]].to;
}
e.cap = 0;
}
if (cnt == 0)
printf(not possible
);
else {
sort(cut, cut + cnt);
printf(possible option:(%d,%d), cut[0].u, cut[0].v);
for (int i = 1; i < cnt; i++) printf(,(%d,%d), cut[i].u, cut[i].v);
printf(
);
}
}
}
int main() {
while (scanf(%d%d%d, &n, &m, &c) != EOF && n + m + c) solve();
return 0;
}