题面

这道题是一道标准的01分数规划:

但是有一些细节可以优化:

不难想到要二分一个mid然后判定图上是否存在一个环S,该环是否满足∑i=1t(Fun[vi]−mid∗Tim[ei])>0

但是上面的算法并不好实现,所以可以将两边同时乘上-1,使式子变为∑i=1t​(mid∗Tim[ei​]−Fun[vi​])<0

那么该问题就转化成了在每一个图中跑一边SPFA来寻找是否存在负环,若存在则l=mid,否则r=mid;

#include <bits/stdc++.h>
#define inc(a,b,c) for(register int i=a;i<=b;i+=c)
#define ini 20010
using namespace std;
int n,m;
struct littlestar{
int from;
int to;
int nxt;
double w;
}star[ini],star2[ini];
int head[ini],cnt,head2[ini];
void add(int u,int v,int w)
{
star[++cnt].to=v;
star[cnt].nxt=head[u];
star[cnt].from=u;
star[cnt].w=w;
head[u]=cnt;
}
int c[ini];
queue<int> q;
double dis[ini];
int vis[ini];
int SPFA()
{
int tot=;
for(int i=;i<=n;i++){
q.push(i);
dis[i]=;
vis[i]=;
}
while(q.size()){
int u=q.front();
q.pop();
vis[u]=;
for(int i=head2[u];i;i=star2[i].nxt){
int v=star2[i].to;
if(dis[v]>dis[u]+star2[i].w){
dis[v]=dis[u]+star2[i].w;
if(vis[v]==){
vis[v]=;
q.push(v);
++tot;
if(tot>*(n+m)) return ;
}
}
}
}
return ;
}
int check(double x)
{
inc(,cnt,){
star2[i]=star[i];
star2[i].w=(double)star[i].w*(double)x-(double)c[star[i].from];
}
inc(,n,){
head2[i]=head[i];
}
if(SPFA()){
return ;
}
else{
return ;
}
}
int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
int main()
{
n=read();
m=read();
inc(,n,) c[i]=read();
inc(,m,){
int u,v,w;
u=read();v=read();w=read();
add(u,v,w);
}
double l=0.00,r=1000010.000,mid;
while(r-l>1e-){
mid=(l+r)/;
if(check(mid)){
l=mid;
}
else{
r=mid;
}
}
printf("%.2lf",l);
}

洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解的更多相关文章

  1. 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows

    P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题目描述 Farmer John has decided to reward his cows for their har ...

  2. 洛谷P2868 [USACO07DEC]观光奶牛 Sightseeing Cows

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

  3. 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

  4. 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows(01分数规划)

    题意 题目链接 Sol 复习一下01分数规划 设\(a_i\)为点权,\(b_i\)为边权,我们要最大化\(\sum \frac{a_i}{b_i}\).可以二分一个答案\(k\),我们需要检查\(\ ...

  5. POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows

    一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\( ...

  6. 洛谷 2868 [USACO07DEC]观光奶牛Sightseeing Cows

    题目戳这里 一句话题意 L个点,P条有向边,求图中最大比率环(权值(Fun)与长度(Tim)的比率最大的环). Solution 巨说这是0/1分数规划. 话说 0/1分数规划 是真的难,但貌似有一些 ...

  7. P2868 [USACO07DEC]观光奶牛Sightseeing Cows

    P2868 [USACO07DEC]观光奶牛Sightseeing Cows [](https://www.cnblogs.com/images/cnblogs_com/Tony-Double-Sky ...

  8. 洛谷 P3088 [USACO13NOV]挤奶牛Crowded Cows 题解

    P3088 [USACO13NOV]挤奶牛Crowded Cows 题目描述 Farmer John's N cows (1 <= N <= 50,000) are grazing alo ...

  9. [USACO07DEC]观光奶牛Sightseeing Cows 二分答案+判断负环

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

随机推荐

  1. UVA 10900 So do you want to be a 2^n-aire?

    #include<bits/stdc++.h> #include<stdio.h> #include<iostream> #include<cmath> ...

  2. UVA 11346 Possibility

    #include<bits/stdc++.h> #include<stdio.h> #include<iostream> #include<cmath> ...

  3. POJ 3068 运送危险化学品 最小费用流 模板题

    "Shortest" pair of paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1215 ...

  4. 原生Js_实现简单选项卡功能

    javascript实现选项卡功能,在<script>...</script>中编写代码 实现步骤 a)获得各操作的dom对象: b)在所有按钮对象上添加单击事件: c)设置所 ...

  5. Unity3D_(游戏)双人3D坦克_简易版

    双人3D坦克实现 player1: WSAD控制上下左右 空格键发射炮弹 player2: IKJL可控制上下左右 B键发射炮弹 每个坦克只有100hp,子弹击中1次扣30hp,hp时时显示在坦克上 ...

  6. [EOJ Monthly2019.11][T1]纸条

    https://acm.ecnu.edu.cn/ 华东师范大学在线评测网站 今天这个题目来自华东师范大学的校赛,比icpc稍难一些,在2019年11月29日周五19:30开始,持续2.5个小时 以下是 ...

  7. JS箭头函数的this

    箭头函数的this看定义他的时候,他的外层有没有函数 有:外层函数的this就是箭头函数的this 无:箭头函数的this就是window obj = {age:18, getAge: ()=> ...

  8. 剑指offer32----之字形打印一颗二叉树

    题目 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. 思路 在这里我们如果单纯的使用队列去弄的话,会很 ...

  9. 【转】如何在Oracle中复制表结构和表数据

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  10. spring的IOC——依赖注入的两种实现类型

    一.构造器注入: 构造器注入,即通过构造函数完成依赖关系的设定.我们看一下spring的配置文件: <constructor-arg ref="userDao4Oracle" ...