标签:com push ems load cst 答案 string rac 代码
首先推一下01分数规划的式子,\(\frac{\sum_{e \in C} w_e}{|C|} > \lambda \Leftrightarrow \sum_{e \in C} w_e > \lambda |C| \Leftrightarrow \sum_{e \in C} (w_e - \lambda) > 0\)
因此,我们可以通过二分的手段枚举答案\(mid\),若\(\sum_{e \in C} (w_e - mid) > 0\),则答案在\(mid\)的右侧;反之,在左侧。
转化每条边的边权为\(w_e - mid\),因为这道题中的边割集不是最小割中的割边,这意味着我们可以选择除了割边以外的其他边。
若\(w_e - mid < 0\),则必选且并不需要将这些边放入流网络中,必选的原因是能够使得整个式子变小,即增大整个式子小于\(0\)的可能性。反之,则作为流网络中的边。
因为最小割是分开源点和汇点的边权和的最小值,因此还要选上最小割的割边。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 110, M = 810;
const double eps = 1e-8, inf = 1e8;
int n, m, S, T;
int h[N], e[M], w[M], ne[M], idx;
double f[M];
int cur[N], d[N];
void add(int a, int b, double c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
e[idx] = a, w[idx] = c, ne[idx] = h[b], h[b] = idx ++;
}
bool bfs()
{
memset(d, -1, sizeof(d));
queue<int> que;
que.push(S);
d[S] = 0, cur[S] = h[S];
while(que.size()) {
int t = que.front();
que.pop();
for(int i = h[t]; ~i; i = ne[i]) {
int ver = e[i];
if(d[ver] == -1 && f[i]) {
d[ver] = d[t] + 1;
cur[ver] = h[ver];
if(ver == T) return true;
que.push(ver);
}
}
}
return false;
}
double find(int u, double limit)
{
if(u == T) return limit;
double flow = 0;
for(int i = cur[u]; ~i && flow < limit; i = ne[i]) {
cur[u] = i;
int ver = e[i];
if(d[ver] == d[u] + 1 && f[i]) {
double t = find(ver, min(f[i], limit - flow));
if(!t) d[ver] = -1;
f[i] -= t, f[i ^ 1] += t, flow += t;
}
}
return flow;
}
double dinic()
{
double res = 0, flow;
while(bfs()) {
while(flow = find(S, inf)) {
res += flow;
}
}
return res;
}
bool check(double mid)
{
double res = 0;
for(int i = 0; i < idx; i += 2) {
if(w[i] <= mid) {
res += w[i] - mid;
f[i] = f[i ^ 1] = 0;
}
else f[i] = f[i ^ 1] = w[i] - mid;
}
res += dinic();
return res >= eps;
}
int main()
{
scanf("%d%d%d%d", &n, &m, &S, &T);
memset(h, -1, sizeof(h));
for(int i = 0; i < m; i ++) {
int a, b;
double c;
scanf("%d%d%lf", &a, &b, &c);
add(a, b, c);
}
double l = 0, r = 1e7;
while(r - l > eps) {
double mid = (l + r) / 2;
if(check(mid)) l = mid;
else r = mid;
}
printf("%.2lf\n", r);
return 0;
}
标签:com push ems load cst 答案 string rac 代码
原文地址:https://www.cnblogs.com/miraclepbc/p/14406987.html