SGU 194. Reactor Cooling

前端之家收集整理的这篇文章主要介绍了SGU 194. Reactor Cooling前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

194. Reactor Cooling

time limit per test: 2 sec.
memory limit per test: 65536 KB
input: standard
output: standard


The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear reactor to produce plutonium for the nuclear bomb they are planning to create. Being the wicked computer genius of this group,you are responsible for developing the cooling system for the reactor.

The cooling system of the reactor consists of the number of pipes that special cooling liquid flows by. Pipes are connected at special points,called nodes,each pipe has the starting node and the end point. The liquid must flow by the pipe from its start point to its end point and not in the opposite direction.

Let the nodes be numbered from 1 to N. The cooling system must be designed so that the liquid is circulating by the pipes and the amount of the liquid coming to each node (in the unit of time) is equal to the amount of liquid leaving the node. That is,if we designate the amount of liquid going by the pipe from i-th node to j-th as f ij,(put f ij = 0 if there is no pipe from node i to node j),for each i the following condition must hold:


sum(j=1..N,f ij) = sum(j=1..N,f ji)


Each pipe has some finite capacity,therefore for each i and j connected by the pipe must be f ij ≤ c ij where c ij is the capacity of the pipe. To provide sufficient cooling,the amount of the liquid flowing by the pipe going from i-th to j-th nodes must be at least l ij,thus it must be f ij ≥ l ij.

Given c ij and l ij for all pipes,find the amount f ij,satisfying the conditions specified above.

Input

The first line of the input file contains the number N (1 ≤ N ≤ 200) - the number of nodes and and M — the number of pipes. The following M lines contain four integer number each - i,j,l ij and c ij each. There is at most one pipe connecting any two nodes and 0 ≤ l ij ≤ c ij ≤ 10 5 for all pipes. No pipe connects a node to itself. If there is a pipe from i-th node to j-th,there is no pipe from j-th node to i-th.

Output

On the first line of the output file print YES if there is the way to carry out reactor cooling and NO if there is none. In the first case M integers must follow,k-th number being the amount of liquid flowing by the k-th pipe. Pipes are numbered as they are given in the input file.

Sample test(s)

Input

Test #1

4 6
1 2 1 2
2 3 1 2
3 4 1 2
4 1 1 2
1 3 1 2
4 2 1 2

Test #2

4 6
1 2 1 3
2 3 1 3
3 4 1 3
4 1 1 3
1 3 1 3
4 2 1 3

Output

Test #1

NO

Test #2

YES
1
2
3
2
1
1

Author: Andrew Stankevich
Resource: Petrozavodsk Winter Trainings 2003
Date: 2003-02-06

#include<cstdio>

#include<cstring>

struct TT

{

int a,b,l,r;

} pipe[210*210];

int map[210][210];

int s,t;

int n,m,dist[210],gap[210];

int min(int a,int b)

{

if(a>b) return b;

return a;

}

int find_path(int p,int limit = 0x3f3f3f3f)

{

if(p==t) return limit;

for(int i=0;i<n;++i)

{

if(dist[p]==dist[i]+1&&map[p][i]>0)

{

int t=find_path(i,min(limit,map[p][i]));

if(t<0) return t;

if(t>0)

{

map[p][i]-=t;

map[i][p]+=t;

return t;

}

}

}

int label=n;

for(int i=0;i<n;++i)

if(map[p][i]>0) label=min(label,dist[i]+1);

if (--gap[dist[p]]==0||dist[0]>=n) return -1;

++gap[dist[p]=label];

return 0;

}

int Isap()

{

gap[s]=n;

int maxflow=0,t=0;

while((t=find_path(s))>=0) maxflow+=t;

return maxflow;

}

int main()

{

while(scanf("%d%d",&n,&m)!=EOF)

{

memset(map,sizeof(map));

memset(dist,sizeof(dist));

memset(gap,sizeof(gap));

s=0;

t=n+1;

for(int i=1; i<=m; i++)

{

scanf("%d%d%d%d",&pipe[i].a,&pipe[i].b,&pipe[i].l,&pipe[i].r);

map[pipe[i].a][pipe[i].b]=pipe[i].r-pipe[i].l;//已经知道L一定比R大

map[s][pipe[i].b]+=pipe[i].l;

map[pipe[i].a][t]+=pipe[i].l;

}

n=n+2;

bool flag=true;

int k=Isap();

for(int i=1;i<=m;i++)

if(map[s][pipe[i].b]!=0 || map[pipe[i].a][t]!=0 )

{

flag=false;

break;

}

if(!flag) printf("NO/n");

else

{

printf("YES/n");

for(int i=1;i<=m;i++)

printf("%d/n",pipe[i].l+map[pipe[i].b][pipe[i].a]);

}

}

return 0;

}

邻接表形式

#include<cstdio>

#include<cstring>

const int N=210;

const int M=510*510;

const int inf=0x7fffffff;

int head[N];

int map[N][N];

int num[N*N],ru[N*N],chu[N*N];

struct TTT

{

int a,r;

}pipe[210*210];

struct Edge

{

int v,next,w;

} edge[M];

int cnt,n,s,t;

void addedge(int u,int v,int w)

{

edge[cnt].v=v;

edge[cnt].w=w;

edge[cnt].next=head[u];

head[u]=cnt++;

edge[cnt].v=u;

edge[cnt].w=0;

edge[cnt].next=head[v];

head[v]=cnt++;

}

int sap()

{

int pre[N],cur[N],dis[N],gap[N];

int flow=0,aug=inf,u;

bool flag;

for(int i=0; i<n; i++)

{

cur[i]=head[i];

gap[i]=dis[i]=0;

}

gap[s]=n;

u=pre[s]=s;

while(dis[s]<n)

{

flag=0;

for(int &j=cur[u]; j!=-1; j=edge[j].next)

{

int v=edge[j].v;

if(edge[j].w>0&&dis[u]==dis[v]+1)

{

flag=1;

if(edge[j].w<aug) aug=edge[j].w;

pre[v]=u;

u=v;

if(u==t)

{

flow+=aug;

while(u!=s)

{

u=pre[u];

edge[cur[u]].w-=aug;

edge[cur[u]^1].w+=aug;

}

aug=inf;

}

break;

}

}

if(flag) continue;

int mindis=n;

for(int j=head[u]; j!=-1; j=edge[j].next)

{

int v=edge[j].v;

if(edge[j].w>0&&dis[v]<mindis)

{

mindis=dis[v];

cur[u]=j;

}

}

if((--gap[dis[u]])==0)

break;

gap[dis[u]=mindis+1]++;

u=pre[u];

}

return flow;

}

int main()

{

int m;

while(scanf("%d%d",&m)!=EOF)

{

s=0;

t=n+1;

cnt=0;

memset(head,-1,sizeof(head));

for(int i=1;i<=m;i++)

{

scanf("%d%d%d%d",&pipe[i].r);

num[i]=cnt+1;

addedge(pipe[i].a,pipe[i].b,pipe[i].r-pipe[i].l);

ru[i]=cnt;

addedge(s,pipe[i].l);

chu[i]=cnt;

addedge(pipe[i].a,t,pipe[i].l);

}

n=n+2;

int t=sap();

bool flag=false;

for(int i=1;i<=m;i++)

if(edge[ru[i]].w!=0||edge[chu[i]].w!=0)

{

flag=true;

break;

}

if(flag) printf("NO/n");

else

{

printf("YES/n");

for(int i=1;i<=m;i++)

printf("%d/n",pipe[i].l+edge[num[i]].w);

}

}

return 0;

}

猜你在找的React相关文章