HDU 3157 Crazy Circuits
2015-10-06 22:37
465 查看
Crazy Circuits
Time Limit: 2000msMemory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3157
64-bit integer IO format: %I64d Java class name: Main
You’ve just built a circuit board for your new robot, and now you need to power it. Your robot circuit consists of a number of electrical components that each require a certain amount of current to operate. Every component has a + and a - lead, which are connected on the circuit board at junctions. Current flows through the component from + to - (but note that a component does not "use up" the current: everything that comes in through the + end goes out the - end).
The junctions on the board are labeled 1, ..., N, except for two special junctions labeled + and - where the power supply terminals are connected. The + terminal only connects + leads, and the - terminal only connects - leads. All current that enters a junction from the - leads of connected components exits through connected + leads, but you are able to control how much current flows to each connected + lead at every junction (though methods for doing so are beyond the scope of this problem1). Moreover, you know you have assembled the circuit in such a way that there are no feedback loops (components chained in a manner that allows current to flow in a loop).
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 100; struct arc { int to,flow,next; arc(int x = 0,int y = 0,int z = -1) { to = x; flow = y; next = z; } } e[maxn*maxn]; int head[maxn],cur[maxn],d[maxn],du[maxn],tot; void add(int u,int v,int flow) { e[tot] = arc(v,flow,head[u]); head[u] = tot++; e[tot] = arc(u,0,head[v]); head[v] = tot++; } bool bfs(int S,int T) { queue<int>q; q.push(S); memset(d,-1,sizeof d); d[S] = 1; while(!q.empty()) { int u = q.front(); q.pop(); for(int i = head[u]; ~i; i = e[i].next) { if(e[i].flow && d[e[i].to] == -1) { d[e[i].to] = d[u] + 1; q.push(e[i].to); } } } return d[T] > -1; } int dfs(int u,int T,int low) { if(u == T) return low; int a,tmp = 0; for(int &i = cur[u]; ~i; i = e[i].next) { if(e[i].flow && d[e[i].to] == d[u]+1&&(a=dfs(e[i].to,T,min(e[i].flow,low)))) { e[i].flow -= a; e[i^1].flow += a; low -= a; tmp += a; if(!low) break; } } if(!tmp) d[u] = -1; return tmp; } int Dinic(int S,int T,int ret = 0) { while(bfs(S,T)) { memcpy(cur,head,sizeof head); ret += dfs(S,T,INF); } return ret; } int main() { char a[10],b[10]; int n,m,u,v,bound,S,T,SS,TT; while(scanf("%d%d",&n,&m),n||m) { memset(head,-1,sizeof head); memset(du,0,sizeof du); int sum = S = 0; T = n + 1; SS = T + 1; TT = SS + 1; for(int i = tot = 0; i < m; ++i) { scanf("%s%s%d",a,b,&bound); if(a[0] == '+') u = S; else sscanf(a,"%d",&u); if(b[0] == '-') v = T; else sscanf(b,"%d",&v); add(u,v,INF); du[u] -= bound; du[v] += bound; } for(int i = 0; i <= T; ++i) { if(du[i] > 0) { add(SS,i,du[i]); sum += du[i]; } else add(i,TT,-du[i]); } u = Dinic(SS,TT); add(T,S,INF); v = Dinic(SS,TT); if(u + v == sum) printf("%d\n",e[tot-1].flow); else puts("impossible"); } return 0; }
View Code
相关文章推荐
- 两道人数多,课程少,query多的题
- easyUI parser的使用
- easyUI 初始化的两种方式
- QueryBuildRange的各种条件的总结
- UINavigationBar自定义返回按钮
- UI 动画之CALayer+CAAnimation 之 CATransition
- HDU 4848 Wow! Such Conquering! 搜索+剪枝
- UI 动画之CALayer+CAAnimation 之 CAAnimationGroup
- UIKit学习笔记2
- UIKit学习笔记1
- UI 动画之CALayer+CAAnimation 之 CAKeyframeAnimation
- 设计模式之ChainOfResponsibility:过滤Request和Response
- UI_数据处理(model)
- Notepad++配合QuickText打造快速高效的文本编辑器
- UI 动画之CALayer+CAAnimation 之 CABasicAnimation
- [232]Implement Queue using Stacks
- 关于UICollectionView在MRC下的纯代码实现之一(在UIViewController中实现)
- 条款20:在传递对象的时候尽量用reference-to-constent来代替,pass-by-value
- 解决UIGestureRecognizer手势与视图上其他滑动视图的冲突
- 为何加入:private static final long serialVersionUID