题目大意:

N个物品,物品间有M组关系,每个物品有一个ai的代价,满足关系后会得到bi的值

求 max(sigma(bi)/sigma(ai))

题解:

很明显的最大权闭合子图,只不过需要处理分数.

这里二分一个答案g

然后直接求sigma(b[i])-sigma(a[i]*g)即可

其中把图中的ai都改成ai*g即可

注意整数处理

 #include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
using namespace std;
const int N=,M=,INF=2e8;
int n,m;double SUM=,ep=0.0000001;
int gi(){
int str=;char ch=getchar();
while(ch>'' || ch<'')ch=getchar();
while(ch>='' && ch<='')str=(str<<)+(str<<)+ch-,ch=getchar();
return str;
}
int val[N];
struct node{
int x,y,v;
}c[M];
int head[(N+M)],num=;
struct Lin{
int next,to;double dis;
}a[(N+M)<<];
void init(int x,int y,double dis){
a[++num].next=head[x];
a[num].to=y;a[num].dis=dis;
head[x]=num;
a[++num].next=head[y];
a[num].to=x;a[num].dis=;
head[y]=num;
}
int S=,T,q[N+M],dep[N+M];
bool bfs(){
for(int i=;i<=T;i++)dep[i]=;
q[]=S;dep[S]=;
int t=,sum=,u,x;
while(t!=sum){
t++;x=q[t];
for(RG int i=head[x];i;i=a[i].next){
u=a[i].to;
if(dep[u] || a[i].dis<ep)continue;
dep[u]=dep[x]+;
q[++sum]=u;
}
}
return dep[T];
}
double dinic(int x,double tot){
if(x==T || !tot)return tot;
int u;double tmp,sum=;
for(RG int i=head[x];i;i=a[i].next){
u=a[i].to;
if(a[i].dis<ep || dep[u]!=dep[x]+)continue;
tmp=dinic(u,min(tot,a[i].dis));
a[i].dis-=tmp;a[i^].dis+=tmp;
sum+=tmp;tot-=tmp;
if(!tot)break;
}
if(sum<ep)dep[x]=;
return sum;
}
double maxflow(){
double tot=,tmp;
while(bfs()){
tmp=dinic(S,INF);
while(tmp>=ep)tot+=tmp,tmp=dinic(S,INF);
}
return tot;
}
void Clear(){
num=;
memset(head,,sizeof(head));
}
bool check(double g){
Clear();
T=n+m+;
for(RG int i=;i<=n;i++)init(S,i,g*(double)val[i]);
for(RG int i=;i<=m;i++){
init(c[i].x,i+n,INF);
init(c[i].y,i+n,INF);
init(i+n,T,c[i].v);
}
double pap=maxflow();
pap=SUM-pap;
if(pap>=ep)return true;
return false;
}
void work(){
n=gi();m=gi();
for(RG int i=;i<=n;i++)val[i]=gi();
for(RG int i=;i<=m;i++)c[i].x=gi(),c[i].y=gi(),c[i].v=gi(),SUM+=c[i].v;
double l=,r=SUM,mid,ans;
while(l<=r){
mid=(l+r)/;
if(check(mid)){
ans=mid;
l=mid+ep;
}
else r=mid-ep;
}
printf("%d\n",(int)ans);
}
int main()
{
freopen("gift.in","r",stdin);
freopen("gift.out","w",stdout);
work();
return ;
}

