2016 ICPC青岛赛 hdu5988 G.Coding Contest

本文介绍了一个编程比赛中关于网络稳定性的优化问题。通过构建一个复杂的网络模型,利用费用流算法求解最优路径,确保所有参赛者都能获取午餐的同时,使网络崩溃的可能性最小。


Coding Contest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 849    Accepted Submission(s): 156


Problem Description
A coding contest will be held in this university, in a huge playground. The whole playground would be divided into N blocks, and there would be M directed paths linking these blocks. The i-th path goes from the  ui -th block to the  vi -th block. Your task is to solve the lunch issue. According to the arrangement, there are  si competitors in the i-th block. Limited to the size of table,  bi  bags of lunch including breads, sausages and milk would be put in the i-th block. As a result, some competitors need to move to another block to access lunch. However, the playground is temporary, as a result there would be so many wires on the path.
For the i-th path, the wires have been stabilized at first and the first competitor who walker through it would not break the wires. Since then, however, when a person go through the i - th path, there is a chance of  pi  to touch
the wires and affect the whole networks. Moreover, to protect these wires, no more than  ci  competitors are allowed to walk through the i-th path.
Now you need to find a way for all competitors to get their lunch, and minimize the possibility of network crashing.
 

Input
The first line of input contains an integer t which is the number of test cases. Then t test cases follow.
For each test case, the first line consists of two integers N (N ≤ 100) and M (M ≤ 5000). Each of the next N lines contains two integers si and  bi  ( si  ,  bi  ≤ 200).
Each of the next M lines contains three integers  ui  ,  vi  and  ci(ci  ≤ 100) and a float-point number  pi (0 <  pi  < 1).
It is guaranteed that there is at least one way to let every competitor has lunch.
 

Output
For each turn of each case, output the minimum possibility that the networks would break down. Round it to 2 digits.
 

Sample Input
  
1 4 4 2 0 0 3 3 0 0 3 1 2 5 0.5 3 2 5 0.5 1 4 5 0.5 3 4 5 0.5
 

Sample Output
  
0.50
 

Source
 

Recommend
jiangzijing2015   |   We have carefully selected several similar problems for you:   5994  5993  5992  5991  5990 
 
题意:n个点,每个点有si个人和bi个面包,有m条有向边,每次通过都有pi的概率破坏这条路,第一次通过不会破坏,求最小破坏概率使得所有人都能吃到面包。

