题目大意:给定一个 N 个点,M 条边的有向图,点有点权,边有边权,求该有向图中的一个环,使得环上点权和与环上边权和之比最大。

题解:0/1 分数规划思想,每次二分一个 mid,在新图上跑 spfa,将问题转化成是否存在负环即可。

代码如下

#include <bits/stdc++.h>
using namespace std;
const int maxn=1010;
const double eps=1e-4; struct node{int to;double w;};
vector<node> G[maxn],res[maxn];
int n,m;
double ans,val[maxn];
double d[maxn];bool in[maxn];int cnt[maxn]; void read_and_parse(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%lf",&val[i]);
for(int i=1,x,y;i<=m;i++){
double z;
scanf("%d%d%lf",&x,&y,&z);
G[x].push_back(node{y,z});
}
} bool spfa(){
queue<int> q;
fill(d+1,d+n+1,1e9);
fill(in+1,in+n+1,0);
fill(cnt+1,cnt+n+1,0);
d[1]=0,in[1]=1,q.push(1);
while(q.size()){
int u=q.front();q.pop(),in[u]=0;
if(cnt[u]>=n)return 1;
for(int i=0;i<res[u].size();i++){
int v=res[u][i].to;double w=res[u][i].w;
if(d[v]>d[u]+w){
d[v]=d[u]+w,cnt[v]=cnt[u]+1;
if(!in[v])q.push(v),in[v]=1;
}
}
}
return 0;
} bool right(double mid){
for(int i=1;i<=n;i++)res[i].clear();
for(int i=1;i<=n;i++)
for(int j=0;j<G[i].size();j++)
res[i].push_back(node{G[i][j].to,mid*G[i][j].w-val[i]});
return spfa();
} void solve(){
double l=0,r=1010;
while(r-l>eps){
double mid=(l+r)/2.0;
if(right(mid))l=mid;
else r=mid;
}
printf("%.2lf\n",l);
} int main(){
read_and_parse();
solve();
return 0;
}

【洛谷P2868】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. 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解

    题面 这道题是一道标准的01分数规划: 但是有一些细节可以优化: 不难想到要二分一个mid然后判定图上是否存在一个环S,该环是否满足∑i=1t(Fun[vi]−mid∗Tim[ei])>0 但是 ...

  6. 【POJ3621】【洛谷2868】Sightseeing Cows(分数规划)

    [POJ3621][洛谷2868]Sightseeing Cows(分数规划) 题面 Vjudge 洛谷 大意: 在有向图图中选出一个环,使得这个环的点权\(/\)边权最大 题解 分数规划 二分答案之 ...

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

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

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

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

  9. tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows

    缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...

随机推荐

  1. Microsoft Visual Studio Ultimate 2013密钥

    Visual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9Visual Studio Premium 2013 KEY(密钥): ...

  2. html 引入页面公共部分(header、footer)

    html引入页面的公共部分,比如导航栏啊,页头页脚之类的. 1.将需要引入的公共html部分转换为js文件,这里推荐一个转换工具地址 http://tool.chinaz.com/Tools/Html ...

  3. 【转】说说MySQL中的Redo log Undo log都在干啥

    阅读目录(Content) 1 undo 1.1 undo是啥 1.2 undo参数 1.3 undo空间管理 2 redo 2.1 redo是啥 2.2 redo 参数 2.3 redo 空间管理 ...

  4. Alertmanager 安装(k8s报警)

    一.下载Alertmanager https://prometheus.io/download/ wget https://github.com/prometheus/alertmanager/rel ...

  5. Java的hashCode和equals方法

    当然健壮的代码,两个都重写那是最好. 用不到hashCode的, 也没有必要重写hashCode. 个人感觉. 哈希机制的Java集合类,例如 Hashtable, HashMap, HashSet ...

  6. Lodop“对象不支持SET__LICENSES属性或方法”SET__LICENSES is not a function”

    Lodop中的方法如果书写错误,就会报错:“对象不支持XXX属性或方法”调试JS会报错”SET__LICENSES is not a function” LODOP.SET_LICENSES是加注册语 ...

  7. 简单触发器实例insert

    create or replace trigger tr_tb_if_archivesafter inserton tb_if_archivesfor each rowdeclarepragma au ...

  8. 自定义 ASP.NET Identity Data Model with EF

    One of the first issues you will likely encounter when getting started with ASP.NET Identity centers ...

  9. ubuntu终端快捷键

    ctrl+alt+t 新终端 ctrl+shift+t打开新的标签页 ctrl+d关闭终端 ctrl+s 暂停屏幕输出 ctrl+q 继续屏幕输出 ctrl+l 清屏 ctrl+alt+f1 切换到第 ...

  10. oracle复习(二)

    十一.replace 替换格式:(原字符串,要查找的字符或字符串,替换的字符或字符串)select replace('hello world','o','a') from dual; //替换时区分大 ...