gift 分数规划的最大权闭合子图的更多相关文章

  1. [SCOI2018]游泳池(计算几何+分数规划+最大权闭合子图)

    题目链接 https://www.luogu.org/problemnew/show/U56187 注:题面参考了网上的其他博客,并非原题题面,因此数据范围可能有误.数据为原创数据. 题解 其实就是许 ...

  2. bzoj 3232 圈地游戏 —— 01分数规划+最小割建图(最大权闭合子图)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3232 心烦意乱的时候调这道题真是...越调越气,就这样过了一晚上... 今天再认真看看,找出 ...

  3. BZOJ.1312.[Neerc2006]Hard Life(分数规划 最大权闭合子图)

    BZOJ 最大密度子图. 二分答案\(x\),转为求是否存在方案满足:\(边数-x*点数\geq 0\). 选一条边就必须选两个点,所以可以转成最大权闭合子图.边有\(1\)的正权,点有\(x\)的负 ...

  4. 2018.11.06 NOIP训练 最大获利(profit)(01分数规划+最大权闭合子图)

    传送门 好题啊. ∑i<jpi,jK∗(200−K)>X\frac{\sum_{i<j}p_{i,j}}{K*(200-K)}>XK∗(200−K)∑i<j​pi,j​​ ...

  5. 【XSY2718】gift 分数规划 网络流

    题目描述 有\(n\)个物品,买第\(i\)个物品要花费\(a_i\)元.还有\(m\)对关系:同时买\(p_i,q_i\)两个物品会获得\(b_i\)点收益. 设收益为\(B\),花费为\(A\), ...

  6. b2OJ_1565_[NOI2009]植物大战僵尸_拓扑排序+最大权闭合子图

    b2OJ_1565_[NOI2009]植物大战僵尸_拓扑排序+最大权闭合子 题意:n*m个植物,每个植物有分数(可正可负),和能保护植物的位置.只能从右往左吃,并且不能吃正被保护着的,可以一个不吃,求 ...

  7. BZOJ_4873_[Shoi2017]寿司餐厅_最大权闭合子图

    BZOJ_4873_[Shoi2017]寿司餐厅_最大权闭合子图 题意:http://www.lydsy.com/JudgeOnline/problem.php?id=4873 分析:我们发现分数正负 ...

  8. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

  9. HDU 3879 Base Station(最大权闭合子图)

    经典例题,好像说可以转化成maxflow(n,n+m),暂时只可以勉强理解maxflow(n+m,n+m)的做法. 题意:输入n个点,m条边的无向图.点权为负,边权为正,点权为代价,边权为获益,输出最 ...

随机推荐

  1. HTML5文件操作API

    HTML5文件操作API       一.文件操作API 在之前我们操作本地文件都是使用flash.silverlight或者第三方的activeX插件等技术,由于使用了这些技术后就很难进行跨平台.或 ...

  2. scrapy crawl xmlfeed spider

    from scrapy.spiders import XMLFeedSpider from myxml.items import MyxmlItem class XmlspiderSpider(XML ...

  3. Tornado 网站demo 一

    web服务器的工作过程 创建 listen socket, 在指定的监听端口, 等待客户端请求的到来 listen socket 接受客户端的请求, 得到 client socket, 接下来通过 c ...

  4. 在wamp集成环境下安装laravel5.2.*框架

    虽然官方一直强烈推荐使用homestead,但是这个相对麻烦一点,所以我还是选择使用wamp集成开发环境.还有这里我只讲解windows系统下的安装,其他例如mac或linux就不写了,此文章是面向刚 ...

  5. 15-TypeScript策略模式

    在前面的简单工厂模式中,通常将每个类.接口定义到不同的文件中.在面向对象开发思想中有一个重要的原则就是封装变化点,在实际操作过程中, 通常被调用方的代码不要去更改,而是增加,这是面向对象的开闭原则.在 ...

  6. monog和github学习

    1.导出服务器数据库到本地以json的格式储存:mongoexport -h ip -d dbname -c user -o D:\mondb\user.json2.导入本地Json到本地项目中:D: ...

  7. js new到底干了什么,new的意义是什么?

    学过JS的都知道 创建对象可以这样 var obj=new Object(); var obj=new Function(); 用内置的函数对象来构造对象 还可以这样自定义函数 function te ...

  8. mosquitto安装和测试

    一.安装 1.windows安装 安装完毕,更新安装目录的dll文件 2.linux安装 编译保存用户数据到数据库的插件 安装 3.启动 mosquitto mosquitto mosquitto_p ...

  9. Spring Security 入门(1-3-3)Spring Security - logout 退出登录

    要实现退出登录的功能我们需要在 http 元素下定义 logout 元素,这样 Spring Security 将自动为我们添加用于处理退出登录的过滤器 LogoutFilter 到 FilterCh ...

  10. vue.js+socket.io+express+mongodb打造在线聊天[二]

    vue.js+socket.io+express+mongodb打造在线聊天[二] 在线地址观看 http://www.chenleiming.com github地址 https://github. ...