【 POJ - 3801】Crazy Circuits(有源汇、上下界最小流)
Description
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 problem). 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).
Figure 1: Examples of two valid circuit diagrams. In (a), all components can be powered along directed paths from the positive terminal to the negative terminal. In (b), components 4 and 6 cannot be powered, since there is no directed path from junction 4 to the negative terminal.In the interest of saving power, and also to ensure that your circuit does not overheat, you would like to use as little current as possible to get your robot to work. What is the smallest amount of current that you need to put through the + terminal (which you can imagine all necessarily leaving through the − terminal) so that every component on your robot receives its required supply of current to function?
Input
The input file will contain multiple test cases. Each test case begins with a single line containing two integers: N (0 ≤ N ≤ 50), the number of junctions not including the positive and negative terminals, and M (1 ≤ M ≤ 200), the number of components in the circuit diagram. The next M lines each contain a description of some component in the diagram. The ith component description contains three fields: pi, the positive junction to which the component is connected, ni, the negative junction to which the component is connected, and an integer Ii (1 ≤ Ii ≤ 100), the minimum amount of current required for component i to function. The junctions pi and ni are specified as either the character ‘+’ indicating the positive terminal, the character ‘-’ indicating the negative terminal, or an integer (between 1 and N) indicating one of the numbered junctions. No two components have the same positive junction and the same negative junction. The end-of-file is denoted by an invalid test case with N = M = 0 and should not be processed.Output
For each input test case, your program should print out either a single integer indicating the minimum amount of current that must be supplied at the positive terminal in order to ensure that every component is powered, or the message “impossible” if there is no way to direct a sufficient amount of current to each component simultaneously.Sample Input
6 10
+ 1 1
1 2 1
1 3 2
2 4 5
+ - 1
4 3 2
3 5 5
4 6 2
5 - 1
6 5 3
4 6
+ 1 8
1 2 4
1 3 5
2 4 6
3 - 1
3 4 3
0 0Sample Output
9
impossibleHint
For those who are electronics-inclined, imagine that you have the ability to adjust the potential on any component without altering its current requirement, or equivalently that there is an accurate variable potentiometer connected in series with each component that you can adjust. Your power supply will have ample potential for the circuit.
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 1010
#define Maxm 100100
#define INF 0xfffffff char s[];
int stt,edd;
int first[Maxn],dis[Maxn];
int sum; struct node
{
int x,y,f,o,next;
}t[Maxm];int len; int mymin(int x,int y) {return x<y?x:y;} void ins(int x,int y,int f)
{
if(x==stt) sum+=f;
t[++len].x=x;t[len].y=y;t[len].f=f;
t[len].next=first[x];first[x]=len;t[len].o=len+;
t[++len].x=y;t[len].y=x;t[len].f=;
t[len].next=first[y];first[y]=len;t[len].o=len-;
} void make_edge(int x,int y,int k1,int k2)
{
ins(stt,y,k1);
ins(x,edd,k1);
ins(x,y,k2-k1);
} queue<int > q;
bool bfs(int st,int ed)
{
while(!q.empty()) q.pop();
memset(dis,-,sizeof(dis));
q.push(st);dis[st]=;
while(!q.empty())
{
int x=q.front();
for(int i=first[x];i;i=t[i].next) if(t[i].f>)
{
int y=t[i].y;
if(dis[y]==-)
{
dis[y]=dis[x]+;
q.push(y);
}
}
q.pop();
}
if(dis[ed]!=-) return ;
return ;
} int ffind(int x,int ed,int flow)
{
if(x==ed) return flow;
int now=;
for(int i=first[x];i;i=t[i].next) if(t[i].f>)
{
int y=t[i].y;
if(dis[y]==dis[x]+)
{
int a=ffind(y,ed,mymin(flow-now,t[i].f));
t[i].f-=a;
t[t[i].o].f+=a;
now+=a;
if(now==flow) break;
}
}
if(now==) dis[x]=-;
return now;
} int max_flow(int st,int ed)
{
int ans=;
while(bfs(st,ed))
{
ans+=ffind(st,ed,INF);
}
if(ans!=sum) return -;
return ans;
} int main()
{
int n,m;
while()
{
scanf("%d%d",&n,&m);
if(n==&&m==) break;
int st=n+,ed=st+;len=;
stt=ed+;edd=stt+;sum=;
memset(first,,sizeof(first));
for(int i=;i<=m;i++)
{
int x,y;
scanf("%s",s);
if(s[]=='+') x=st;
else if(s[]=='-') x=ed;
else
{
int now=;x=;
while(s[now]>=''&&s[now]<='') x=x*+s[now++]-'';
}scanf("%s",s);
if(s[]=='+') y=st;
else if(s[]=='-') y=ed;
else
{
int now=;y=;
while(s[now]>=''&&s[now]<='') y=y*+s[now++]-'';
}
int c;
scanf("%d",&c);
make_edge(x,y,c,INF);
}
int id=len+;
ins(ed,st,INF);
int now=max_flow(stt,edd);
if(now==-) printf("impossible\n");
else
{
printf("%d\n",t[id].f);
}
}
return ;
}
[POJ3801]
2016-06-13 13:12:16
【 POJ - 3801】Crazy Circuits(有源汇、上下界最小流)的更多相关文章
- HDU 3157 Crazy Circuits (有源汇上下界最小流)
题意:一个电路板,上面有N个接线柱(标号1~N) 还有两个电源接线柱 + - 然后是 给出M个部件正负极的接线柱和最小电流,求一个可以让所有部件正常工作的总电流. 析:这是一个有源汇有上下界的 ...
- BZOJ_2502_清理雪道_有源汇上下界最小流
BZOJ_2502_清理雪道_有源汇上下界最小流 Description 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道), ...
- 【Loj117】有源汇上下界最小流(网络流)
[Loj117]有源汇上下界最小流(网络流) 题面 Loj 题解 还是模板题. #include<iostream> #include<cstdio> #include< ...
- hdu3157有源汇上下界最小流
题意:有源汇上下界最小流裸题,主要就是输入要用字符串的问题 #include<bits/stdc++.h> #define fi first #define se second #defi ...
- BZOJ 2502 清理雪道(有源汇上下界最小流)
题面 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降的方向. 你的团队负责每周定时清理雪道.你们拥有一架直升飞机, ...
- BZOJ 2502 清理雪道/ Luogu P4843 清理雪道 (有源汇上下界最小流)
题意 有一个有向无环图,求最少的路径条数覆盖所有的边 分析 有源汇上下界最小流板题,直接放代码了,不会的看dalao博客:liu_runda 有点长,讲的很好,静心看一定能看懂 CODE #inclu ...
- sgu176 有源汇上下界最小流
题意:有一堆点和边,1起点,n终点,某些边有可能必须满流,要求满足条件的最小流 解法:按原图建边,满流的即上下界都是容量,但是这样按有源汇上下界可行流求出来的可能不是最小流,那么我们需要开始建边的时候 ...
- SGU 176 Flow construction(有源汇上下界最小流)
Description 176. Flow construction time limit per test: 1 sec. memory limit per test: 4096 KB input: ...
- HDU 3157 Crazy Circuits(有源汇上下界最小流)
HDU 3157 Crazy Circuits 题目链接 题意:一个电路板,上面有N个接线柱(标号1~N),还有两个电源接线柱 + -.给出一些线路,每一个线路有一个下限值求一个能够让全部部件正常工作 ...
- bzoj 2502 清理雪道 (有源汇上下界最小流)
2502: 清理雪道 Time Limit: 10 Sec Memory Limit: 128 MB Description 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场 ...
随机推荐
- careercup-栈与队列 3.2
3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.push.pop和min三个方法的时间复杂度必须为O(1). 我们假设除了用一个栈s1来保存数据,还用另一个栈s ...
- PureMVC(JS版)源码解析(十一):Model类
这篇博文讲PureMVC三个核心类——Model类.Model类的构造函数及工厂函数[即getInstance()方法]和View类.Controller类是一样的,这里就不重复讲解了,只 ...
- python学习笔记--Django入门二 Django 的模板系统
为了使网站更干净简洁更容易维护,页面的设计和Python的代码必须分离开.我们可以使用Django的 模板系统 (Template System)来实现这种模式. 几个简单的模板标签(tag): ...
- JavaScript 应用开发 #1:理解模型与集合
在 < Backbone 应用实例 > 这个课程里面,我们会一起用 JavaScript 做一个小应用,它可以管理任务列表,应用可以创建新任务,编辑还有删除任务等等.这个实例非常好的演示了 ...
- chmod -R o+rX /data
When using chmod -R o+rx /data , you set the execute permission on all directories as well as files ...
- Linux bash中的变量分类
1.本地变量 VAR=XYZ 2.局部变量 local VAR=XYZ 3.环境变量 在子shell中可以起作用 export VAR=XYZ 4.位置变量 $0 脚本名本身 $1 第一个参数 $2 ...
- HTML5的你应该记住的一些知识点
刚开始学HTML5是从w3school开始的,那只是非常简单的一些了解,后面开始看一些xiongdilian的HTML5+CSS3的视频,照着视频做了一些简单的demo(需要的童鞋可以联系我,当然网上 ...
- 初学HTML5系列二:HTML5新增的事件属性
Window事件属性: 属性 值 描述 onafterprint script 文档打印之后运行的脚本. 属性发生于用户设置页面打印并且打印对话框已出现之后. onbeforeprint scri ...
- 懒人神器之T4模板
最近遇到一个比较令人烦躁的问题,特别是对于我等懒癌末期者.实在难以忍受!具体问题是这样,这个项目是一个新的项目.使用EF框架来开发,那么在搭建架构时,当我们新加一个Entity时,就需要在每个层级添加 ...
- Laravel_1 安装
1>http://www.golaravel.com/post/install-and-run-laravel-5-x-on-windows/ 2>http://www.golaravel ...