hdu 3157 poj 3801 Crazy Circuits(有源汇有上下界最下流)

Crazy Circuits

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


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
 



处理有源汇有上下界最大流问题是:
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 <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

void read(int &x)
{
    char ch;
    x=0;
    while(ch=getchar(),ch!=' '&&ch!='\n')
    {
        x=x*10+ch-'0';
    }
}

struct Edge
{
    int from,to,cap,flow;
    bool operator <(const Edge e) const
    {
        if(e.from!=from)  return from<e.from;
        else return to<e.to;
    }
    Edge() {}
    Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow) {}
};

struct Dinic
{
    vector<Edge> edges;
    vector<int> G[MAXN];
    bool vis[MAXN];//BFS使用
    int d[MAXN];   //从起点到i的距离
    int cur[MAXN]; //当前弧下标
    int n,m,s,t,maxflow;   //节点数 边数(包括反向弧) 源点编号和弧点编号

    void init(int n)
    {
        this->n=n;
        for(int i=0;i<=n;i++)
            G[i].clear();
        edges.clear();
    }

    void addedge(int from,int to,int cap)
    {
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool bfs()
    {
        MEM(vis,0);
        MEM(d,-1);
        queue<int> q;
        q.push(s);
        d[s]=maxflow=0;
        vis[s]=1;
        while(!q.empty())
        {
            int u=q.front(); q.pop();
            int sz=G[u].size();
            for(int i=0;i<sz;i++)
            {
                Edge e=edges[G[u][i]];
                if(!vis[e.to]&&e.cap>e.flow)
                {
                    d[e.to]=d[u]+1;
                    vis[e.to]=1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int dfs(int u,int a)
    {
        if(u==t||a==0)  return a;
        int sz=G[u].size();
        int flow=0,f;
        for(int &i=cur[u];i<sz;i++)
        {
            Edge &e=edges[G[u][i]];
            if(d[u]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
            {
                e.flow+=f;
                edges[G[u][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0)  break;
            }
        }
        return flow;
    }

    int Maxflow(int s,int t)
    {
        this->s=s; this->t=t;
        int flow=0;
        while(bfs())
        {
            MEM(cur,0);
            flow+=dfs(s,INF);
        }
        return flow;
    }
}Dic;

int low[300];

int main()
{
//    fread;
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(!n&&!m)  break;
        int s=0,t=n+1;
        int ss=n+2,tt=n+3;
        Dic.init(tt);
        MEM(low,0);
        for(int i=0;i<m;i++)
        {
            char c1[5],c2[5];
            int u,v,w;
            scanf("%s %s %d",c1,c2,&w);
            if(c1[0]=='+')
                u=s;
            else
                sscanf(c1,"%d",&u);
            if(c2[0]=='-')
                v=t;
            else
                sscanf(c2,"%d",&v);
            low[u]-=w; low[v]+=w;
            Dic.addedge(u,v,INF);//构造附加网络
        }
        int sum=0;
        for(int i=0;i<=n+1;i++)
        {
            if(low[i]>0)
            {
                Dic.addedge(ss,i,low[i]);
                sum+=low[i];
            }
            else
                Dic.addedge(i,tt,-low[i]);
        }
        int mx=Dic.Maxflow(ss,tt);
//        cout<<"mx1 "<<mx<<endl;
        Dic.addedge(t,s,INF);
        int id=Dic.edges.size()-2;
        mx+=Dic.Maxflow(ss,tt);
//        cout<<"mx2 "<<mx<<endl;
        if(mx!=sum)
            puts("impossible");
        else
            printf("%d\n",Dic.edges[id].flow);
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值