hdu 3157 流量有上下限的最小流

Crazy Circuits

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 154    Accepted Submission(s): 62


Problem 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 1). 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?

Hint
1 For those who are electronics-inclined, imagine that you have the ability to adjust the potential on any componentwithout 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.
 

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  i th 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
 

Source


定义边(u,v,b,c),即从点u到点v的边,流量下限是b,上限是c。

原来的边(u,v,b,c)建边u->v,容量为c-b。

in[v]代表以v为终点的流量下界和,out[u]代表以u为起点的流量下界和。

创造虚拟源点SS,汇点TT。

建边SS->i,容量为in[i]。建边i->TT,容量为out[i]。

跑一次最大流maxflow(SS,TT),加边(T,S,inf) (S,T为原始网络的源点和汇点)

再泡一次最大流maxflow(SS,TT).

检查SS的出边,如果有没有满流的,则不存在可行流,即无解。

否则最小可行流=inf-残余网络中(T,S)。


#include<cstdio>
#define min(a,b) (a<b?a:b)
using namespace std;
const int mm=4444;
const int mn=55;
const int oo=1000000000;
int node,src,dest,edge;
int ver[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn],in[mn];
inline void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0;i<node;++i)head[i]=-1,in[i]=0;
    edge=0;
}
void addedge(int u,int v,int c)
{
    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool bfs()
{
    int i,u,v,l,r=0;
    for(i=0;i<node;++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0;l<r;++l)
        for(i=head[u=q[l]];i>=0;i=next[i])
            if(flow[i]&&dis[v=ver[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}
int dfs(int u,int exp)
{
    if(u==dest)return exp;
    for(int &i=work[u],v,tmp;i>=0;i=next[i])
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}
void Dinic_flow()
{
    while(bfs())
    {
        for(int i=0;i<node;++i)work[i]=head[i];
        while(dfs(src,oo));
    }
}
int Limit_flow()
{
    int i,src0,dest0;
    src0=src,dest0=dest;
    src=node++,dest=node++;
    head[src]=head[dest]=-1;
    for(i=0;i<node-2;++i)
    {
        if(in[i]>0)addedge(src,i,in[i]);
        if(in[i]<0)addedge(i,dest,-in[i]);
    }
    Dinic_flow();
    addedge(dest0,src0,oo);
    Dinic_flow();
    for(i=head[src];i>=0;i=next[i])
        if(flow[i])return -1;
    for(i=head[dest0];i>=0;i=next[i])
        if(ver[i]==src0)return flow[i^1];
    return 0;
}
inline void get(int &a)
{
    char c;
    while(((c=getchar())<'0'||c>'9')&&c!='-'&&c!='+');
    if(c=='-'){a=dest;return;}
    if(c=='+'){a=src;return;}
    for(a=0;c>='0'&&c<='9';c=getchar())a=a*10+c-'0';
}
int main()
{
    int u,v,c,n,m,ans;
    while(get(n),get(m),n+m)
    {
        prepare(n+2,0,n+1);
        while(m--)
        {
            get(u),get(v),get(c);
            in[u]-=c,in[v]+=c;
            addedge(u,v,oo);
        }
        if((ans=Limit_flow())>=0)printf("%d\n",ans);
        else printf("impossible\n");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值