题目大意:

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. 使用Python定制词云

    一.实验介绍 1.1 实验内容 在互联网时代,人们获取信息的途径多种多样,大量的信息涌入到人们的视线中.如何从浩如烟海的信息中提炼出关键信息,滤除垃圾信息,一直是现代人关注的问题.在这个信息爆炸的时代 ...

  2. jQuery函数学习

    函数:after(content) 功能:在每个匹配的元素后面添加html内容 返回:jQuery对象 参数:content (<Content>): Content to insert ...

  3. python 进程复习

    import os import time ret = os.fork() # 创建子线程 if ret ==0: # 子进程中返回值为0,父进程>0 while True: print('.. ...

  4. logging日志

    import logging logging.basicConfig(filename='log.log', format='%(asctime)s - %(name)s - %(levelname) ...

  5. bzoj千题计划177:bzoj1858: [Scoi2010]序列操作

    http://www.lydsy.com/JudgeOnline/problem.php?id=1858 2018 自己写的第1题,一遍过 ^_^ 元旦快乐 #include<cstdio> ...

  6. 算法题丨3Sum Closest

    描述 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  7. 前端基础之html-Day12

    1.web服务本质 import socket def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bi ...

  8. c++中模板是什么?为什么要定义模板?

    一.c++中模板是什么? 首先: int Max(int x, int y) { return x > y ? x : y; } float Max(float a,float b) { ret ...

  9. 批量检测GoAhead系列服务器中Digest认证方式的服务器弱口令

    最近在学习用python写爬虫工具,某天偶然发现GoAhead系列服务器的登录方式跟大多数网站不一样,不是采用POST等方法,通过查找资料发现GoAhead是一个开源(商业许可).简单.轻巧.功能强大 ...

  10. ssh整合之六管理我们的配置文件

    1.我们的ssh搭建已经搭建好了,之前是纯xml方式,而且我们的配置文件,是一个框架一个配置文件.这样的话,配置文件中的内容就会很多,这样以后修改起来也会很麻烦,因        此,我们尝试着把这些 ...