博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【 POJ - 3801】Crazy Circuits(有源汇、上下界最小流)
阅读量:5161 次
发布时间:2019-06-13

本文共 6064 字,大约阅读时间需要 20 分钟。

Description

You’ve just built a circuit board for your new robot, and now you need to power it. Your robot circuit consists of a number of electrical components that each require a certain amount of current to operate. Every component has a + and a − lead, which are connected on the circuit board at junctions. Current flows through the component from + to − (but note that a component does not “use up” the current: everything that comes in through the + end goes out the − end). 
The junctions on the board are labeled 1, ..., N, except for two special junctions labeled + and − where the power supply terminals are connected. The + terminal only connects + leads, and the − terminal only connects − leads. All current that enters a junction from the − leads of connected components exits through connected + leads, but you are able to control how much current flows to each connected + lead at every junction (though methods for doing so are beyond the scope of this problem). Moreover, you know you have assembled the circuit in such a way that there are no feedback loops (components chained in a manner that allows current to flow in a loop). 
Figure 1: Examples of two valid circuit diagrams. In (a), all components can be powered along directed paths from the positive terminal to the negative terminal. In (b), components 4 and 6 cannot be powered, since there is no directed path from junction 4 to the negative terminal. 
In the interest of saving power, and also to ensure that your circuit does not overheat, you would like to use as little current as possible to get your robot to work. What is the smallest amount of current that you need to put through the + terminal (which you can imagine all necessarily leaving through the − terminal) so that every component on your robot receives its required supply of current to function?

Input

The input file will contain multiple test cases. Each test case begins with a single line containing two integers: N (0 ≤ N ≤ 50), the number of junctions not including the positive and negative terminals, and M (1 ≤ M ≤ 200), the number of components in the circuit diagram. The next M lines each contain a description of some component in the diagram. The ith component description contains three fields: pi, the positive junction to which the component is connected, ni, the negative junction to which the component is connected, and an integer Ii (1 ≤ Ii ≤ 100), the minimum amount of current required for component i to function. The junctions pi and ni are specified as either the character ‘+’ indicating the positive terminal, the character ‘-’ indicating the negative terminal, or an integer (between 1 and N) indicating one of the numbered junctions. No two components have the same positive junction and the same negative junction. The end-of-file is denoted by an invalid test case with N = M = 0 and should not be processed.

Output

For each input test case, your program should print out either a single integer indicating the minimum amount of current that must be supplied at the positive terminal in order to ensure that every component is powered, or the message “impossible” if there is no way to direct a sufficient amount of current to each component simultaneously.

 

Sample Input

6 10 + 1 1 1 2 1 1 3 2 2 4 5 + - 1 4 3 2 3 5 5 4 6 2 5 - 1 6 5 3 4 6 + 1 8 1 2 4 1 3 5 2 4 6 3 - 1 3 4 3 0 0

Sample Output

9impossible

Hint

For those who are electronics-inclined, imagine that you have the ability to adjust the potential on any component without altering its current requirement, or equivalently that there is an accurate variable potentiometer connected in series with each component that you can adjust. Your power supply will have ample potential for the circuit.
 
 
【分析】
  就是一个有源汇、有上下界的最小流。汇点源点连一条INF的边变成循环流,新增超级源点、超级汇点,拆边,跑最大流判满流。这题不用拆边再反过来跑一遍求最小流,因为所有边的上届都是INF。
 
代码如下:
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 #define Maxn 1010 9 #define Maxm 100100 10 #define INF 0xfffffff 11 12 char s[110]; 13 int stt,edd; 14 int first[Maxn],dis[Maxn]; 15 int sum; 16 17 struct node 18 { 19 int x,y,f,o,next; 20 }t[Maxm];int len; 21 22 int mymin(int x,int y) { return x
q; 41 bool bfs(int st,int ed) 42 { 43 while(!q.empty()) q.pop(); 44 memset(dis,-1,sizeof(dis)); 45 q.push(st);dis[st]=0; 46 while(!q.empty()) 47 { 48 int x=q.front(); 49 for(int i=first[x];i;i=t[i].next) if(t[i].f>0) 50 { 51 int y=t[i].y; 52 if(dis[y]==-1) 53 { 54 dis[y]=dis[x]+1; 55 q.push(y); 56 } 57 } 58 q.pop(); 59 } 60 if(dis[ed]!=-1) return 1; 61 return 0; 62 } 63 64 int ffind(int x,int ed,int flow) 65 { 66 if(x==ed) return flow; 67 int now=0; 68 for(int i=first[x];i;i=t[i].next) if(t[i].f>0) 69 { 70 int y=t[i].y; 71 if(dis[y]==dis[x]+1) 72 { 73 int a=ffind(y,ed,mymin(flow-now,t[i].f)); 74 t[i].f-=a; 75 t[t[i].o].f+=a; 76 now+=a; 77 if(now==flow) break; 78 } 79 } 80 if(now==0) dis[x]=-1; 81 return now; 82 } 83 84 int max_flow(int st,int ed) 85 { 86 int ans=0; 87 while(bfs(st,ed)) 88 { 89 ans+=ffind(st,ed,INF); 90 } 91 if(ans!=sum) return -1; 92 return ans; 93 } 94 95 int main() 96 { 97 int n,m; 98 while(1) 99 {100 scanf("%d%d",&n,&m);101 if(n==0&&m==0) break;102 int st=n+1,ed=st+1;len=0;103 stt=ed+1;edd=stt+1;sum=0;104 memset(first,0,sizeof(first));105 for(int i=1;i<=m;i++)106 {107 int x,y;108 scanf("%s",s);109 if(s[0]=='+') x=st;110 else if(s[0]=='-') x=ed;111 else112 {113 int now=0;x=0;114 while(s[now]>='0'&&s[now]<='9') x=x*10+s[now++]-'0'; 115 }scanf("%s",s);116 if(s[0]=='+') y=st;117 else if(s[0]=='-') y=ed;118 else119 {120 int now=0;y=0;121 while(s[now]>='0'&&s[now]<='9') y=y*10+s[now++]-'0'; 122 }123 int c;124 scanf("%d",&c);125 make_edge(x,y,c,INF);126 }127 int id=len+2;128 ins(ed,st,INF);129 int now=max_flow(stt,edd);130 if(now==-1) printf("impossible\n");131 else132 {133 printf("%d\n",t[id].f);134 }135 }136 return 0;137 }
[POJ3801]

 

2016-06-13 13:12:16

转载于:https://www.cnblogs.com/Konjakmoyu/p/5575593.html

你可能感兴趣的文章
机器学习数学【1】
查看>>
Problem E: Automatic Editing
查看>>
Java数组排序
查看>>
SpringBoot 使用 MyBatis 分页插件 PageHelper 进行分页查询
查看>>
《DSP using MATLAB》Problem 6.17
查看>>
微信公众平台开发实战Java版之如何网页授权获取用户基本信息
查看>>
一周TDD小结
查看>>
(三)建筑物多边形化简系列——去除冗余点
查看>>
Spring Boot Oauth2缓存UserDetails到Ehcache
查看>>
sizeof与strlen的用法
查看>>
2017 ICPCECPC 邀请赛 F,D,E, I 题解
查看>>
Linux 下常见目录及其功能
查看>>
python Termux Android 开发介绍
查看>>
开源框架中常用的php函数
查看>>
Java语法糖初探(三)--变长参数
查看>>
Liunx常用命令(Mile)
查看>>
nginx 的提升多个小文件访问的性能模块
查看>>
set&map
查看>>
集合类总结
查看>>
spring boot开发REST接口
查看>>