UVAlive--4529--Dangerous Tunnels(二分+拆点最大流)



Dangerous Tunnels

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

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 P1P2 ,
and P3 are, respectively, 3, 5, and 6. And, the dangerous degree of {P2P3} 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

Figure 1: An example.

can be selected at the same time; however, P1 and P2 cannot be selected at the same time, since they both pass r2 .
Kulolo has a higher altitude than all rest stations while Gulolo has a lower altitude than all rest stations. Kinglolo is a thoughtful chief. It is ensured that the altitudes of the rest stations on each selected path are non-increasing, so that the path is
more suitable for transportation. For example, in Figure 1, the path (Kulolo, r3 , r1 , Gulolo) will never be selected,
since r1 is higher than r3 .

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:

  1. all paths are from Kulolo to Gulolo,
  2. no two paths pass the same rest station,
  3. the altitudes of the rest stations on each path are non-increasing, and
  4. 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

  1. The number of rest stations, n : 0 < n200 .
  2. The number of tunnels, t : t > 0 .
  3. The dangerous degree of a tunnel, d : 1d100000 .
  4. 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 r1r2,..., 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 10
2 3 5
1
1
2
0 1 5
1 2 6
2
3
2
0 1 5
3 4 7
1
3
6
0 1 8
0 2 12
0 3 15
3 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 0x3f3f3f3f
int 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);
			else
			add(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;
			}
			else
			l=mid+1;
		}
		printf("Case %d: ",Case++);
		if(ans==0)
			printf("no solution\n");
		else
			printf("%d\n",ans);
	}
	return 0;
}

时间: 2024-11-01 04:06:49

UVAlive--4529--Dangerous Tunnels(二分+拆点最大流)的相关文章

POJ2391.Ombrophobic Bovines(不喜欢雨的奶牛)——floyd+二分+拆点+最大流

http://poj.org/problem?id=2391 写的挫的最大流会超时~~~ 题目描述: Jack 农场主的奶牛实在是太讨厌被淋湿了.决定在农场设置降雨警报,这样在快要下 雨的时候可以让奶牛们都知道.他们设置设计了一个下雨撤退计划,这样在下雨之前每头奶牛都 能躲到避雨点.然而,天气预报并不总是准确的.为了使得错误的天气预报影响尽可能小,他们 希望尽可能晚地拉响警报,只要保证留有足够的时间让所有的奶牛都能回到避雨点就可以了. 农场有F 块草地,1≤F≤200,奶牛们在草地上吃草.这些草

POJ 2391 Ombrophobic Bovines(二分+拆点+最大流)

http://poj.org/problem?id=2391 题意: 给定一个无向图,点i处有Ai头牛,点i处的牛棚能容纳Bi头牛,求一个最短时间T,使得在T时间内所有的牛都能进到某一牛棚里去. 思路: 建立一个源点和汇点,源点和牛棚的初始牛量相连,汇点和牛棚容量相连.这样跑最大流,如果最后流量等于牛的总数时,就说明是可以的. 那么,怎么连边呢?二分时间,根据时间来连边,所以首先我们先跑一遍floyd计算出两点距离.然后在该时间下,如果d[i][j],那么就添加边(i,i',INF),表面这段路

POJ 2391 floyd二分+拆点+最大流

Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20904   Accepted: 4494 Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in their hooves. They h

UVALive 6168 Fat Ninjas --二分小数+搜索

题意:一个NxN的网格地板,有一些激光束从天花板垂直射向地面的某个网格,一个圆要安全地从左走到右,不碰到上边界,下边界以及激光束,问这个圆的直径最大能达到多大. 分析:可以二分直径,关键在check函数的写法.可以讲这个圆缩成一个点,把圆的直径转化为激光的扫描范围,当激光范围完全堵死一条通道的时候,这个直径则是不可行的.怎样判断是否堵死一条通道了呢.每次check(dis)的时候,枚举激光束对,如果激光束之间距离小于dis,那么它们两个之间建一条边.还要注意处理边界,如果激光束范围与上边界或下边

Risk UVA - 12264 拆点法+最大流+二分

/** 题目:Risk UVA - 12264 链接:https://vjudge.net/problem/UVA-12264 题意:给n个点的无权无向图(n<=100),每个点有一个非负数ai. 若ai==0则此点归敌方所有,若ai>0则此点归你且上面有ai个属于你的士兵. 保证至少有一个属于你的点与敌方的点相邻.你可以让你的每个士兵最多移动一次 ,每次可以待在原地或者去到相邻的属于你的领地,但每个点至少要留1各士兵, 使得最薄弱的关口尽量坚固.关口是指与敌方点相邻的点,薄弱与坚固分别指兵少

poj 2749 Building roads (二分+拆点+2-sat)

Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6229   Accepted: 2093 Description Farmer John's farm has N barns, and there are some cows that live in each barn. The cows like to drop around, so John wants to build some ro

UVALive - 3211 (2-SAT + 二分)

layout: post title: 训练指南 UVALive - 3211 (2-SAT + 二分) author: "luowentaoaa" catalog: true mathjax: true tags: - 2-SAT - 图论 - 训练指南 Now or later UVALive - 3211 题意 n架飞机,每架可选择两个着落时间.安排一个着陆时间表,使得着陆间隔的最小值最大 题解 二分查找最大值P,每次都用2-SAT判断是否可行. #include<bits

bzoj 2285 [Sdoi2011]保密(二分,spfa + 最大流)

Description 现在,保密成为一个很重要也很困难的问题.如果没有做好,后果是严重的.比如,有个人没有自己去修电脑,又没有拆硬盘,后来的事大家都知道了. 当然,对保密最需求的当然是军方,其次才是像那个人.为了应付现在天上飞来飞去的卫星,军事基地一般都会建造在地下. 某K国的军事基地是这样子的:地面上两排大天井共n1个作为出入口,内部是许多除可以共享出入口外互不连通的空腔,每个空腔有且只有两个出入口,并且这两个出入口不会在同一排.为了方便起见,两排出入口分别编号为1,3,5…和2,4,6…并

POJ 2455Secret Milking Machine(二分+网络流之最大流)

题目地址:POJ2455 手残真浪费时间啊..又拖到了今天才找出了错误..每晚两道题不知不觉又变回了每晚一道题...sad.. 第一次在isap中忘记调用bfs,第二次则是遍历的时候居然是从1开始遍历的...sad... 这题思路倒是很简单,就是有一个比较坑的地方,就是这里的重边要当两条边来用,以前受最短路什么的影响,直接把慢的删了,很明显不对...这里的两条重边都是可以走的. 建图思路是,将1当作源点,将n当作汇点.右边的地方就连边,注意是无向边.最后判断最大流是否等于道路条数.二分找最小值.