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