hdu 5988 费用流

Coding Contest

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


 

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 sicompetitors 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

 

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
using namespace std;
#define ll long long
const int maxm = 50005;
const int INF = 1e9 + 7;
const double eps = 1e-9;
struct node
{
	int u, v, flow, next;
	double cost;
}edge[maxm];
int n, m, s, t, cnt, sum, FLOW;
int head[maxm], vis[maxm], pre[maxm];
double dis[maxm];
double  ksm(double a, int b)
{
	double ans = 1;
	while (b)
	{
		if (b % 2 == 1) ans *= a;
		a *= a, b /= 2;
	}
	return ans;
}
void init()
{
	cnt = 0, s = 0, t = n * 2 + 1;
	for (int i = 0;i <= t;i++) head[i] = -1;
}
void add(int u, int v, int flow, double cost)
{
	edge[cnt].u = u, edge[cnt].v = v;
	edge[cnt].flow = flow, edge[cnt].cost = cost;
	edge[cnt].next = head[u], head[u] = cnt++;
	edge[cnt].u = v, edge[cnt].v = u;
	edge[cnt].flow = 0, edge[cnt].cost = -cost;
	edge[cnt].next = head[v], head[v] = cnt++;
}
int bfs()
{
	queue<int>q;
	for (int i = 0;i <= t;i++)
		dis[i] = INF, pre[i] = -1, vis[i] = 0;
	dis[s] = 0, q.push(s);
	while (!q.empty())
	{
		int u = q.front();q.pop();
		vis[u] = 0;
		for (int i = head[u];i != -1;i = edge[i].next)
		{
			int v = edge[i].v;
			if (dis[v] > dis[u] + edge[i].cost + eps&&edge[i].flow)
			{
				dis[v] = dis[u] + edge[i].cost;
				pre[v] = i;
				if (!vis[v])
				{
					vis[v] = 1;
					q.push(v);
				}
			}
		}
	}
	if (dis[t] == INF) return 0;
	return 1;
}
double MCMF()
{
	double ans = 0;
	int minflow;
	while (bfs())
	{
		minflow = INF;
		for (int i = pre[t];i != -1;i = pre[edge[i].u])
			minflow = min(minflow, edge[i].flow);
		for (int i = pre[t];i != -1;i = pre[edge[i].u])
			edge[i].flow -= minflow, edge[i ^ 1].flow += minflow;
		ans += dis[t] * minflow;
	}
	return ans;
}
int main()
{
	int i, j, k, T, a, b, c, u, v;
	double p, ans;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d%d", &n, &m);
		init();
		for (i = 1;i <= n;i++)
		{
			scanf("%d%d", &a, &b);
			if (a > b) add(s, i, a - b, 0);
			if (b > a) add(i, t, b - a, 0);
		}
		for (i = 1;i <= m;i++)
		{
			scanf("%d%d%d%lf", &u, &v, &c, &p);
			p = -log2(1 - p);
			if (c > 0) add(u, v, 1, 0);
			if (c > 1) add(u, v, c - 1, p);
		}
		ans = MCMF();
		ans = pow(2, -ans);
		printf("%.2f\n", 1 - ans);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值