Crazy circuits (hdu 3157 flow with minimum flow at upper and lower limits)

Source: Internet
Author: User

Crazy circuitsTime limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 611 Accepted Submission (s): 308


Problem Descriptionyou ' ve just built a circuit board for your new robot, and now your need to power it. Your robot circuit consists of a number of electrical components the require a certain amount of current to operate. Every component has a + and a-lead, which is connected on the circuit board at junctions. Current flows through the component from + to-(but note that a component does isn't "use up" of the current:everything that Comes in through the + End goes out the-end).

The junctions on the board is labeled 1, ..., N, except for the special junctions labeled + And-where The power supply terminals is connected. The + terminal only connects + leads, and the-terminal only connects-leads. All current this enters a junction from the-leads of connected components exits through connected + leads, but is a ble to control how much-flows to each connected + leads at every junction (though methods for doing so is beyond T He scope of this problem1). Moreover, you know you had assembled the circuit in such a to that there is no feedback loops (components chained in a manner that allows-current-to-flow in a loop).


Figure 1: Examples of valid circuit diagrams.
In (a), all of the can is powered along directed paths from the positive terminal to the negative terminal.
In (b), 4 and 6 cannot are powered, since there is no directed path from junction 4 to the negative terminal.
In the interest of saving power, and also to ensure this your circuit does not overheat, you would like to use as little C Urrent as possible to get your robot to work. What's the smallest amount of current so you need to put through the + terminal (which you can imagine all necessarily Leaving through the-terminal) so, every component on your robot receives it required supply of current to function?

Hint1 for those who is electronics-inclined, imagine, the ability to adjust the potential on any Componentwithou T altering its current requirement, or equivalently this there is a accurate variable potentiometer connected in series W ith each component so you can adjust. Your power supply'll has ample potential for the circuit.

Inputthe input file would contain multiple test cases. Each test case, begins with a, containing, integers: N(0 <= N<=), the number of junctions not including the positive and negative terminals, and M(1 <= M<=), the number of the components in the circuit diagram. The next MLines 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 are connected, and an integer Ii(1 <= Ii<=), the minimum amount of the current required for component Ito function. The junctions Piand niis specified as either the character ' + ' indicating the positive terminal, the character '-' indicating the negative term Inal, or an integer (between 1 and N) indicating one of the numbered junctions. No of the same positive junction and the same negative junction. The end-of-file is denoted by a invalid test case with N= M= 0 and should not being processed.
Outputfor each input test case, your program should print off either a single integer indicating the minimum amount of cur Rent that must being supplied at the positive terminal in order to ensure that every component is powered, or the message " Impossible"If there is no-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

Source2008 ACM-ICPC Pacific Northwest Region
Recommendchenrui | We have carefully selected several similar problems for you:3155 3154 3151 3156 3158

Test instructions: There are two positive and negative n nodes and M components, each of which tells the endpoint which node to be connected to, and each component has a minimum current limit of work, and asks what is the minimum current that satisfies the condition for all components to work.

Idea: The topic already has the source point S and the meeting point T, and then add additional source point SS and the meeting point TT, the original edge of the capacity of the INF (because there is no upper limit), if the in[i]>0 SS to I edge capacity of in[i], if in[i]<0 I to the TT edge capacity of-in[i], Run the maximum flow, and then T to the S to the edge of the INF, and then run the maximum flow, to determine whether the SS out of the full flow, if the flow is no solution, otherwise there is output edge (t,s) flow.

In addition, this kind of question looks at the explanation here: Click Open link

Code:

#include <iostream> #include <functional> #include <cstdio> #include <cstring> #include < algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include < vector> #include <set> #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define Pi  ACOs ( -1.0) #define EPS 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A; I <= b;  i++) #define FREE (i,a,b) for (i = A, I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A i > B; i--) #define MEM (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d         ", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d ", &a, &b, &c) #define PF Printf#define DBG PF ("hi\n") typedef long long ll;using namespace std; #define INF 0x3f3f3f3f#define mod 1000000009const int maxn = 1005;co NST int MAXN = 2005;const int MAXM = 200010;struct edge{int to,next,cap,flow;} Edge[maxm];int tol;int head[maxn];int gap[maxn],dep[maxn],pre[maxn],cur[maxn];int n,m;char s1[10],s2[10];int In[MAXN    ];int s,t,ss,tt;void init () {tol=0;    memset (head,-1,sizeof (head)); memset (In,0,sizeof (in));     Add edge, one-way figure three parameters, bidirectional figure four parameters void Addedge (int u,int v,int w,int rw=0) {edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u]; edge[tol].flow=0;    head[u]=tol++; Edge[tol].to=u; EDGE[TOL].CAP=RW;    EDGE[TOL].NEXT=HEAD[V]; edge[tol].flow=0; head[v]=tol++;}    Input parameters: Starting point, end point, number of points//point number has no effect, as long as the total number of input points int sap (int start,int end,int N) {memset (gap,0,sizeof (GAP));    memset (dep,0,sizeof (DEP));    memcpy (cur,head,sizeof (head));    int U=start;    Pre[u]=-1;    Gap[0]=n;    int ans=0;            while (dep[start]<n) {if (u==end) {int min=inf; for (int i=pre[u];i!=-1;i=pre[edge[i^1].to]) if (min>edge[i].cap-edge[i].flow) min=ed           Ge[i].cap-edge[i].flow; for (int i=pre[u];i!=-1;i=pre[edge[i^1].to]) {edge[i].flow+=min;            Edge[i^1].flow-=min;            } U=start;            Ans+=min;        Continue        } bool Flag=false;        int V;            for (int i=cur[u];i!=-1;i=edge[i].next) {v=edge[i].to;                if (Edge[i].cap-edge[i].flow && dep[v]+1==dep[u]) {flag=true;                Cur[u]=pre[v]=i;            Break            }} if (flag) {u=v;        Continue        } int min=n;            for (int i=head[u];i!=-1;i=edge[i].next) if (Edge[i].cap-edge[i].flow && dep[edge[i].to]<min)                {min=dep[edge[i].to];            Cur[u]=i;        } gap[dep[u]]--;        if (!gap[dep[u]]) return ans;        dep[u]=min+1;        gap[dep[u]]++;    if (U!=start) u=edge[pre[u]^1].to; } return ans; void Solve () {for (int i=0;i<=n+1;i++)        {if (in[i]>0) Addedge (Ss,i,in[i]);    if (in[i]<0) Addedge (I,tt,-in[i]);    } SAP (SS,TT,N+4);    Addedge (T,s,inf);    SAP (SS,TT,N+4);        for (int i=head[ss];~i;i=edge[i].next) if (edge[i].cap-edge[i].flow>0) {printf ("impossible\n");    return; } printf ("%d\n", Edge[head[t]].flow);}    int main () {#ifndef Online_judge freopen ("C:/users/lyf/desktop/in.txt", "R", stdin), #endif int i,j,u,v,w;        while (scanf ("%d%d", &n,&m) && (n+m)) {init ();        s=0,t=n+1,ss=n+2,tt=n+3;            for (i=0;i<m;i++) {scanf ("%s%s%d", s1,s2,&w);            if (s1[0]== ' + ') u=s;                else {u=0;            For (J=0;j<strlen (S1); j + +) u=u*10+s1[j]-' 0 ';            } if (s2[0]== '-') v=t;                else {v=0;       for (J=0;j<strlen (S2); j + +) v=v*10+s2[j]-' 0 ';     } in[v]+=w;            In[u]-=w;        Addedge (U,v,inf);    } solve (); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Crazy circuits (hdu 3157 flow with minimum flow at upper and lower limits)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.