HDU3157 Crazy Circuits 上下界的最小流

构造附加网络,然后求一次最大流,再添加(t,s,INF),在求一次最大流,如果满流那么(t,s)的流量就是最小流,否则不存在可行流。

poj上面也有这题。。不过题目有问题,只要按照sample输出就可以过了。。。绝对有问题

Crazy Circuits

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


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
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

#define MAXN  20010
#define MAXM  500010
#define INF    0x3FFFFFF

struct edge
{
    int to,c,next;
};

edge e[MAXM];
int head[MAXN],en,vn;
int n,m; //n为点数 m为边数
int dis[MAXN];
int gap[MAXN];
int st, ed;
int in[MAXN];  

void add(int a, int b, int c) 
{
    e[en].to=b;
    e[en].c=c;
    e[en].next=head[a];
    head[a]=en++;
    e[en].to=a;
    e[en].c=0;
    e[en].next=head[b];
    head[b]=en++;
}

int isap(int u,int flow) 
{
    if(u==ed)
		return flow;
    int j,mindis=vn-1,t=flow,d;
    for(j=head[u];j!=-1;j=e[j].next) 
	{
        int v=e[j].to,val=e[j].c;
        if(val>0)
		 {
            if(dis[v]+1==dis[u])
			 {
                if(t<e[j].c) 
                    d=t;
				else 
                    d=e[j].c;
                d=isap(v,d);
                e[j].c-=d,e[j^1].c+=d;
                t-=d;
                if(dis[st]>=vn)
                    return flow-t;
                if(t==0) 
                    break;
            }
            if(dis[v]<mindis)
                mindis=dis[v];
        }
    }
    if(t==flow) 
	{
        --gap[dis[u]];
        if (gap[dis[u]]==0) 
            dis[st]=vn;
        dis[u]=mindis+1;
        ++gap[dis[u]];
    }
    return flow-t;
}
 
int maxflow() 
{
    int ret=0;
    memset(gap,0,sizeof(gap));
    memset(dis,0,sizeof(dis));
    gap[0]=vn;
    while (dis[st]<vn)
        ret+=isap(st, INF);
    return ret;
}

int limitflow()  
{ 
    int i,st0,ed0,en0,ret=0;
    st0=st,ed0=ed,en0;
    st=vn++,ed=vn++;  
    head[st]=head[ed]=-1;
    for(i=0;i<vn-2;++i)
    {
        if(in[i]>0)
			add(st,i,in[i]);
        if(in[i]<0)
			add(i,ed,-in[i]);
    }
    //add(ed0,st0,INF);
    maxflow(); 
    en0=en;
    add(ed0,st0,INF);
    maxflow(); 
    for(i=head[st];i!=-1;i=e[i].next)
        if(e[i].c)
			return -1; 	
	ret=e[en0^1].c;
    return ret;
}

void solve()
{
	char a[10],b[10];
	int u,v,l;
	memset(in,0,sizeof(in));
	memset(head,-1,sizeof(head));
	st=0,ed=n+1,en=0,vn=n+2;
	for(int i=0;i<m;i++)
	{
		scanf("%s%s%d",a,b,&l);
		if(a[0]=='+')
			u=st;
		else
		{
			u=0;
			for(int i=0;a[i]!='\0';u=u*10+(a[i++]-'0')) ;
		} 
		if(b[0]=='-')
			v=ed;
		else
		{
			v=0;
			for(int i=0;b[i]!='\0';v=v*10+(b[i++]-'0')) ;
		}
		in[u]-=l,in[v]+=l;
		add(u,v,INF);  //此处没有上限 
	}
	int ret=limitflow();
	if(ret==-1)
		printf("impossible\n");
	else
		printf("%d\n",ret);
}

int main()
{
	while(scanf("%d%d",&n,&m)!=EOF && n+m)
		solve();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值