题目大意:

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. 《Language Implementation Patterns》之 符号表

    前面的章节我们学会了如何解析语言.构建AST,如何访问重写AST,有了这些基础,我们可以开始进行"语义分析"了. 在分析语义的一个基本方面是要追踪"符号",符号 ...

  2. 用python实现简单购物车功能

    all_asset = 0 i1 = input("请输入总资产:") all_asset = int(i1) goods = [ {'name':'电脑','price':199 ...

  3. 使用HttpClient4.5实现HTTPS的双向认证

    说明:本文主要是在平时接口对接开发中遇到的为保证传输安全的情况特要求使用https进行交互的情况下,使用httpClient4.5版本对HTTPS的双向验证的  功能的实现    首先,老生常谈,文章 ...

  4. JAVA_SE基础——7.常量&变量

    上一篇,我讲了标识符&关键字    这篇我来解释下变量&常量~~~ 变量与常量这两个概念相信大家都不会感到陌生,在数学中就已经涉及了变量与常量.理解变量与常量,可以举这样一个例子: 例 ...

  5. ubuntu启动报/root/.profile mesg:ttyname failed错误的解决办法

    修改/root/.profile文件,如下命令 sudo gedit /root/profile 将文中的最后一行mesg n修改成tty -s && mesg n

  6. restful架构风格设计准则(六)版本管理

    读书笔记,原文链接:http://www.cnblogs.com/loveis715/p/4669091.html,感谢作者! 版本管理 在前面已经提到过,一个REST系统为资源所抽象出的URI实际上 ...

  7. leetcode算法: Find the Difference

    Given two strings s and t which consist of only lowercase letters.String t is generated by random sh ...

  8. Python入门之函数的介绍/定义/定义类型/函数调用/Return

    本篇目录: 一. 函数的介绍 二. 函数的定义 三. 定义函数的三种类型 四. 函数调用的阶段 五. Return返回值 ======================================= ...

  9. Ajax.html:35 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org

    AJAX的容易错误的地方 Ajax.html:35 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated ...

  10. Java-Maven(六):Eclipse中Maven插件的命令操作

    之前几个章节学习了maven的概念,及maven插件安装后如何创建工程,那么maven工程中是如何使用maven命令呢?本章节将会学习这个话题. 在pom.xml中配置maven命令插件 如果向在ma ...