Coding Contest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 849 Accepted Submission(s): 156
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 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.
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
0.50
思路:求最小破坏概率就是求最大不破坏概率。就是(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);
}
}
371



被折叠的 条评论
为什么被折叠?



