#9 //[SDOI2017]新生舞会
2024-08-27 05:22:59
题解:
分数规划+费用流
常数巨大开o2加inline加register还是不行
我也不知道为什么
代码:
#include <bits/stdc++.h>
using namespace std;
const int N=5e4;
const int N1=2e3;
#define INF 1e9
#define INF2 1e4
#define eps 1e-7
#define rg register
#define IL inline
int pre[N],aa[N],s,t,n,m;
double dis[N];
bool vis[];
int l,p1[N1][N1],p2[N1][N1],head[];
struct re{
int a,b,c,flow,from;
double cost;
}a[N];
IL void arr(int x,int y,int z,int flow,double cost)
{
a[++l].a=head[x];
a[l].b=y;
a[l].c=z;
a[l].flow=flow;
a[l].from=x;
a[l].cost=cost;
head[x]=l;
}
IL bool bellmanford(double &flow,double &cost)
{
queue<int>q;
for (rg int i=;i<=t;i++) dis[i]=INF;
aa[s]=INF;
memset(vis,,sizeof(vis));
q.push(s);
while (!q.empty())
{
rg int x=q.front(); q.pop();
rg int u=head[x];
while (u)
{
rg int v=a[u].b;
if (dis[x]+a[u].cost<dis[v]&&a[u].c>a[u].flow)
{
dis[v]=dis[x]+a[u].cost;
aa[v]=min(aa[x],a[u].c-a[u].flow);
pre[v]=u;
if (vis[v])
{
vis[v]=; q.push(v);
}
}
u=a[u].a;
}
vis[x]=;
}
if (dis[t]==INF) return();
rg int x=t; flow+=aa[t]; cost+=aa[t]*dis[t];
while (pre[x])
{
rg int y=pre[x];
a[y].flow+=aa[t];
if (y%==) a[y+].flow-=aa[t];
else a[y-].flow-=aa[t];
x=a[y].from;
}
return ;
}
double flow,cost;
IL void mincost()
{
while (bellmanford(flow,cost));
}
#define mid (h+t)/2
IL bool check(double x)
{
l=;
memset(head,,sizeof(head));
for (rg int i=;i<=n;i++)
for (rg int j=;j<=n;j++)
{
arr(i,j+n,,,-(p1[i][j]-p2[i][j]*x));
arr(j+n,i,,,(p1[i][j]-p2[i][j]*x));
}
s=,t=*n+;
for (rg int i=;i<=n;i++)
{
arr(,i,,,); arr(i,,,,);
arr(i+n,t,,,); arr(t,i+n,,,);
}
flow=; cost=;
mincost();
if (cost<=) return();
else return();
}
int main()
{
std::ios::sync_with_stdio(false);
cin>>n;
for (rg int i=;i<=n;i++)
for (rg int j=;j<=n;j++)
cin>>p1[i][j];
for (rg int i=;i<=n;i++)
for (rg int j=;j<=n;j++)
cin>>p2[i][j];
double h=,t=INF2;
while (t-h>eps)
{
if (check(mid)) h=mid;
else t=mid;
}
printf("%.6f",h);
return ;
}
最新文章
- java 单例(懒汉式)
- Python: 解决pip安装源被墙的问题
- importSTV的使用
- sql语句like多个条件的写法实例
- java_ExecutorService, CompletionService - 有返回值并行工作方式
- qt软键盘输入
- C#获取当前路径,获取当前路径的上一层路径
- TR069协议向导——一个帮助你了解TR069协议的简明教程(一)
- Struts2 学习笔记16 struts标签 part2
- js 小数计算为啥和想象中不一样!
- hp MSA50 5盘RAID5重建为4盘RAID5怎么恢复数据
- 软件工程(GZSD2015) 第三次作业
- [转] KVM storage performance and cache settings on Red Hat Enterprise Linux 6.2
- Linux后门权限维持手法
- samba服务器笔记 (一)
- GNU Binutils简介及基本用法
- 【电子基础】液晶显示器原理&#183;LCD驱动基础
- Druid学习---配置_DruidDataSource参考配置
- sqoop 数据迁移
- [CODECHEF]EASYEX