176. Flow construction
time limit per test: 1 sec.
memory limit per test: 4096 KB
memory limit per test: 4096 KB
input: standard
output: standard
output: standard
You have given the net consisting of nodes and pipes; pipes connect the nodes. Some substance can flow by pipes, and flow speed in any pipe doesn't exceed capacity of this pipe.
The substance cannot be accumulated in the nodes. But it is being produced in the first node with the non-negative speed and being consumed with the same speed in the last node.
You have some subset taken from the set of pipes of this net. You need to start the motion of substance in the net, and your motion must fully fill the pipes of the given subset. Speed of the producing substance in the first node must be minimal.
Calculate this speed and show the scene of substance motion.
Remember that substance can't be accumulated in the nodes of the net.
The substance cannot be accumulated in the nodes. But it is being produced in the first node with the non-negative speed and being consumed with the same speed in the last node.
You have some subset taken from the set of pipes of this net. You need to start the motion of substance in the net, and your motion must fully fill the pipes of the given subset. Speed of the producing substance in the first node must be minimal.
Calculate this speed and show the scene of substance motion.
Remember that substance can't be accumulated in the nodes of the net.
Input
Two positive integer numbers N (1<=N<=100) and M have been written in the first line of the input - numbers of nodes and pipes.
There are M lines follows: each line contains four integer numbers Ui, Vi, Zi, Ci; the numbers are separated by a space. Ui is the beginning of i-th pipe, Vi is its end, Zi is a capacity of i-th pipe (1<=Zi<=10^5) and Ci is 1 if i-th pipe must be fully filled, and 0 otherwise.
Any pair of nodes can be connected only by one pipe. If there is a pipe from node A to node B, then there is no pipe from B to A. Not a single node is connected with itself.
There is no pipe which connects nodes number 1 and N. Substance can flow only from the beginning of a pipe to its end.
There are M lines follows: each line contains four integer numbers Ui, Vi, Zi, Ci; the numbers are separated by a space. Ui is the beginning of i-th pipe, Vi is its end, Zi is a capacity of i-th pipe (1<=Zi<=10^5) and Ci is 1 if i-th pipe must be fully filled, and 0 otherwise.
Any pair of nodes can be connected only by one pipe. If there is a pipe from node A to node B, then there is no pipe from B to A. Not a single node is connected with itself.
There is no pipe which connects nodes number 1 and N. Substance can flow only from the beginning of a pipe to its end.
Output
Write one integer number in the first line of the output - it ought to be the minimal speed of the producing substance in the first node.
Write M integers in the second line - i-th number ought to be the flow speed in the i-th pipe (numbering of pipes is equal to the input).
If it is impossible to fill the given subset, write "Impossible".
Write M integers in the second line - i-th number ought to be the flow speed in the i-th pipe (numbering of pipes is equal to the input).
If it is impossible to fill the given subset, write "Impossible".
Sample test(s)
Input
Input 1: 4 4 1 2 2 0 2 4 1 1 1 3 2 1 3 4 3 0 Input 2: 4 4 1 2 1 0 2 4 2 1 1 3 3 1 3 4 2 0
Output
Output 1:
3
1 1 2 2
Output 2:
Impossible
3
1 1 2 2
Output 2:
Impossible
分析:这题为有上下界的最小流算法,完全不懂啊,学了一下,还是不大理解,求神牛指教啊。。。
转傻崽构图:
经典题目,有上下界的最小流,做法入下
1.in[v]表示以v为终点的边的下界之和,out[u]表示以u为起点的边的下界之和
2.虚拟出ss,tt,连(ss,in[v])和(out[u],tt)
3.做一次maxflow(ss,tt)
4.加一条t->s的inf边
5.再做一次maxflow(ss,tt)
6.如果两次maxflow之和 < in之和,则不存在可行流
7.最后答案为f[t -> s]既inf – 残余[t -> s]
===============
为什么要按红色部分来呢,根据胡伯涛的论文,貌似是先加t->s的inf边,然后最大流,然后去掉无关的边,再反向最大流,可是这样跑到第6组就跑不动了。。。。
求解释。。。
尽管AC了,还是不解啊!!!
#include<cstdio>
using namespace std;
const int mm=11111;
const int mn=111;
const int oo=1000000000;
int node,src,dest,edge,n,m;
int ver[mm],flow[mm],ans[mm],id[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn],in[mn];
inline int min(int a,int b)
{
return a<b?a:b;
}
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;
edge=0;
}
inline void addedge(int u,int v,int c,int ID)
{
ver[edge]=v,flow[edge]=c,id[edge]=ID,next[edge]=head[u],head[u]=edge++;
ver[edge]=u,flow[edge]=0,id[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool Dinic_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]>0&&dis[v=ver[i]]<0)
{
dis[q[r++]=v]=dis[u]+1;
if(v==dest)return 1;
}
return 0;
}
int Dinic_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]>0&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
{
flow[i]-=tmp;
flow[i^1]+=tmp;
return tmp;
}
return 0;
}
void Dinic_flow()
{
while(Dinic_bfs())
{
for(int i=0;i<node;++i)work[i]=head[i];
while(Dinic_dfs(src,oo));
}
}
void limit_min_flow()
{
int i,src0,dest0,edge0,ret=0;
src0=src,dest0=dest,edge0=edge;
src=node,dest=node+1;
head[src]=head[dest]=-1;
for(i=1;i<node;++i)
if(in[i]>0)addedge(src,i,in[i],0);
else addedge(i,dest,-in[i],0);
node+=2;
Dinic_flow();
addedge(dest0,src0,oo,0);
Dinic_flow();
for(i=head[src];i>=0;i=next[i])
if(flow[i]>0)
{
printf("Impossible\n");
return;
}
for(i=head[dest0];i>=0;i=next[i])
if(ver[i]==src0)break;
if(i<0)
{
printf("Impossible\n");
return;
}
ret=flow[i^1];
printf("%d\n",ret);
for(i=0;i<edge0;++i)ans[id[i]]=flow[i^1];
for(i=1;i<=m;++i)printf("%d ",ans[i]);
printf("\n");
}
int main()
{
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
int i,u,v,k,c;
while(scanf("%d%d",&n,&m)!=-1)
{
for(i=0;i<=n;++i)in[i]=0;
prepare(n+1,1,n);
for(i=1;i<=m;++i)
{
scanf("%d%d%d%d",&u,&v,&c,&k);
if(k)in[u]-=c,in[v]+=c,ans[i]=c;
else addedge(u,v,c,i);
}
limit_min_flow();
}
return 0;
}