题目传送门
题目大意
给定一个费用流,每条边有一个初始流量$c_i$和单位流量费用$d_i$,增加一条边的1单位的流量需要花费$b_i$的代价而减少一条边的1单位的流量需要花费$a_i$的代价。要求最小化总费用减少量和调整次数的比值(至少调整一次)。
根据基本套路,二分答案,移项,可以得到每条边的贡献。
设第$i$条边的流量变化量为$m_i$,每次变化花费的平均费用为$w_i$。那么有
$\sum c_id_i - \sum (c_i + m_i)d_i + |m_i|(w_i + mid) > 0$
$\sum m_id_i+ |m_i|(w_i + mid) < 0$
那么二分答案后等于判断是否存在一个增广环的费用和为负。(请手动参见式子脑补边权)。
然后可以写个spfa。
Code
/**
* bzoj
* Problem#3597
* Accepted
* Time: 464ms
* Memory: 1720k
*/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
typedef bool boolean; template <typename T>
void pfill(T* pst, const T* ped, T val) {
for ( ; pst != ped; *(pst++) = val);
} typedef class Edge {
public:
int ed, nx;
double w; Edge(int ed, int nx, double w) : ed(ed), nx(nx), w(w) { }
} Edge; typedef class MapManager {
public:
int* h;
vector<Edge> es; MapManager() { }
MapManager(int n) {
h = new int[(n + )];
pfill(h, h + n + , -);
}
~MapManager() {
delete[] h;
es.clear();
} void addEdge(int u, int v, double w) {
es.push_back(Edge(v, h[u], w));
h[u] = (signed) es.size() - ;
} Edge& operator [] (int p) {
return es[p];
}
} MapManager; const double dinf = 1e10;
const double eps = 1e-; typedef class Graph {
public:
int n, s;
MapManager g; int *cnt;
double *f;
boolean *vis; Graph(int n, int s) : n(n), s(s), g(n) {
cnt = new int[(n + )];
f = new double[(n + )];
vis = new boolean[(n + )];
pfill(vis, vis + n + , false);
} boolean neg_circle() {
static queue<int> que;
pfill(f, f + n + , dinf);
pfill(cnt, cnt + n + , );
f[s] = , cnt[s]++;
que.push(s);
while (!que.empty()) {
int e = que.front();
que.pop();
vis[e] = false;
for (int i = g.h[e], eu; ~i; i = g[i].nx) {
eu = g[i].ed;
double w = f[e] + g[i].w;
if (w < f[eu]) {
f[eu] = w, cnt[eu]++;
if (cnt[eu] > n)
return true;
if (!vis[eu]) {
que.push(eu);
vis[eu] = true;
}
}
}
}
return false;
}
} Graph; int n, m;
int *u, *v, *a, *b, *c, *d;
double init_ans = 0.0; inline void init() {
scanf("%d%d", &n, &m);
u = new int[(m + )];
v = new int[(m + )];
a = new int[(m + )];
b = new int[(m + )];
c = new int[(m + )];
d = new int[(m + )];
for (int i = ; i <= m; i++) {
scanf("%d%d%d%d%d%d", u + i, v + i, a + i, b + i, c + i, d + i);
init_ans += c[i] * d[i];
}
} boolean check(double mid) {
Graph graph(n + , n + );
MapManager& g = graph.g; for (int i = ; i <= m; i++) {
if (u[i] == n + ) {
g.addEdge(u[i], v[i], );
} else if (c[i]) {
g.addEdge(u[i], v[i], d[i] + b[i] + mid);
g.addEdge(v[i], u[i], -d[i] + a[i] + mid);
} else {
g.addEdge(u[i], v[i], d[i] + b[i] + mid);
}
} return graph.neg_circle();
} inline void solve() {
double l = , r = init_ans, mid;
for (int t = ; t < && l < r - eps; t++) {
mid = (l + r) / ;
if (check(mid))
l = mid;
else
r = mid;
}
printf("%.2lf\n", l);
} int main() {
init();
solve();
return ;
}
spfa
然后再讲讲费用流做法。因为不存在负的流量,但是我们知道$c_i' \geqslant 0$,因此我们考虑将$m_i$变成$-c_i + m_i'$。
其中$m_i'$始终非负。可以理解为这个做法就是将所有边的流量先变为0再重新调整。
对于绝对值的部分稍微处理一下就行了。(之前那个式子脑残取了等,调了半天,sad。。。)
Code
/**
* bzoj
* Problem#3597
* Accepted
* Time: 1168ms
* Memory: 2004k
*/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
using namespace std;
typedef bool boolean; template <typename T>
void pfill(T* pst, const T* ped, T val) {
for ( ; pst != ped; *(pst++) = val);
} typedef class Edge {
public:
int ed, nx, r;
double c; Edge(int ed = , int nx = , int r = , double c = ) : ed(ed), nx(nx), r(r), c(c) { }
} Edge; typedef class MapManager {
public:
int* h;
vector<Edge> es; MapManager() { }
MapManager(int n) {
h = new int[(n + )];
pfill(h, h + n + , -);
}
~MapManager() {
delete[] h;
es.clear();
} void addEdge(int u, int v, int r, double c) {
es.push_back(Edge(v, h[u], r, c));
h[u] = (signed) es.size() - ;
} void addArc(int u, int v, int cap, double c) {
addEdge(u, v, cap, c);
addEdge(v, u, , -c);
} Edge& operator [] (int p) {
return es[p];
}
} MapManager; const signed int inf = (signed) (~0u >> );
const double dinf = 1e10;
const double eps = 1e-; template <typename T>
T __abs(T x) {
return (x < ) ? (-x) : (x);
} typedef class Network {
public:
int S, T;
MapManager g; int *le;
int *mf;
double *f;
boolean *vis; Network() { }
// be sure T is the last node
Network(int S, int T) : S(S), T(T), g(T) {
f = new double[(T + )];
le = new int[(T + )];
mf = new int[(T + )];
vis = new boolean[(T + )];
pfill(vis, vis + T, false);
}
~Network() {
delete[] f;
delete[] le;
delete[] mf;
delete[] vis;
} double spfa() {
double w;
static queue<int> que;
pfill(f, f + T + , dinf);
que.push(S);
f[S] = , le[S] = -, mf[S] = inf;
while (!que.empty()) {
int e = que.front();
que.pop();
vis[e] = false;
for (int i = g.h[e], eu; ~i; i = g[i].nx) {
if (!g[i].r)
continue;
eu = g[i].ed, w = f[e] + g[i].c;
if (w < f[eu]) {
f[eu] = w, le[eu] = i, mf[eu] = min(mf[e], g[i].r);
if (!vis[eu]) {
vis[eu] = true;
que.push(eu);
}
}
}
}
if (__abs(f[T] - dinf) < eps)
return dinf;
double rt = ;
for (int p = T, e; ~le[p]; p = g[e ^ ].ed) {
e = le[p];
g[e].r -= mf[T];
g[e ^ ].r += mf[T];
rt += mf[T] * g[e].c;
}
return rt;
} double min_cost() {
double rt = , delta;
while (__abs((delta = spfa()) - dinf) >= eps) {
rt += delta;
}
return rt;
}
} Network; // S: n + 1, T: n + 2
int n, m;
int *u, *v, *a, *b, *c, *d;
double init_ans = 0.0; inline void init() {
scanf("%d%d", &n, &m);
u = new int[(m + )];
v = new int[(m + )];
a = new int[(m + )];
b = new int[(m + )];
c = new int[(m + )];
d = new int[(m + )];
for (int i = ; i <= m; i++) {
scanf("%d%d%d%d%d%d", u + i, v + i, a + i, b + i, c + i, d + i);
init_ans += c[i] * d[i];
}
} boolean check(double mid) {
double cost = ;
Network network(n + , n + );
MapManager& g = network.g;
for (int i = ; i <= m; i++) {
if (u[i] == n + ) {
g.addArc(u[i], v[i], c[i], );
} else {
g.addArc(u[i], v[i], c[i], -(a[i] + mid) + d[i]);
g.addArc(u[i], v[i], inf, b[i] + mid + d[i]);
// cost += c[i] * (-d[i] + a[i] + mid);
cost += c[i] * (-d[i] + a[i] + mid);
}
}
cost += network.min_cost();
return cost < ;
} inline void solve() {
double l = , r = init_ans, mid;
for (int t = ; t < && l < r - eps; t++) {
mid = (l + r) / ;
if (check(mid))
l = mid;
else
r = mid;
}
printf("%.2lf\n", l);
} int main() {
init();
solve();
return ;
}