HDU 3157 Crazy Circuits (有源汇上下界最小流)
Posted dwtfukgv
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 3157 Crazy Circuits (有源汇上下界最小流)相关的知识,希望对你有一定的参考价值。
题意:一个电路板,上面有N个接线柱(标号1~N) 还有两个电源接线柱 + - 然后是 给出M个部件正负极的接线柱和最小电流,求一个可以让所有部件正常工作的总电流。
析:这是一个有源汇有上下界的最小流。
有源汇有上下界最大流:
1.构造附加网络
2.对ss、tt求最大流(ss、tt满流则有解)
3.若有解,对s、t求最大流
有源汇有上下界最小流:
1.构造附加网络(不添加[t,s]边)
2.对ss、tt求最大流
3.添加[t,s]边
4.对ss、tt求最大流
5.若ss、tt满流,则[t,s]的流量就是最小流
代码如下:
#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(i,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 double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 80 + 10; const int maxm = 1e5 + 10; const int mod = 50007; 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]; int d[maxn]; bool vis[maxn]; int cur[maxn]; void init(int n){ this-> n = n; for(int i = 0; i < n; ++i) G[i].cl; edges.cl; } void addEdge(int from, int to, int cap){ edges.pb((Edge){from, to, cap, 0}); edges.pb((Edge){to, from, 0, 0}); m = edges.sz; G[from].pb(m-2); G[to].pb(m-1); } bool bfs(){ ms(vis, 0); vis[s] = 1; d[s] = 1; queue<int> q; q.push(s); while(!q.empty()){ int x = q.front(); q.pop(); for(int i = 0; i < G[x].sz; ++i){ Edge &e = edges[G[x][i]]; if(!vis[e.to] && e.cap > e.flow){ d[e.to] = d[x] + 1; vis[e.to] = 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].sz; ++i){ Edge &e = edges[G[x][i]]; if(d[e.to] == d[x] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0){ e.flow += f; edges[G[x][i]^1].flow -= f; a -= f; flow += f; if(a == 0) break; } } return flow; } int maxflow(int s, int t){ this->s = s; this-> t = t; int flow = 0; while(bfs()){ ms(cur, 0); flow += dfs(s, INF); } return flow; } void solve(int t){ for(int i = 0; i < G[s].sz; ++i){ Edge &e = edges[G[s][i]]; if(e.cap > e.flow){ puts("impossible"); return ; } } printf("%d\n", edges[*G[t].rbegin()].flow); } }; Dinic dinic; int in[maxn]; int main(){ while(scanf("%d %d", &n, &m) == 2 && n+m){ int s = n + 2, t = n + 3; dinic.init(n + 10); char a[15], b[15], c[15]; ms(in, 0); for(int i = 0; i < m; ++i){ scanf("%s %s %s", a, b, c); int u = a[0] == ‘+‘ ? 0 : atoi(a); int v = b[0] == ‘-‘ ? n+1 : atoi(b); int val = atoi(c); in[v] += val; in[u] -= val; dinic.addEdge(u, v, INF); } for(int i = 0; i <= n+1; ++i){ if(in[i] > 0) dinic.addEdge(s, i, in[i]); if(in[i] < 0) dinic.addEdge(i, t, -in[i]); } dinic.maxflow(s, t); dinic.addEdge(n+1, 0, INF); dinic.maxflow(s, t); dinic.solve(n+1); } return 0; }
Crazy Circuits
以上是关于HDU 3157 Crazy Circuits (有源汇上下界最小流)的主要内容,如果未能解决你的问题,请参考以下文章
POJ 3801/HDU 3157 Crazy Circuits | 有下界的最小流
HDU 3157 Crazy Circuits (有源汇上下界最小流)
[hdu] 3157 Crazy Circuits || 有源汇上下界最小流