Crazy Circuits
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 536 | Accepted: 435 |
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?
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).
![](https://i-blog.csdnimg.cn/blog_migrate/2bbbbbde6abb1998ca6567ebc2506023.png)
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 0
Sample Output
9 impossible
Hint
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.
深深觉得这个题目难度很大,这个题目是说有一个电路图,给出接线柱和电子件数,和每件电子的联系和最低电流,包括正负开关,这个题目是看人家的,自己真是打死也做不出来,水啊!!!求最小流的办法
其实就是一个 有源汇 有上下界 最小流 问题
处理有源汇有上下界最大流问题是:
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]的流量就是最小流
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define M 9999999
#define N 100010
struct Node
{
int v,w,next;
}unit[500000];
int dis[N],gap[N],head[N],nodes;
int in[N],low[N];
int sourse,sink,nn;
void addedge(int u,int v,int w)
{
unit[nodes].v=v;
unit[nodes].w=w;
unit[nodes].next=head[u];
head[u]=nodes++;
unit[nodes].v=u;
unit[nodes].w=0;
unit[nodes].next=head[v];
head[v]=nodes++;
}
int dfs(int s,int a)
{
if(s==sink)
return a;
int left=a,mindis=nn;
for(int j=head[s]; j!=-1; j=unit[j].next)
{
int v=unit[j].v;
if(unit[j].w)
{
if(dis[v]+1==dis[s])
{
int minn=min(left,unit[j].w);
minn=dfs(v,minn);
unit[j].w-=minn;
unit[j^1].w+=minn;
left-=minn;
if(dis[sourse]>=nn)
return a-left;
if(left==0)break;
}
if(dis[v]<mindis)
mindis=dis[v];
}
}
if(left==a)
{
if(!(--gap[dis[s]]))
dis[sourse]=nn;
dis[s]=mindis+1;
gap[dis[s]]++;
}
return a-left;
}
int sap(int s,int e)
{
int ans=0;
nn=e+1;
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
gap[0]=nn;
sourse=s;
sink=e;
while(dis[sourse]<nn)
ans+=dfs(sourse,M);
return ans;
}
int main()
{
int n,u,m,v;
int i,j,k;
while(scanf("%d%d",&n,&m),n|m)
{
int s=0,t=n+m+1,ss=t+1,tt=t+2,sum=0;
memset(head,-1,sizeof(head));
memset(in,0,sizeof(in));
nodes=0;
char s1[2],s2[2];
int f;
for(i=1;i<=m;i++)
{
scanf("%s%s%d",s1,s2,&f);
if(s1[0]=='+')
u=s;
else
sscanf(s1,"%d",&u);
if(s2[0]=='-')
v=t;
else
sscanf(s2,"%d",&v);
addedge(u,v,M-f);
in[u]-=f;
in[v]+=f;
}
for(i=0;i<=t;i++)
{
if(in[i]>0){sum+=in[i];addedge(ss,i,in[i]);}
if(in[i]<0)addedge(i,tt,-in[i]);
}
int f1=sap(ss,tt);
int p=nodes;
addedge(t,s,M);
int f2=sap(ss,tt);
if(f1+f2!=sum)printf("impossible\n");
else printf("%d\n",unit[p^1].w);
}
return 0;
}