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);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值