UVAlive--4529--Dangerous Tunnels(二分+拆点最大流)
来源:互联网 发布:印尼越南 知乎 编辑:程序博客网 时间:2024/05/17 18:23
Description
Somewhere in the world, there are two tribes separated by mountains. The two tribes are named Kulolo and Gulolo, respectively, where Kulolo is at a higher altitude and Gulolo is at a lower altitude. Due to the limitation of geography, Gulolo has fewer resources than Kulolo. In order to transport resources from Kulolo to Gulolo efficiently, several tunnels were built inside the mountains between these two tribes. There are also some rest stations built for people to take a break during the transportation in the tunnels. More specifically, each terminal of a tunnel is either Kulolo, Gulolo, or a rest station.
The structure of those tunnels is not stable. A dangerous degree has been estimated for each tunnel, due to its stability, in advance. A tunnel with a higher dangerous degree is considered to be more dangerous; that is, it is more probably to collapse. Kinglolo, the chief of Kulolo, would like to select some paths through the tunnels to Gulolo with little risk. In Kinglolo's opinion, the dangerous degree of a path is equal to the maximum dangerous degree of the tunnels in the path; and the dangerous degree of a set of paths is equal to the maximum dangerous degree of the paths in it. For example, consider Figure 1. The dangerous degrees of P1, P2 , and P3 are, respectively, 3, 5, and 6. And, the dangerous degree of {P2, P3} is 6.
Since all tunnels are narrow, a limited quantity of resources can be transported along a path in one day. Therefore, every day, depending on the amount of resources needed to be transported, a different number, say k , of paths is required. Moreover, to avoid congestion, these k selected paths cannot pass any rest station in common. For example, in Figure 1, P2 and P3
People in Kulolo believe that Kinglolo is the most brilliant man in the world, since he always selects a set of k paths that is as little dangerous as possible (i.e., the maximum dangerous degree of the selected paths is minimized). Now, given the data of the constructed tunnels, you are asked to find k paths that Kinglolo may select. In summary, the k selected paths, if exist, should satisfy the following:
- all paths are from Kulolo to Gulolo,
- no two paths pass the same rest station,
- the altitudes of the rest stations on each path are non-increasing, and
- the maximum dangerous degree of the paths is minimized.
For simplicity, only the maximum dangerous degree of the selected paths should be reported.
Technical Specification
- The number of rest stations, n : 0 < n200 .
- The number of tunnels, t : t > 0 .
- The dangerous degree of a tunnel, d : 1d100000 .
- The number of paths which should be selected, k : 1k10 .
Input
The input consists of multiple test cases. The first line of each case contains a positive integer n(0 < n200) which indicates that there are n rest stations r1, r2,..., rn . For ease of description, Kulolo and Gulolo are denoted by r0 and rn+1 , respectively. We assume that ri is higher than rj for any 0i < jn + 1 . The second line of each case contains a positive integert(t > 0) that specifies the number of tunnels. Each of the following t lines contains three integers p , q , d(0pn + 1, 0qn + 1, pq, 1d100000) separated by white space, which indicate there is a tunnel with dangerous degree d connecting rp and rq . Then, a line containing a positive integer k(1k10) is provided, which is the number of paths that should be selected. You can assume that there is at most one tunnel between any two rest stations. The last test case is followed by a line containing a zero.
Output
For each test case, print a line containing the test case number (beginning with 1) followed by the maximum dangerous degree of the k paths that Kinglolo may select. If the solution does not exist, print `` no solution". Use the format of the sample output.
Sample Input
2 4 0 1 3 1 3 12 2 0 102 3 5 1 1 2 0 1 51 2 6 2 3 2 0 1 53 4 71 3 6 0 1 80 2 120 3 153 1 9 3 4 8 2 4 12 2 0
Sample Output
Case 1: 10 Case 2: no solution Case 3: no solution Case 4: 12
题意:从0--n+1,中间有一些路,走的时候起点必须大于终点,每条路都有一定的危险值,每一种方案需要挑选至少K条路,这些路里危险度最大的那条路就是整个方案的危险值,输出最小的危险值,如果没有方案的话输出no solutation!
二分枚举危险度,建图,然后跑一边拆点最大流,每条边容量为1,判断最大流结果是否大于等于k
#include<cstdio>#include<queue>#include<cstring>#include<algorithm>using namespace std;#define MAXN 220*220#define MAXM 220*220*5#define INF 0x3f3f3f3fint u[MAXN],v[MAXN],c[MAXN],n,m,k;int head[MAXN],dis[MAXN],cnt,cur[MAXN],vis[MAXN];struct node{int u,v,cap,flow,next;}edge[MAXM];void init(){memset(head,-1,sizeof(head));cnt=0;}void add(int a,int b,int w){node E={a,b,w,0,head[a]};edge[cnt]=E;head[a]=cnt++;node E1={b,a,0,0,head[b]};edge[cnt]=E1;head[b]=cnt++;}bool BFS(int s,int t){queue<int>q;memset(vis,0,sizeof(vis));memset(dis,-1,sizeof(dis));q.push(s);dis[s]=0;vis[s]=1;while(!q.empty()){int u=q.front();q.pop();for(int i=head[u];i!=-1;i=edge[i].next){node E=edge[i];if(E.cap>E.flow&&!vis[E.v]){dis[E.v]=dis[E.u]+1;vis[E.v]=1;if(E.v==t) return true;q.push(E.v);}}}return false;}int DFS(int x,int a,int e){if(a==0||e==x) return a;int flow=0,f;for(int &i=cur[x];i!=-1;i=edge[i].next){node &E=edge[i];if(dis[E.v]==dis[E.u]+1&&(f=DFS(E.v,min(E.cap-E.flow,a),e))>0){flow+=f;a-=f;edge[i].flow+=f;edge[i^1].flow-=f;if(a==0) break;}}return flow;}int MAXflow(int s,int t){int flow=0;while(BFS(s,t)){memcpy(cur,head,sizeof(head));flow+=DFS(s,INF,t);}return flow;}bool judge(int x){init();for(int i=0;i<m;i++){if(c[i]<=x){if(u[i]==0)add(u[i],v[i],1);elseadd(u[i]+1+n,v[i],1);}}for(int i=1;i<=n+1;i++)add(i,i+1+n,1);return MAXflow(0,n+1)>=k;}int main(){int Case=1;while(scanf("%d",&n)!=EOF){if(n==0) break;scanf("%d",&m);memset(u,0,sizeof(u));memset(v,0,sizeof(v));memset(c,0,sizeof(c));int r=0;for(int i=0;i<m;i++){scanf("%d%d%d",&u[i],&v[i],&c[i]);r=max(c[i],r);if(u[i]>v[i])swap(u[i],v[i]);}scanf("%d",&k);int l=0,ans=0;while(r>=l){int mid=(l+r)/2;if(judge(mid)){ans=mid;r=mid-1;}elsel=mid+1;}printf("Case %d: ",Case++);if(ans==0)printf("no solution\n");elseprintf("%d\n",ans);}return 0;}
- UVAlive--4529--Dangerous Tunnels(二分+拆点最大流)
- uva 4259 - Dangerous Tunnels 【二分 + 最大流】
- [最大流+二分答案] LightOJ 1167 - Dangerous Tunnels
- LightOJ 1167 - Dangerous Tunnels 二分枚举+最大流
- UVALive 2957 Bring Them There(拆点+最大流)
- UVALive 2957Bring Them There (最大流+拆点)
- poj 2391(最大流+二分+拆点)
- POJ 2391 拆点+最大流+二分
- hdu 3277 二分+拆点最大流
- HDU 3036 拆点二分最大流
- poj 2391(二分+拆点+最大流)
- poj2391 二分最大流+拆点
- poj3228 最大流+拆点+二分
- POJ 2112 Optimal Milking (二分+最大流/多重匹配) && POJ 2391 Ombrophobic Bovines( 二分+拆点+最大流)
- UVALive - 3268 Jamie's Contact Groups(二分+最大流)
- uvalive 3268 二分图匹配 或者 最大流
- UVALive 3415 浅谈二分图最大点独立集架构建模
- UVALive 7958|Gym 101201G|Maximum Islands|二分图|最大独立点集
- /proc/sys/vm/max_map_count耗尽时,调用glibc 2.11.3 free()导致程序crash,问题追踪和解决
- Android 异步消息处理机制(Handler 、 Looper 、MessageQueue)源码解析
- 监听日志太大,有10g怎么破?
- ubuntu安装chrome
- beego模板语法 go语言模版语法
- UVAlive--4529--Dangerous Tunnels(二分+拆点最大流)
- GCD主队列、全局队列
- 柴俊理金:美指强势回归打压,金价受挫原油逆袭
- Android仿斗鱼领取鱼丸文字验证(三)
- HDOU/HDU 2548 两军交锋(看你的思维~)
- new Handler().postDelayed() 延迟intent跳转
- Reactive Cocoa详解(1)----神奇的RAC宏
- 解决Duo13内置博通蓝牙和博通Wifi冲突
- android学习笔记(二)__系统框架