洛谷3705 [SDOI2017] 新生舞会 【01分数规划】【KM算法】

题目分析:

裸题。怀疑$ O(n^4log{n}) $跑不过,考虑Edmonds-Karp优化。

代码:

 #include<bits/stdc++.h>
using namespace std; const int maxn = ; const double eps = 1e-; int n; int a[maxn][maxn],b[maxn][maxn]; double lx[maxn],ly[maxn],c[maxn][maxn];
int inS[maxn],inT[maxn],Left[maxn];
double slack[maxn]; void read(){
scanf("%d",&n);
for(int i=;i<=n;i++) for(int j=;j<=n;j++) scanf("%d",&a[i][j]);
for(int i=;i<=n;i++) for(int j=;j<=n;j++) scanf("%d",&b[i][j]);
} int match(int now){
inS[now] = ;
for(int i=;i<=n;i++){
if(inT[i]) continue;
if(lx[now]+ly[i] - c[now][i] <= eps){
inT[i] = ;
if(!Left[i] || match(Left[i])){
Left[i] = now;
return true;
}
}else slack[i] = min(slack[i],lx[now]+ly[i]-c[now][i]);
}
return false;
} void update(){
double pp = 1e9;
for(int i=;i<=n;i++) if(!inT[i]) pp = min(pp,slack[i]);
for(int i=;i<=n;i++){
if(inS[i]) lx[i] -= pp;
if(inT[i]) ly[i] += pp;
else slack[i] -= pp;
}
} bool KM(double now){
for(int i=;i<=n;i++) for(int j=;j<=n;j++) c[i][j] = a[i][j]-now*b[i][j];
for(int i=;i<=n;i++) {
lx[i] = -1E9,ly[i] = ; Left[i] = ;
for(int j=;j<=n;j++) lx[i] = max(lx[i],c[i][j]);
}
for(int i=;i<=n;i++){
for(int j=;j<=n;j++) slack[j] = 1e9;
for(;;){
for(int j=;j<=n;j++) inS[j] = inT[j] = ;
if(match(i)) break;
update();
}
}
double ans = ;
for(int i=;i<=n;i++) ans += lx[i] + ly[i];
if(ans <) return false;
else return true;
} double divide(double l,double r){
if(r-l <= eps) return l;
double mid = (l+r)/2.0;
int flag = KM(mid);
if(flag) return divide(mid,r);
else return divide(l,mid);
} int main(){
read();
printf("%.6lf",divide(,1E4));
return ;
}
上一篇:ADO.NET笔记——调用存储过程


下一篇:HDU1006