176. Flow construction
time limit per test: 0.5 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
管道中的流只能从开始节点到终点
管道有容量 其中的流量有一个限制 就是限制其中的流量是否是满流 如果要求满流 该管道流量=容量
求最小流量
如果有最小流量 输出每个管道的流量
主要是在限制流量=容量时 一开始不知道如何处理
流量=容量时 low=容量 需要改变两个节点low数组中的值 由于要记录流过管道的流量
所以在建边时 把边中cap flow的值都设置为容量的值
没有限制时 low=0 high=容量 所以节点low数组中的值不要改变 权值为容量(-0)
后面的就是常规的了
#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,int flow)
{
edges.push_back(Edge(from,to,cap,flow));
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[200];
int main()
{
// fread;
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int s=1,t=n;
int ss=n+2,tt=n+3;
Dic.init(tt);
MEM(low,0);
for(int i=0;i<m;i++)
{
int u,v,w,c;
scanf("%d%d%d%d",&u,&v,&w,&c);
if(c)//满流
{
low[u]-=w; low[v]+=w;
Dic.addedge(u,v,w,w);
}
else//不需要满流 low=0
{
Dic.addedge(u,v,w,0);
}
}
int sum=0;
for(int i=1;i<=n;i++)
{
if(low[i]>0)
{
sum+=low[i];
Dic.addedge(ss,i,low[i],0);
}
else
Dic.addedge(i,tt,-low[i],0);
}
int mx=Dic.Maxflow(ss,tt);
Dic.addedge(t,s,INF,0);
mx+=Dic.Maxflow(ss,tt);
if(mx!=sum)
puts("Impossible");
else
{
int id=Dic.edges.size()-2;
printf("%d\n",Dic.edges[id].flow);
for(int i=0;i<m;i++)
{
if(i==0)
printf("%d",Dic.edges[i*2].flow);
else printf(" %d",Dic.edges[i*2].flow);
}
printf("\n");
}
}
return 0;
}