ZOJ 2314 Reactor Cooling

来源:互联网 发布:怎么找文献的数据 编辑:程序博客网 时间:2024/05/01 06:19
题目链接 : ZOJ2314
Reactor Cooling

 


 

Time Limit: 5 Seconds                                    Memory Limit:32768 KB                                                    Special Judge                            

 


 

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 fij, (put fij = 0 if there is no pipe from node i to node j), for each i the following condition must hold:

fi,1+fi,2+...+fi,N = f1,i+f2,i+...+fN,i

Each pipe has some finite capacity, therefore for each i and j connected by the pipe must be fij <= cij where cij 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 lij, thus it must be fij >= lij.

Given cij and lij for all pipes, find the amount fij, satisfying the conditions specified above.


 

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


 

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, lij and cij each. There is at most one pipe connecting any two nodes and 0 <= lij <= cij <= 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 Input

2

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

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


 

Sample Input

NO

YES
1
2
3
2
1
1

 


 


                            Author: Andrew Stankevich
                            Source: Andrew Stankevich's Contest #1

 

 

上下界最大流类型的第一个题

这个题是求无源汇点的一个可行流 用了4中不同的最大流来求 

BFS增广  530 ms

ISAP 20 ms

预流推进FIFO 50 ms

预流推进最高标号 80 ms (不知道是不是写挫了。。)

 

就只贴ISAP的代码了

 

#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>#include<string>#include<vector>#include<map>#include<set>#include<queue>using namespace std;int n,m;int now[205];vector<int> gra[205];int d[205],cont[205];int cap[205][205];int pre[205],cur[205];bool vis[205];void bfs(int t,int s){    int x,to;    queue<int> que;    for(int i=0;i<n;i++) vis[i]=cont[i]=cur[i]=0,d[i]=0x3fffffff;    d[t]=0;cont[0]=1;    que.push(t);vis[t]=1;    while(!que.empty())    {        x=que.front();que.pop();        for(int i=0;i<gra[x].size();i++)        {            to=gra[x][i];            if(vis[to]==0 && cap[to][x])            {                d[to]=d[x]+1;                vis[to]=1;                que.push(to);                cont[d[to]]++;            }        }    }}int SAP(int s,int t){if(s==t) return 0x7fffffff;    bfs(t,s);    int ans=0,x=s,y,flow,back,temp;    while(d[s]<n)    {        y=-1;        if(x==73)            int kk=0;        for(int i=cur[x];i<gra[x].size();i++)            if(cap[x][gra[x][i]] && d[x]==d[gra[x][i]]+1)            {                y=gra[x][i];                cur[x]=i;                break;            }        if(y!=-1)        {            pre[y]=x;            x=y;            if(x==t)            {                flow=0x3fffffff;                for(;y!=s;y=pre[y])                    if(flow>cap[pre[y]][y]) flow=cap[pre[y]][y],back=pre[y];                for(;x!=s;x=pre[x])                    cap[x][pre[x]]+=flow,cap[pre[x]][x]-=flow;                ans+=flow;                x=back;            }        }        else        {            y=n;            for(int i=0;i<gra[x].size();i++)                if(cap[x][gra[x][i]] && y>d[gra[x][i]])                    y=d[gra[x][i]],cur[x]=i;            cont[d[x]]--;            if(cont[d[x]]==0) break;            cont[y+1]++;            d[x]=y+1;            if(x!=s) x=pre[x];        }    }    return ans;}int fa[40005][3];int main(){    int t;    scanf("%d",&t);    while(t--)    {        int x,y,l,r;        scanf("%d%d",&n,&m);        memset(now,0,sizeof(now));        memset(cap,0,sizeof(cap));        for(int i=1;i<n+3;i++) gra[i].clear();        for(int i=0;i<m;i++)        {            scanf("%d%d%d%d",&x,&y,&l,&r);            cap[x][y]=r-l;            gra[x].push_back(y);            gra[y].push_back(x);            now[x]-=l;now[y]+=l;            fa[i][0]=x;fa[i][1]=y;fa[i][2]=r;        }        int sum=0;        for(int i=1;i<=n;i++)            if(now[i]>0) gra[n+1].push_back(i),gra[i].push_back(n+1),cap[n+1][i]=now[i],sum+=now[i];            else if(now[i]<0) gra[i].push_back(n+2),gra[n+2].push_back(i),cap[i][n+2]=-now[i];        n+=2;        if(SAP(n-1,n)==sum)        {            printf("YES\n");            for(int i=0;i<m;i++)                printf("%d\n",fa[i][2]-cap[fa[i][0]][fa[i][1]]);        }        else printf("NO\n");        putchar(10);    }    return 0;}


 

原创粉丝点击