SGU 194. Reactor Cooling
正文
194. Reactor Cooling
time limit per test: 0.5 sec.
memory limit per test: 65536 KB
memory limit per test: 65536 KB
input: standard
output: 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 fij, (put fij = 0 if there is no pipe from node i to node j), for each i the following condition must hold:
sum(j=1..N, fij) = sum(j=1..N, fji)
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.
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:
sum(j=1..N, fij) = sum(j=1..N, fji)
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.
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 cijeach. There is at most one pipe connecting any two nodes and 0 ≤ lij ≤ cij ≤ 105 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.
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.
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
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
NO
Test #2
YES
1
2
3
2
1
1
分析
无源汇上下界可行流模板题。
每条边的流量就是这条边的反向边的流量+流量下界。
code
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<cmath> 5 #include<iostream> 6 7 using namespace std; 8 9 const int N = 20100; 10 const int INF = 1e9; 11 12 struct Edge{ 13 int to,nxt,c; 14 }e[500100]; 15 int head[N],dis[N],cur[N],q[N]; 16 int M[N],id[500100],B[500100]; // id存第i条边的编号 17 int n,m,SS,TT,L,R,Sum,tot; 18 19 inline int read() { 20 int x = 0,f = 1;char ch = getchar(); 21 for (; ch<'0'||ch>'9'; ch = getchar()) if (ch=='-') f = -1; 22 for (; ch>='0'&&ch<='9'; ch = getchar()) x = x * 10 + ch - '0'; 23 return x * f; 24 } 25 void add_edge(int u,int v,int c) { 26 e[++tot].to = v;e[tot].c = c;e[tot].nxt= head[u];head[u] = tot; 27 } 28 void init() { 29 tot = 1;SS = n+1;TT = n+2;Sum = 0; 30 } 31 bool bfs() { 32 for (int i=1; i<=n+2; ++i) { 33 cur[i] = head[i],dis[i] = -1; 34 } 35 L = 1;R = 0; 36 q[++R] = SS; 37 dis[SS] = 0; 38 while (L <= R) { 39 int u = q[L++]; 40 for (int i=head[u]; i; i=e[i].nxt) { 41 int v = e[i].to; 42 if (dis[v] == -1 && e[i].c > 0) { 43 dis[v] = dis[u] + 1; 44 q[++R] = v; 45 if (v == TT) return true; 46 } 47 } 48 } 49 return false; 50 } 51 int dfs(int u,int flow) { 52 if (u == TT) return flow; 53 int used = 0; 54 for (int &i=cur[u]; i; i=e[i].nxt) { 55 int v = e[i].to; 56 if (dis[v] == dis[u] + 1 && e[i].c > 0) { 57 int tmp = dfs(v,min(e[i].c,flow-used)); 58 if (tmp > 0) { 59 e[i].c -= tmp;e[i^1].c += tmp; 60 used += tmp; 61 if (used == flow) break; 62 } 63 } 64 65 } 66 if (used != flow) dis[u] = -1; 67 return used; 68 } 69 int dinic() { 70 int ans = 0; 71 while (bfs()) ans += dfs(SS,INF); 72 return ans; 73 } 74 75 int main() { 76 while (~scanf("%d%d",&n,&m)) { 77 init(); 78 for (int i=1; i<=m; ++i) { 79 int u = read(),v = read(),b = read(),c = read(); 80 add_edge(u,v,c-b); 81 id[i] = tot; 82 add_edge(v,u,0); 83 M[u] -= b;M[v] += b; 84 B[i] = b; 85 } 86 for (int i=1; i<=n; ++i) { 87 if (M[i] > 0) add_edge(SS,i,M[i]),add_edge(i,SS,0),Sum += M[i]; 88 if (M[i] < 0) add_edge(i,TT,-M[i]),add_edge(TT,i,0); 89 } 90 int ans = dinic(); 91 if (ans == Sum) { 92 puts("YES"); 93 for (int i=1; i<=m; ++i) 94 printf("%d\n",e[id[i] ^ 1].c + B[i]); 95 } 96 else puts("NO"); 97 } 98 return 0; 99 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 历时 8 年,我冲上开源榜前 8 了!
· 物流快递公司核心技术能力-海量大数据处理技术
· 四大AI编程工具组合测评
· 关于能否用DeepSeek做危险的事情,DeepSeek本身给出了答案
· 如何在 Github 上获得 1000 star?