【洛谷P2868】Sightseeing Cows
题目大意:给定一个 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的更多相关文章
- 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows
P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题目描述 Farmer John has decided to reward his cows for their har ...
- 洛谷P2868 [USACO07DEC]观光奶牛 Sightseeing Cows
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows(01分数规划)
题意 题目链接 Sol 复习一下01分数规划 设\(a_i\)为点权,\(b_i\)为边权,我们要最大化\(\sum \frac{a_i}{b_i}\).可以二分一个答案\(k\),我们需要检查\(\ ...
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解
题面 这道题是一道标准的01分数规划: 但是有一些细节可以优化: 不难想到要二分一个mid然后判定图上是否存在一个环S,该环是否满足∑i=1t(Fun[vi]−mid∗Tim[ei])>0 但是 ...
- 【POJ3621】【洛谷2868】Sightseeing Cows(分数规划)
[POJ3621][洛谷2868]Sightseeing Cows(分数规划) 题面 Vjudge 洛谷 大意: 在有向图图中选出一个环,使得这个环的点权\(/\)边权最大 题解 分数规划 二分答案之 ...
- POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows
一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\( ...
- P2868 [USACO07DEC]观光奶牛Sightseeing Cows
P2868 [USACO07DEC]观光奶牛Sightseeing Cows [](https://www.cnblogs.com/images/cnblogs_com/Tony-Double-Sky ...
- tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows
缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...
随机推荐
- Day3-1 函数
定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 特性: 减少重复代码 使程序变的可扩展 使程序变得易维护 语法: def calc(x, y): ...
- C# Note11:如何优雅地退出WPF应用程序
前言 I should know how I am supposed to exit my application when the user clicks on the Exit menu item ...
- Java中 VO、 PO、DO、DTO、 BO、 QO、DAO、POJO的概念(转)
PO(persistant object) 持久对象 在 o/r 映射的时候出现的概念,如果没有 o/r 映射,没有这个概念存在了.通常对应数据模型 ( 数据库 ), 本身还有部分业务逻辑的处理.可以 ...
- java学习之—数组的曾删改查
/** * 数组的曾删改查 * Create by Administrator * 2018/6/8 0008 * 上午 9:54 **/ public class HighArray { priva ...
- windows 10 & task view & shortcut
windows 10 & task view & shortcut Win + Tab https://blogs.windows.com/windowsexperience/2014 ...
- Map接口----Map中嵌套Map
package cn.good.com; import java.util.HashMap; import java.util.Iterator; import java.util.Map; impo ...
- vue之综合Demo:打沙袋
demo7.html <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/1 ...
- MySQL——基础操作
参考博客:http://www.cnblogs.com/wupeiqi/articles/5713315.html 1.创建用户.授权(默认root,密码为空) 创建: create user 'al ...
- CF 1041 1042整理
终于回来整理了,这两场比赛我也是醉了,第一场-1分,第二场被skip,还是太菜啊qaq CF1041 T1T2过水忽略直接看后面 T3大意:给你一个长度为n的序列a1,a2,a3···an,你需要把这 ...
- codeforces-div2-449-B
题意:确定一个回文偶数十进制数字,输入k和q,求前k小的和对q取余的值 解题思路:首先确定一个,第k个回文偶数一定前半段一定是k,比如第12个,这个数就是1221: 代码: #include<i ...