开始最大流判断容量是否大于C,如果不大于,然后去找最小割中的弧,枚举容量为C,TLE。因为最小割中的弧增加的容量一定会引起最大流的增加,于是我们可以再原有最大流的基础上枚举最小割容量中的弧,然后判断是否大于等于C即可。
楼主渣代码。
#include <iostream> #include <cstdlib> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <vector> #include <queue> #include <algorithm> #include <map> using namespace std; const int maxn = 110; const int INF = 0x3f3f3f3f; struct Edge { int from, to, cap, flow; bool operator < (const Edge &e) const { if(e.from != from) return from < e.from; else return to < e.to; } 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<Edge> edges; vector<int> G[maxn]; bool vis[maxn]; int d[maxn]; int cur[maxn]; void init(int n) { this->n = n; for(int i = 0; i <= n; i++) G[i].clear(); edges.clear(); } void ClearFlow () { for(int i = 0; i < edges.size(); i++) edges[i].flow = 0; } void AddEdge(int from, int to, int cap) { edges.push_back(Edge (from, to, cap, 0)); edges.push_back(Edge (to, from, 0, 0)); m = edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool BFS() { memset(vis, 0, sizeof(vis)); queue<int> Q; Q.push(s); d[s] = 0; vis[s] = 1; while(!Q.empty()) { int x = Q.front(); Q.pop(); for(int i = 0; i < G[x].size(); i++) { Edge& e = edges[G[x][i]]; if(!vis[e.to] && e.cap > e.flow) { vis[e.to] = 1; d[e.to] = d[x]+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 Reduce() { for(int i = 0; i < edges.size(); i++) edges[i].cap -= edges[i].flow; } void Mincut(vector<int> &cut) { cut.clear(); for(int i = 0; i < edges.size(); i++) { Edge& e = edges[i]; if(vis[e.from] && !vis[e.to] && e.cap > 0) cut.push_back(i); } } }; void readint(int &x) { char c; while(!isdigit(c)) c = getchar(); x = 0; while(isdigit(c)) { x = x*10 + c-'0'; c = getchar(); } } void writeint(int x) { if(x > 9) writeint(x/10); putchar(x%10+'0'); } //////////////////// Dinic g; int n, m, s, t; int c; int times; vector<int> cut; vector<Edge> ans; void prosess() { g.init(n+3); s = 1, t = n; while(m--) { int x, y, z; readint(x), readint(y), readint(z); g.AddEdge(x, y, z); } printf("Case %d: ", ++times); int flow = g.Maxflow(s, t); if(flow >= c) { printf("possible\n"); return ;} else { g.Mincut(cut); ans.clear(); g.Reduce(); for(int i = 0; i < cut.size(); i++) { Edge&e = g.edges[cut[i]]; e.cap = c; g.ClearFlow(); if(flow + g.Maxflow(s, t) >= c) ans.push_back(e); e.cap = 0; } } if(ans.empty()) { printf("not possible\n"); return ;} sort(ans.begin(), ans.end()); int first = 1; for(int i = 0; i < ans.size(); i++) { Edge& e = ans[i]; if(first) { printf("possible option:(%d,%d)", e.from, e.to); first = 0; } else printf(",(%d,%d)", e.from, e.to); } printf("\n"); } int main() { times = 0; while(~scanf("%d%d%d", &n, &m, &c) && n) { prosess(); } return 0; }