1、#115. 无源汇有上下界可行流
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> /* ⊂_ヽ \\ Λ_Λ 来了老弟 \('ㅅ') > ⌒ヽ / へ\ / / \\ レ ノ ヽ_つ / / / /| ( (ヽ | |、\ | 丿 \ ⌒) | | ) / 'ノ ) Lノ */ using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << " "; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl ' ' #define boost ios::sync_with_stdio(false);cin.tie(0) #define rep(a, b, c) for(int a = (b); a <= (c); ++ a) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } inline void cmax(int &x,int y){if(x<y)x=y;} inline void cmax(ll &x,ll y){if(x<y)x=y;} inline void cmin(int &x,int y){if(x>y)x=y;} inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = 1e5+9; struct E{ int v,val,id; int nxt; }edge[maxn]; int head[maxn],gtot=0; void addedge(int u,int v,int val,int id){ edge[gtot].v = v; edge[gtot].val = val; edge[gtot].nxt = head[u]; edge[gtot].id = -1; head[u] = gtot++; edge[gtot].v = u; edge[gtot].val = 0; edge[gtot].nxt = head[v]; edge[gtot].id = id; head[v] = gtot++; } int dis[maxn],cur[maxn]; bool bfs(int s,int t){ memset(dis, inf, sizeof(dis)); queue<int>que; que.push(s); dis[s] = 0; for(int i=s; i<=t; i++) cur[i] = head[i]; while(!que.empty()){ int u = que.front(); que.pop(); for(int i=head[u]; ~i; i=edge[i].nxt){ int v = edge[i].v, val = edge[i].val; if(val > 0 && dis[v] > dis[u]+1) { dis[v] = dis[u] + 1; que.push(v); } } } return dis[t] < inf; } int dfs(int u, int t, int maxflow){ if(u == t || maxflow == 0) return maxflow; for(int i=cur[u]; ~i; i = edge[i].nxt){ cur[u] = i; int v = edge[i].v,val = edge[i].val; if(val > 0 && dis[v] == dis[u] + 1){ int f = dfs(v, t, min(maxflow, val)); if(f > 0){ edge[i].val -= f; edge[i ^ 1].val += f; return f; } } } return 0; } int dinic(int s,int t){ int flow = 0; while(bfs(s, t)){ while(int f = dfs(s,t,inf)) flow += f; } return flow; } int ans[maxn],du[maxn],liu[maxn]; int main(){ memset(head, -1, sizeof(head)); int n,m; scanf("%d%d", &n, &m); int s = 0, t = n+1; for(int i=1; i<=m; i++) { int u,v,l,r; scanf("%d%d%d%d", &u, &v, &l, &r); ans[i] = l; du[u] -= l; du[v] += l; liu[i] = r - l; addedge(u, v, r - l, i); } int sum = 0; for(int i=1; i<=n; i++) { if(du[i] > 0) addedge(s, i, du[i], -1), sum += du[i]; if(du[i] < 0) addedge(i, t, -du[i], -1); } int tmp = dinic(s, t); if(sum == tmp) { puts("YES"); for(int i=1; i<=n; i++) { for(int j=head[i]; ~j; j= edge[j].nxt){ if(edge[j].id == -1) continue; int id = edge[j].id; ans[id] += (edge[j].val); // cout<< i << "," << edge[j].v << " = " << (edge[j].val) << endl;; } } for(int i=1; i<=m; i++) printf("%d ", ans[i]); } else puts("NO"); return 0; }
2、#116. 有源汇有上下界最大流
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> /* ⊂_ヽ \\ Λ_Λ 来了老弟 \('ㅅ') > ⌒ヽ / へ\ / / \\ レ ノ ヽ_つ / / / /| ( (ヽ | |、\ | 丿 \ ⌒) | | ) / 'ノ ) Lノ */ using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << " "; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl ' ' #define boost ios::sync_with_stdio(false);cin.tie(0) #define rep(a, b, c) for(int a = (b); a <= (c); ++ a) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } inline void cmax(int &x,int y){if(x<y)x=y;} inline void cmax(ll &x,ll y){if(x<y)x=y;} inline void cmin(int &x,int y){if(x>y)x=y;} inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = 1e5+9; struct E{ int v,val; int nxt; } edge[maxn]; int head[maxn],gtot=0; void addedge(int u,int v,int val){ edge[gtot].v = v; edge[gtot].val = val; edge[gtot].nxt = head[u]; head[u] = gtot++; edge[gtot].v = u; edge[gtot].val = 0; edge[gtot].nxt = head[v]; head[v] = gtot++; } int dis[maxn],cur[maxn]; int n,m; bool bfs(int s,int t){ memset(dis, inf, sizeof(dis)); queue<int>que; que.push(s); dis[s] = 0; for(int i=0; i<=n+1; i++) cur[i] = head[i]; //注意这里不再是s到t。 while(!que.empty()){ int u = que.front(); que.pop(); for(int i=head[u]; ~i; i=edge[i].nxt){ int v = edge[i].v, val = edge[i].val; if(val > 0 && dis[v] > dis[u]+1) { dis[v] = dis[u] + 1; que.push(v); } } } return dis[t] < inf; } int dfs(int u, int t, int maxflow){ if(u == t || maxflow == 0) return maxflow; for(int i=cur[u]; ~i; i = edge[i].nxt){ cur[u] = i; int v = edge[i].v,val = edge[i].val; if(val > 0 && dis[v] == dis[u] + 1){ int f = dfs(v, t, min(maxflow, val)); if(f > 0){ edge[i].val -= f; edge[i ^ 1].val += f; return f; } } } return 0; } int dinic(int s,int t){ int flow = 0; while(bfs(s, t)){ while(int f = dfs(s,t,inf)) flow += f; } return flow; } int ans[maxn],du[maxn]; int main(){ memset(head, -1, sizeof(head)); int s,t; scanf("%d%d%d%d", &n, &m, &s, &t); int S = 0, T = n+1; for(int i=1; i<=m; i++) { int u,v,l,r; scanf("%d%d%d%d", &u, &v, &l, &r); ans[i] = l; du[u] -= l; du[v] += l; addedge(u, v, r - l); } addedge(t, s, inf); int sum = 0; for(int i=1; i<=n; i++) { if(du[i] >= 0) addedge(S, i, du[i]), sum += du[i]; else addedge(i, T, -du[i]); } int tmp = dinic(S, T); if(sum == tmp) { printf("%d ", dinic(s, t)); } else puts("please go home to sleep"); return 0; }
3、#117. 有源汇有上下界最小流
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> /* ⊂_ヽ \\ Λ_Λ 来了老弟 \('ㅅ') > ⌒ヽ / へ\ / / \\ レ ノ ヽ_つ / / / /| ( (ヽ | |、\ | 丿 \ ⌒) | | ) / 'ノ ) Lノ */ using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << " "; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl ' ' #define boost ios::sync_with_stdio(false);cin.tie(0) #define rep(a, b, c) for(int a = (b); a <= (c); ++ a) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } inline void cmax(int &x,int y){if(x<y)x=y;} inline void cmax(ll &x,ll y){if(x<y)x=y;} inline void cmin(int &x,int y){if(x>y)x=y;} inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = 4e5+9; struct E{ int v,val; int nxt; } edge[maxn]; int head[maxn],gtot=0; void addedge(int u,int v,int val){ edge[gtot].v = v; edge[gtot].val = val; edge[gtot].nxt = head[u]; head[u] = gtot++; edge[gtot].v = u; edge[gtot].val = 0; edge[gtot].nxt = head[v]; head[v] = gtot++; } int dis[maxn],cur[maxn]; int n,m; bool bfs(int s,int t){ memset(dis, inf, sizeof(dis)); queue<int>que; que.push(s); dis[s] = 0; for(int i=0; i<=n+1; i++) cur[i] = head[i]; //注意这里不再是s到t。 while(!que.empty()){ int u = que.front(); que.pop(); for(int i=head[u]; ~i; i=edge[i].nxt){ int v = edge[i].v, val = edge[i].val; if(val > 0 && dis[v] > dis[u]+1) { dis[v] = dis[u] + 1; que.push(v); } } } return dis[t] < inf; } int dfs(int u, int t, int maxflow){ if(u == t || maxflow == 0) return maxflow; for(int i=cur[u]; ~i; i = edge[i].nxt){ cur[u] = i; int v = edge[i].v,val = edge[i].val; if(val > 0 && dis[v] == dis[u] + 1){ int f = dfs(v, t, min(maxflow, val)); if(f > 0){ edge[i].val -= f; edge[i ^ 1].val += f; return f; } } } return 0; } int dinic(int s,int t){ int flow = 0; while(bfs(s, t)){ while(int f = dfs(s,t,inf)) flow += f; } return flow; } int ans[maxn],du[maxn]; int main(){ memset(head, -1, sizeof(head)); int s,t; scanf("%d%d%d%d", &n, &m, &s, &t); int S = 0, T = n+1; for(int i=1; i<=m; i++) { int u,v,l,r; scanf("%d%d%d%d", &u, &v, &l, &r); ans[i] = l; du[u] -= l; du[v] += l; addedge(u, v, r - l); } int sum = 0; for(int i=1; i<=n; i++) { if(du[i] >= 0) addedge(S, i, du[i]), sum += du[i]; else addedge(i, T, -du[i]); } int tmp = dinic(S, T); addedge(t, s, inf); tmp += dinic(S, T); if(sum == tmp) { printf("%d ", edge[gtot-1].val); } else puts("please go home to sleep"); return 0; }