01分数规划:通常的问法是:在一张有 \(n\) 个点,\(m\) 条边的有向图中,每一条边均有其价值 \(v\) 与其代价 \(w\);求在图中的一个环使得这个环上所有的路径的权值和与代价和的比率最小\大。即求 \(\frac{\sum v}{\sum w}\) 的最小值\最大值。
通常的解法也是比较固定的,我们首先假设求最大值,最优的答案为 \(L\),\(L = \frac{\sum v}{\sum w}\)。接下来我们对于这个式子进行变形:
\(L * \sum w = \sum v\)
\(L * \sum w - \sum v = 0\)
注意到这里面的 \(L\) 是我们假设出来的最优解。若我们二分这个答案,设我们当前二分出来的答案为 \(R\),则考虑当 \(R < L\) 和 \(R > L\) 的时候会分别出现什么状况。当我们选定了一个 \(R\),一条边的 \(R * w - v\) 就是一个固定的值,不妨将它视作新的边权。而原式便是这些边权的和。若此时图中有负环,因为 \(R * w - v\) 随 \(R\) 的增大单调不减,说明可以将 \(R\) 的值设定得更大,有更有的解。存在零环说明此时的 \(R\) 恰好等于 \(L\),没有负环 & 零环说明不能取到当前值。
1.HNOI2009最小圈
裸的01分数规划,只是最大变成了最小。做法还是一样,将spfa转化为最长路判正环即可。
#include <bits/stdc++.h>
using namespace std;
#define maxn 10050
#define eps 0.0000000001
#define db double
int n, m, cnp = , head[maxn];
db ans, dis[maxn];
bool vis[maxn], mark[maxn]; struct edge
{
int to, last;
db co, w;
}E[maxn]; void add(int u, int v, db w)
{
E[cnp].to = v, E[cnp].w = w;
E[cnp].last = head[u], head[u] = cnp ++;
} bool spfa(int u)
{
vis[u] = , mark[u] = ;
for(int i = head[u]; i; i = E[i].last)
{
int v = E[i].to;
if(dis[v] < dis[u] + E[i].co)
{
dis[v] = dis[u] + E[i].co;
if(vis[v] || spfa(v)) { vis[u] = ; return ; }
}
}
vis[u] = ;
return ;
} bool check()
{
memset(mark, , sizeof(mark));
memset(dis, , sizeof(dis));
for(int i = ; i <= n; i ++)
if(!mark[i] && spfa(i)) return ;
return ;
} int main()
{
scanf("%d%d", &n, &m);
for(int i = ; i <= m; i ++)
{
int u, v; db w;
scanf("%d%d%lf", &u, &v, &w);
add(u, v, w);
}
db l = -, r = ;
while(l + eps < r)
{
db mid = (l + r) / 2.0;
for(int i = ; i < cnp; i ++) E[i].co = mid - E[i].w;
if(check()) ans = mid, r = mid;
else l = mid;
}
printf("%.8lf\n", ans);
return ;
}
2.APIO2017商旅
其实也是裸裸的一道题,建图的方式略有隐藏罢了。注意到两个点之间只能携带一种物品,我们将这些路径建成边连起来,然后套路即可。只不过因为这题我二分的是整数,所以要注意判零环。
#include <bits/stdc++.h>
using namespace std;
#define maxn 105
#define INF 99999999
int n, m, T, a[maxn][maxn * ][];
int R[maxn][maxn], val[maxn][maxn];
int ans, E[maxn][maxn], dis[maxn];
bool mark[maxn], vis[maxn]; int read()
{
int x = , k = ;
char c;
c = getchar();
while(c < '' || c > '') { if(c == '-') k = -; c = getchar(); }
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * k;
} void Floyd()
{
for(int k = ; k <= n; k ++)
for(int i = ; i <= n; i ++)
for(int j = ; j <= n; j ++)
R[i][j] = min(R[i][j], R[i][k] + R[k][j]);
} void Build()
{
for(int i = ; i <= n; i ++)
for(int j = ; j <= n; j ++)
{
if(R[i][j] >= INF) continue;
for(int k = ; k <= T; k ++)
if(~a[i][k][] && ~a[j][k][])
{
int v = a[j][k][] - a[i][k][];
if(v > val[i][j]) val[i][j] = v;
}
}
} bool spfa(int u)
{
vis[u] = , mark[u] = ;
for(int i = ; i <= n; i ++)
{
if(i == u || R[i][u] >= INF) continue;
if(dis[i] > dis[u] + E[i][u] || (!mark[i] && dis[i] == dis[u] + E[i][u]))
{
dis[i] = dis[u] + E[i][u];
if(vis[i] || spfa(i)) { vis[u] = ; return ;}
}
else if(dis[i] == dis[u] + E[i][u] && vis[i])
{ vis[u] = ; return ; }
}
vis[u] = ;
return ;
} bool check()
{
memset(dis, , sizeof(dis));
memset(mark, , sizeof(mark));
for(int i = ; i <= n; i ++)
if(!mark[i] && spfa(i)) return ;
return ;
} void work()
{
int l = , r = ;
while(l <= r)
{
int mid = (l + r) >> ;
for(int i = ; i <= n; i ++)
for(int j = ; j <= n; j ++)
{
if(R[i][j] >= INF) continue;
E[i][j] = mid * R[i][j] - val[i][j];
}
if(check()) ans = mid, l = mid + ;
else r = mid - ;
}
} int main()
{
n = read(), m = read(), T = read();
for(int i = ; i <= n; i ++)
for(int j = ; j <= * T; j ++)
if(j & ) a[i][(j >> ) + ][] = read();
else a[i][j >> ][] = read();
for(int i = ; i <= n; i ++)
for(int j = ; j <= n; j ++)
if(i != j) R[i][j] = INF;
for(int i = ; i <= m; i ++)
{
int x = read(), y = read(), t = read();
R[x][y] = min(R[x][y], t);
}
Floyd();
Build();
work();
printf("%d\n", ans);
return ;
}
3.SDOI2017新生舞会
就套路吧……只不过要注意下精度。【我的代码跑得非常非常的慢……】
#include <bits/stdc++.h>
using namespace std;
#define maxn 450
#define INF 9999999999.9
#define int long long
#define db double
#define eps 0.00000001 int n, a[maxn][maxn], b[maxn][maxn];
int flow[maxn], S, T, pre[maxn];
int cnp, head[maxn];
deque <int> q; bool vis[maxn], flag;
db dis[maxn], cost; struct edge
{
int to, last, u, f, F;
db co;
}E[maxn * ]; int read()
{
int x = , k = ;
char c;
c = getchar();
while(c < '' || c > '') { if(c == '-') k = -; c = getchar(); }
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * k;
} void add(int u, int v, int f, int co)
{
E[cnp].u = u, E[cnp].to = v, E[cnp].co = (db) co;
E[cnp].last = head[u]; E[cnp].f = E[cnp].F = f; head[u] = cnp ++;
E[cnp].u = v, E[cnp].to = u, E[cnp].co = -(db) co;
E[cnp].last = head[v]; E[cnp].f = E[cnp].F = ; head[v] = cnp ++;
} void init()
{
int a = * n + ;
for(int i = ; i <= a; i ++) dis[i] = INF;
} bool SPFA()
{
q.push_back(S); init();
flow[S] = INF, vis[S] = , dis[S] = ;
while(!q.empty())
{
int u = q.front(); q.pop_front(); vis[u] = ;
for(int i = head[u]; ~i; i = E[i].last)
{
int v = E[i].to;
if(E[i].f && dis[v] > dis[u] + E[i].co)
{
dis[v] = dis[u] + E[i].co; pre[v] = i;
flow[v] = min(flow[u], E[i].f);
if(!vis[v])
{
vis[v] = ;
if(!q.empty() && dis[v] < dis[q.front()]) q.push_front(v);
else q.push_back(v);
}
}
}
}
if(dis[T] >= INF) return ; else return ;
} db work()
{
cost = ; int ans = ;
while(SPFA())
{
int e = pre[T];
while()
{
E[e].f -= flow[T], E[e ^ ].f += flow[T];
if(E[e].u != S) e = pre[E[e].u];
else break;
}
ans += flow[T]; cost += (db) flow[T] * dis[T];
}
return cost;
} signed main()
{
n = read(); T = * n + ;
memset(head, -, sizeof(head));
for(int i = ; i <= n; i ++)
for(int j = ; j <= n; j ++)
a[i][j] = read();
for(int i = ; i <= n; i ++)
for(int j = ; j <= n; j ++)
b[i][j] = read();
for(int i = ; i <= n; i ++)
for(int j = ; j <= n; j ++)
add(i, j + n, , );
for(int i = ; i <= n; i ++)
{
add(i + n, T, , );
add(S, i, , );
}
db r = 10000.0, l = eps, ans = ;
while(r - l > eps)
{
db mid = (r + l) / 2.0;
for(int i = ; i < cnp; i += )
{
int u = E[i].u, v = E[i].to; E[i].f = E[i].F, E[i ^ ].f = E[i ^ ].F;
if(!u || !v || u == * n + || v == * n + ) continue;
E[i].co = -((db) a[u][v - n] - mid * (db) b[u][v - n]);
E[i ^ ].co = - E[i].co;
}
db mf = work();
if(mf > ) r = mid;
else ans = mid, l = mid;
}
printf("%.6lf\n", ans);
return ;
}