您的位置:首页 > 其它

HDU 5988 Coding Contest(费用流)

2016-12-17 14:02 323 查看
题意:有n个地点m条有向路,每个地点有选手和食物,现在要求每个选手能吃到食物,需要经过这些路,每次经过一条路会有一定概率影响赛场,第一次经过不影响,问影响赛场的最小概率是多少。

思路:容斥原理可知道对赛场的总概率是 : 1-(p1^n1)*(p2^n2)...*(pm^nm),其中pi为经过第i条路不影响赛场的概率,ni表示经过该条路的(次数-1),因为第一次经过不影响,要使整个式子结果最小,就是后面那部分最大,也就是log((p1^n1)*(p2^n2)...*(pm^nm))最大,那么加个负号的话就是 -n1 * log(p1) -n2 * log(p2) ....  -nm * log(pm)最小,就转化为了最小费用流了。

#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<set>
#include<vector>
#include<algorithm>
const double eps = 1e-6;
const int maxn = 205;
const double INF = 1e9;
using namespace std;

struct P {
int from, to, cap, flow;
double cost;
P() {}
P(int f, int t, int ca, int fl, double c) {
from = f; to = t;
flow = fl; cost = c;
cap = ca;
}
};
vector<P> edge;
vector<int> G[maxn];
int n, m, T, s, t;
int inq[maxn];
double d[maxn];
int p[maxn], a[maxn];
int have[maxn][2];

void init() {
s = 0; t = n + 1;
edge.clear();
for(int i = 0; i < maxn; i++) G[i].clear();
}

void add(int f, int t, int c, double co) {
edge.push_back(P(f, t, c, 0, co));
edge.push_back(P(t, f, 0, 0, -co));
int sz = edge.size();
G[f].push_back(sz - 2);
G[t].push_back(sz - 1);
}

bool bellmanford(int s, int t, int &flow, double &cost) {
fill(d, d + maxn, INF);
memset(inq, 0, sizeof(inq));
d[s] = 0; inq[s] = 1;
p[s] = 0; a[s] = INF;
queue<int> q; q.push(s);
while(!q.empty()) {
int u = q.front(); q.pop();
inq[u] = 0;
for(int i = 0; i < G[u].size(); i++) {
P &e = edge[G[u][i]];
if(e.cap <= e.flow || d[e.to] <= d[u] + e.cost + eps) continue;
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if(inq[e.to]) continue;
q.push(e.to); inq[e.to] = 1;
}
}
if(d[t] >= INF - 10) return false;
flow += a[t]; cost += d[t] * a[t];
for(int u = t; u != s; u = edge[p[u]].from) {
edge[p[u]].flow += a[t];
edge[p[u] ^ 1].flow -= a[t];
}
return true;
}

double mincost(int s, int t) {
int f = 0;
double c = 0;
while(bellmanford(s, t, f, c));
return c;
}

int main() {
scanf("%d", &T);
while(T--) {
scanf("%d %d", &n, &m);
init();
int s = 0, t = n + 1;
for(int i = 1; i <= n; i++) {
scanf("%d %d", &have[i][0], &have[i][1]);
add(s, i, have[i][0], 0);
add(i, t, have[i][1], 0);
}
while(m--) {
int u, v, c;
double p, np;
scanf("%d %d %d %lf", &u, &v, &c, &p);
np = 1 - p;
add(u, v, 1, 0);

4000
if(c > 1) add(u, v, c - 1, -log10(np));
}
double ans = -mincost(s, t);
double p = 1 - pow(10, ans);
printf("%.2f\n", p);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: