Coding Contest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2332 Accepted Submission(s): 509
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.
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.
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
题意:n个点,每个点有si个人和bi个面包,有m条有向边,每次通过都有pi的概率破坏这条路,第一次通过不会破坏,求最小破坏概率使得所有人都能吃到面包。
解题思路:求最小破坏概率就是求最大不破坏概率,即(1-p)的乘积,显然是费用流问题。将每条边的费用变为-log2(1-p)即可。这里费用是浮点数,要加eps来判断,不然会tle(不过这题好像卡常数,同样的代码不一定能过)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 60100
#define MAXM 1000100
int vis[MAXN], pre[MAXN], a[MAXN];
int n, m, S[MAXN], B[MAXN], u, v, f;
double p, d[MAXN];
struct Edge
{
int u, v, c, next;
double cost;
} edge[MAXM];
int s[MAXN], cnt;
void init()
{
cnt = 0;
memset(s, -1, sizeof s);
}
void add(int u, int v, int c, double cost)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].cost = cost;
edge[cnt].c = c;
edge[cnt].next = s[u];
s[u] = cnt++;
edge[cnt].u = v;
edge[cnt].v = u;
edge[cnt].cost = -cost;
edge[cnt].c = 0;
edge[cnt].next = s[v];
s[v] = cnt++;
}
bool spfa(int ss, int ee, int &flow, double &cost)
{
queue<int> q;
for (int i = ss; i <= ee; i++) d[i] = 1.0*INF, vis[i] = 0, pre[i] = -1;
d[ss] = 0, vis[ss] = 1, pre[ss] = 0, a[ss] = INF;
q.push(ss);
while (!q.empty())
{
int u = q.front(); q.pop();
vis[u] = 0;
for (int i = s[u]; ~i; i = edge[i].next)
{
int v = edge[i].v;
if (edge[i].c > 0 && d[v] > d[u] + edge[i].cost + 1e-8)
{
d[v] = d[u] + edge[i].cost;
pre[v] = i;
a[v] = min(a[u], edge[i].c);
if (!vis[v])
{
vis[v] = 1;
q.push(v);
}
}
}
}
if (pre[ee] == -1) return 0;
flow += a[ee];
cost += d[ee] * a[ee];
int u = ee;
while (u != ss)
{
edge[pre[u]].c -= a[ee];
edge[pre[u] ^ 1].c += a[ee];
u = edge[pre[u]].u;
}
return 1;
}
double MCMF(int ss, int ee)
{
int flow = 0;
double cost = 0;
while (spfa(ss, ee, flow, cost));
return cost;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
init();
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &S[i], &B[i]);
if (S[i] > B[i]) add(0, i, S[i] - B[i], 0);
if (B[i] > S[i]) add(i, n + 1, B[i] - S[i], 0);
}
for (int i = 1; i <= m; i++)
{
scanf("%d%d%d%lf", &u, &v, &f, &p);
if (f <= 0) continue;
add(u, v, 1, 0);
if (f > 1) add(u, v, f - 1, -log2(1 - p));
}
printf("%.2f\n", 1 - pow(2, -MCMF(0, n + 1)));
}
return 0;
}