题意:给定上一个有容量和下界的网络,让你求出一组可行解。
析:先建立一个超级源点 s 和汇点 t ,然后在输入时记录到每个结点的下界的和,建边的时候就建立c - b的最后再建立 s 和 t , 在建立时,如果 i 结点的输入的大于输出的,那么就是从 s 建立一条边,否则 i 与 t 建立,然后跑一次最大流,就OK了,注意求出的流量是没有下界,再加上下界的就好了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include <bitset> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a, b, sizeof a) #define sz size() #define pu push_up #define pd push_down #define cl clear() #define all 1,n,1 #define FOR(x,n) for(int i = (x); i < (n); ++i) #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e16; const double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 200 + 50; const int mod = 1000; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r > 0 && r <= n && c > 0 && c <= m; } struct Edge{ int from, to, cap, 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; edges.clear(); for(int i = 0; i < n; ++i) G[i].clear(); } void addEdge(int from, int to, LL 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; } }; Dinic dinic; int in[maxn*maxn], out[maxn*maxn]; int B[maxn*maxn]; int main(){ scanf("%d %d", &n, &m); int s = 0, t = n + 1; for(int i = 0; i < m; ++i){ int u, v, b, c; scanf("%d %d %d %d", &u, &v, &b, &c); dinic.addEdge(u, v, c - b); B[i] = b; in[v] += b; out[u] += b; } int ans = 0; for(int i = 1; i <= n; ++i){ int c = in[i] - out[i]; if(c > 0) dinic.addEdge(s, i, c), ans += c; else dinic.addEdge(i, t, -c); } if(dinic.maxFlow(s, t) != ans){ puts("NO"); return 0; } puts("YES"); for(int i = 0; i < m; ++i) printf("%d\n", dinic.edges[i<<1].flow + B[i]); return 0; }