• 有源汇点有上下界的最小流(电路问题)


    http://acm.hdu.edu.cn/showproblem.php?pid=3157

    Crazy Circuits

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

    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 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).


    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, andM (1 <=M <= 200), the number of components in the circuit diagram. The nextM lines each contain a description of some component in the diagram. Theith 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 integerIi (1 <=Ii <= 100), the minimum amount of current required for componenti to function. The junctionspi and ni are specified as either the character '+' indicating the positive terminal, the character '-' indicating the negative terminal, or an integer (between 1 andN) 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 withN =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


    题意:给出一个n和m分别代表n个焊点(1,2,3,4,5,……表示)和m个电器元件,另外还有一个正电极和一个负电极,每个用电器都有一个启动电流,就是说要保证流过该用电器的流量大于等于该电流I,在满足电流守恒的原则下问正电极至少要提供多少的流量(电流)才能使整个系统正常运行;

    分析,这是有上下界流量的最小流

    程序:

    #include"stdio.h"
    #include"string.h"
    #include"queue"
    #include"stdlib.h"
    #include"iostream"
    #include"algorithm"
    #define M 300
    #define inf 100000000
    using namespace std;
    struct node
    {
        int u,v,w,c,next;
    }edge[M*M];
    int t,head[M],work[M],dis[M];
    void init()
    {
        t=0;
        memset(head,-1,sizeof(head));
    }
    void add(int u,int v,int w,int c)
    {
        edge[t].u=u;
        edge[t].v=v;
        edge[t].w=w;
        edge[t].c=c;
        edge[t].next=head[u];
        head[u]=t++;
    
        edge[t].u=v;
        edge[t].v=u;
        edge[t].w=0;
        edge[t].c=c;
        edge[t].next=head[v];
        head[v]=t++;
    }
    int bfs(int start,int endl)
    {
        queue<int>q;
        memset(dis,-1,sizeof(dis));
        q.push(start);
        dis[start]=0;
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            for(int i=head[u];i!=-1;i=edge[i].next)
            {
                int v=edge[i].v;
                if(edge[i].w&&dis[v]==-1)
                {
                    dis[v]=dis[u]+1;
                    q.push(v);
                    if(v==endl)
                        return 1;
                }
            }
        }
        return 0;
    }
    int dfs(int u,int a,int endl)
    {
        if(u==endl)
            return a;
        for(int &i=work[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(edge[i].w&&dis[v]==dis[u]+1)
            {
                int tt=dfs(v,min(a,edge[i].w),endl);
                if(tt)
                {
                    edge[i].w-=tt;
                    edge[i^1].w+=tt;
                    return tt;
                }
            }
        }
        return 0;
    }
    int Dinic(int start,int endl)
    {
        int ans=0;
        while(bfs(start,endl))
        {
            memcpy(work,head,sizeof(head));
            while(int tt=dfs(start,inf,endl))
                ans+=tt;
        }
        return ans;
    }
    int val[M*M],id[M*M];
    int main()
    {
        int n,m,i,j;
        while(scanf("%d%d",&n,&m),m||n)
        {
            init();
            char xx[5],yy[5];
            int x,y,z;
            int st=n+2;
            int sd=n+3;
            int source=0;
            int sink=n+1;
            int R=inf;//上限初始化为无限大
            int sum=0;
            for(j=1;j<=m;j++)
            {
                scanf("%s%s%d",xx,yy,&z);
                if(xx[0]=='+')
                    x=source;
                else if(xx[0]=='-')
                    x=sink;
                else
                {
                    int len=strlen(xx);
                    int ss=0;
                    for(i=0;i<len;i++)
                        ss=ss*10+xx[i]-'0';
                    x=ss;
                }
    
                if(yy[0]=='+')
                    y=source;
                else if(yy[0]=='-')
                    y=sink;
                else
                {
                    int len=strlen(yy);
                    int ss=0;
                    for(i=0;i<len;i++)
                        ss=ss*10+yy[i]-'0';
                    y=ss;
                }
                add(x,y,R-z,R);
                id[j]=t-2;
                add(x,sd,z,0);
                add(st,y,z,0);
                sum+=z;
                val[j]=z;
            }
            int ans1=Dinic(st,sd);
            add(sink,source,inf,0);
            int ans2=Dinic(st,sd);
            //printf("%d %d %d
    ",ans1,ans2,sum);
            if(ans1+ans2!=sum)
                printf("impossible
    ");
            else
                printf("%d
    ",edge[t-1].w);
        }
        return 0;
    }
    


  • 相关阅读:
    HDU 4024 Dwarven Sniper’s hunting(数学公式 或者是二分)
    二分图最大匹配总结
    HDU 4022 Bombing (STL应用)
    HDU 1847 Good Luck in CET4 Everybody!(组合博弈)
    HDU 1556 Color the ball(树状数组)
    HDU 4023 Game(博弈)
    HDU 1406 完数(水题)
    HDU 4021 24 Puzzle
    Oracle 多表查询优化
    【编程之美】字符串移位包含的问题(续)
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348184.html
Copyright © 2020-2023  润新知