思路:求最小破坏概率就是求最大不破坏概率。就是(1-p)的乘积,显然这道题是费用流问题,费用流模板里面费用是用加减的,所以这里取对数log2(1-p),模板求的是最小费用,所以再加个负号,也就是费用w=-log2(1-p),最后答案再取回对数就好了。注意就是这里的费用是浮点数,那么就要加eps来判断,不然会tle的。下面给代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<string>
#include<functional>
typedef long long LL;
using namespace std;
#define MAXN 110
#define MAXM 25000
#define ll l,mid,now<<1
#define rr mid+1,r,now<<1|1
#define lson l1,mid,l2,r2,now<<1
#define rson mid+1,r1,l2,r2,now<<1|1
#define pi acos(-1.0)
#define INF 2e9
const double eps = 1e-8;
const int mod = 1e9 + 7;
struct Edge
{
	int to, next, cap, flow;
	double cost;
}edge[MAXM];
int head[MAXN], tol;
int pre[MAXN];
double dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从0~N-1  
void init(int n)
{
	N = n;
	tol = 0;
	memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int cap, double cost)
{
	edge[tol].to = v;
	edge[tol].cap = cap;
	edge[tol].cost = cost;
	edge[tol].flow = 0;
	edge[tol].next = head[u];
	head[u] = tol++;
	edge[tol].to = u;
	edge[tol].cap = 0;
	edge[tol].cost = -cost;
	edge[tol].flow = 0;
	edge[tol].next = head[v];
	head[v] = tol++;
}
bool spfa(int s, int t)
{
	queue<int>q;
	for (int i = 0; i <= N; i++)
	{
		dis[i] = INF;
		vis[i] = false;
		pre[i] = -1;
	}
	dis[s] = 0;
	vis[s] = true;
	q.push(s);
	while (!q.empty())
	{
		//cout<<1<<endl;  
		int u = q.front();
		q.pop();
		vis[u] = false;
		for (int i = head[u]; i != -1; i = edge[i].next)
		{
			int v = edge[i].to;
			if (edge[i].cap > edge[i].flow &&
				dis[v]-dis[u]-edge[i].cost>eps)
			{
				dis[v] = dis[u] + edge[i].cost;
				pre[v] = i;
				if (!vis[v])
				{
					vis[v] = true;
					q.push(v);
				}
			}
		}
	}
	if (pre[t] == -1)return false;
	else return true;
}
//返回的是最大流,cost存的是最小费用  
int minCostMaxflow(int s, int t, double &cost)
{
	int flow = 0;
	cost = 0;
	while (spfa(s, t))
	{
		//cout<<1<<endl;  
		int Min = INF;
		for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to])
		{
			if (Min > edge[i].cap - edge[i].flow)
				Min = edge[i].cap - edge[i].flow;
		}
		for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to])
		{
			edge[i].flow += Min;
			edge[i ^ 1].flow -= Min;
			cost += edge[i].cost * Min;
		}
		flow += Min;
	}
	return flow;
}
int main()
{
	int t;
	scanf("%d", &t);
	while (t--){
		int n, m;
		scanf("%d%d", &n, &m);
		init(n + 1);
		for (int i = 1; i <= n; i++){
			int s, b;
			scanf("%d%d", &s, &b);
			int f = s - b;
			if (f > 0)
				addedge(0, i, f, 0);
			else if (f < 0)
				addedge(i, n + 1, -f, 0);
		}
		while (m--){
			int u, v, f;
			double w;
			scanf("%d%d%d%lf", &u, &v, &f, &w);
			w = -log2(1 - w);
			if (f > 0)
				addedge(u, v, 1, 0);
			if (f - 1>0)
				addedge(u, v, f - 1, w);
		}
		double cost = 0;
		minCostMaxflow(0, n + 1, cost);
		cost = 1 - pow(2, -cost);
		printf("%.2lf\n", cost);
	}
}


UIUC ICPC Spring Coding Contest 2019是UIUC(伊利诺伊大学厄巴纳-香槟分校)举办的一个编程比。UIUC ICPC Spring Coding Contest 2019是ACM国际大学生程序设计竞(ACM International Collegiate Programming Contest)的一部分。ACM国际大学生程序设计竞是世界上最具影响力的大学生计算机竞之一,每年吸引了来自全球各地的大学生参与。这个比旨在培养学生的算法和编程技能,提供一个展示和交流的平台。参者需要在规定时间内解决一系列编程问题。 参加UIUC ICPC Spring Coding Contest 2019对于那些对算法和编程有兴趣的学生来说,是一个很好的学习和锻炼机会。比中的问题通常涉及各种算法和数据结构,要求参者能够用编程语言实现有效和高效的解决方案。参者可以通过解决问题来提高他们的算法和编程技能,并与其他参者交流和学习。 在准备UIUC ICPC Spring Coding Contest 2019之前,建议参者先掌握一些基本的编程知识和技能,如数据结构、算法、编程语言等。参者可以参考一些相关的教程和学习资料,如GeeksforGeeks和HackerEarth等网站提供的编程教程。此外,还可以参考一些竞经验分享的文章和博客,了解其他人是如何准备和参加编程比的。 总之,参加UIUC ICPC Spring Coding Contest 2019是一个很好的机会,可以提高算法和编程技能,与其他参者交流和学习。准备比前,建议参者掌握基本的编程知识和技能,并参考一些相关的教程和学习资料。祝你在比中取得好成绩!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Awesome Competitive Programming Awesome](https://blog.csdn.net/qq_27009517/article/details/86593200)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值