POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】

[POJ3621] Sightseeing Cows 题意LL个点,PP边的点边带权的有向图,求一个环点权和与边权和比值的最大值。题解01分数规划,点权为valueivalue_i,边权为costicost_i,最大化∑valuei/∑costi\sum value_i / \sum cost_i。 01分数规划讲解 用了二分法。 实现时用到了spfa的dfs版本判负环,精度1E-4,交G++ 0MS。代码/// by ztx /// blog. 阅读详情

题目链接:http://poj.org/problem?id=3621

Sightseeing Cows

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11526 Accepted: 3930

Description

Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to spend their free time.

Fortunately, they have a detailed city map showing the L (2 ≤ L ≤ 1000) major landmarks (conveniently numbered 1.. L) and the P (2 ≤ P ≤ 5000) unidirectional cow paths that join them. Farmer John will drive the cows to a starting landmark of their choice, from which they will walk along the cow paths to a series of other landmarks, ending back at their starting landmark where Farmer John will pick them up and take them back to the farm. Because space in the city is at a premium, the cow paths are very narrow and so travel along each cow path is only allowed in one fixed direction.

While the cows may spend as much time as they like in the city, they do tend to get bored easily. Visiting each new landmark is fun, but walking between them takes time. The cows know the exact fun values Fi (1 ≤ Fi ≤ 1000) for each landmark i.

The cows also know about the cowpaths. Cowpath i connects landmark L1i to L2i (in the direction L1i -> L2i ) and requires time Ti (1 ≤ Ti ≤ 1000) to traverse.

In order to have the best possible day off, the cows want to maximize the average fun value per unit time of their trip. Of course, the landmarks are only fun the first time they are visited; the cows may pass through the landmark more than once, but they do not perceive its fun value again. Furthermore, Farmer John is making the cows visit at least two landmarks, so that they get some exercise during their day off.

Help the cows find the maximum fun value per unit time that they can achieve.

Input

* Line 1: Two space-separated integers: L and P
* Lines 2..L+1: Line i+1 contains a single one integer: Fi
* Lines L+2..L+P+1: Line L+i+1 describes cow path i with three space-separated integers: L1i , L2i , and Ti

Output

* Line 1: A single number given to two decimal places (do not perform explicit rounding), the maximum possible average fun per unit time, or 0 if the cows cannot plan any trip at all in accordance with the above rules.

Sample Input

5 7
30
10
10
5
10
1 2 3
2 3 2
3 4 5
3 5 2
4 5 5
5 1 3
5 2 2

Sample Output

6.00

Source

 

题目概括:

有 L 个 landmarks, P条 cow path(有向边),每个点可获得娱乐值 Fi ,不过每条边需要花费时间 Ti,我们要求的是选走任意几个点(路径要构成一个环)单位时间获得最大的娱乐值即 求 ΣFi / ΣTi 的最大值。

解题思路:

要从N中取K,并且求 ΣFi / ΣTi 的最大值,很明显用二分+01分数规划

令 ΣFi / ΣTi >= X, 则 ΣFi - ΣTi*X >= 0, 也就转换为了求这个有向图是否存在正环,我们直到SPFA可以轻松通过dfs判断点的访问次数来判断是否有负环,我们只需要把SPFA的求最短路的判断条件换成求最长路的判断条件即可以判断是否存在正环了。

 

AC code:

 1 ///POJ 3621 01分数规划+SPFA判断正环
 2 #include <iostream>
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 #include <algorithm>
 6 #include <string.h>
 7 #include <vector>
 8 #include <math.h>
 9 #include <limits>
10 #include <set>
11 #include <map>
12 #define INF ox3f3f3f3f
13 using namespace std;
14 
15 const int MAXN = 505050;
16 int v[MAXN];       ///点权
17 int fst[MAXN], vb[MAXN], vc[MAXN], nxt[MAXN];
18 bool vis[MAXN], fh;                     ///记录访问点的次数
19 double dist[MAXN];    ///用于判断正环
20 int N, M, cnt;
21 
22 void add(int a, int b, int c)   ///静态邻接表
23 {
24     ++cnt;
25     nxt[cnt] = fst[a];
26     fst[a] = cnt;
27     vb[cnt] = b;
28     vc[cnt] = c;         ///边权
29 }
30 
31 void spfa_dfs(int p, double x)
32 {
33     vis[p] = true;
34     for(int e = fst[p]; e; e = nxt[e])
35     {
36         double C = v[vb[e]]-x*vc[e];
37         if(dist[vb[e]] >= C+dist[p]) continue;   ///与spfa判断负环恰好相反,只取大的
38         if(vis[vb[e]])
39         {
40             fh = 1; return;
41         }
42         dist[vb[e]] = C+dist[p];
43         spfa_dfs(vb[e], x);
44         if(fh) return;
45     }
46     vis[p] = 0;
47 }
48 
49 bool ok(double x)
50 {
51     for(int i = 1; i <= N; i++)
52     {
53         vis[i] = dist[i] = 0;
54     }
55     fh = 0;
56     for(int i = 1; i <= N; i++)
57     {
58         spfa_dfs(i, x);
59         if(fh) return true;   ///有正环
60     }
61     return false;
62 }
63 int main()
64 {
65     scanf("%d%d", &N, &M);
66     for(int i = 1; i <= N; i++)
67         scanf("%d", &v[i]);
68     for(int i = 1; i <= M; i++)
69     {
70         int a, b, c;
71         scanf("%d%d%d", &a, &b, &c);
72         add(a, b, c);
73     }
74     double l = 0, r = 1000000000;
75     while(r-l>1e-6)
76     {
77         double mid = (l+r)/2.0;
78         if(ok(mid)) l = mid;
79         else r = mid;
80     }
81     printf("%.2lf\n", l);
82 
83     return 0;
84 }

 

(三十九)Flask 多应用架构(Multiple Apps)与 DispatcherMiddleware 实战解析 阅读详情

相关推荐

【小白指南】如何将deepseek引入WPS\Word\Excel中去?

如何将deepseek引入WPS\Word\Excel中去?纯小白都能看的懂,一步步操作就能行

2503_90498422的博客 1万+

POJ3621 sightseeing cows 01分数规划+SPFA判定

传送门 题目:给出N个点M条有向边,要求选出一个环,使得这上面 点权和/边权和 最大。 思路:明显的01分数规划,只是这里的判定不是很好搞。我们先按着分数规划的思路来。设Xi={0,1}Xi = \{0, 1\},Yi={0,1}Yi = \{0, 1\}, R=∑Vi∗Xi∑Wj∗Yj\ R = \frac{∑Vi*Xi}{∑Wj*Yj} 所以有判定函数 F(R)=∑Vi∗Xi−R∗∑Wi∗

geng4512的专栏 1475

html python mysql交互_干货!Python与MySQL数据库的交互实战

安装PyMySQL库如果你想要使用python操作MySQL数据库,就必须先要安装pymysql库,这个库的安装很简单,直接使用pip install pymysql;假如这种方式还是安装不上,就用如下链接找一个合适的安装包,进行安装,这个就不细说了。https://www.lfd.uci.edu/~gohlke/pythonlibs/使用Python连接MySQL数据库1)六个常用的连接参数参数...

weixin_31043555的博客 1561

SPFA(判负环) + 二分(01分数规划) - Sightseeing Cows - POJ 3621

SPFA(判负环) + 二分(01分数规划) - Sightseeing Cows - POJ 3621 题意: 给定一张L个点、P条边的有向图,每个点都有一个权值f[i],每条边都有一个权值t[i]。 求图中的一个环,使“环上各点的权值之和”除以“环上各边的权值之和”最大。 输出这个最大值。 注意: 数据保证至少存在一个环。 输入格式 第一行包含两个整数L和P。 接下来L行每行一个整数,表示f[i]。 再接下来P行,每行三个整数a,b,t[i],表示点a和b之间存在一条边,边的权值为t[i]。 输出格式

njuptACMcxk的博客 265

基于FPGA的超声成像算法的硬件设计

顶层设计如下图: 1. Verilog的硬件设计型号是 ALTERA 2.数据的输入量已经给出,附上的四个矩阵输入都要实现。 3.波束形成部分要在FPGA上完成,最后的matlab的代码部分只是获取数据后的图像显示 4.不需要动态聚焦,只是完成固定的四个不同输入数据的显示即可 5.不需要实物,但要实现硬件的仿真,要有相应的模块图和结构图,类似如下 具体实现过程和最终结果如下: 定义aperture(孔径)大小————输入量,数据已给出,一共四个规格的ape...

FPGA/MATLAB学习教程/源码/项目合作开发 1万+

Android 14安装的APK默认允许运行时权限

本文介绍了在Android系统中实现安装应用后自动授予运行时权限的方法。通过在安装完成广播中检测应用安装状态,调用新增的DefaultPermissionGrantPolicy.grantAppRuntimePermissions()函数来授予常规权限,并使用allowSpecialPermission()处理特殊权限(如安装未知应用和悬浮窗权限)。文章详细说明了如何修改DefaultPermissionGrantPolicy类,添加grantAppRuntimePermissions和grantRunti

热衷于Android Framework和系统APP的修改经验分享 686

POJ 3621 Sightseeing Cows

POJ 3621 Sightseeing Cows图论,最优比率环路,01规划 传送门:POJ 题意一群奶牛(奶牛。。。)要出去玩,一张无向图,每个点有欢乐值,每条边有路程。求一个旅行路线(环,从哪出发回到哪),每个地方可以走多次,但欢乐值只计算一次。使得总欢乐值/总路程 值最大。思路 首先是01规划问题。01规划大概就是,给定两个数组,a[i]表示选取i的收益,b[i]表示选取i的代价。如果选

P.H.S 407

SAP 调用Ftp服务器

FTP(File transfer protocol)是一种标准的网络协议,可以用于在互联网上传递文件。SAP系统中可通过系列配置和提供的些标准函数,用于查看、更新、下载FTP服务器上的文件。提示:以下是本篇文章正文内容,下面案例可供参考。

qq_38304013的博客 1418

POJ3621 Sightseeing Cows

环形分数规划 正常判环方式(SPFA正,队列,无SLF) Memory: 2464K Time: 360MS http://paste.ubuntu.com/15575107/ 超快判环(抄题解,DFS版SPFA) Memory: 768K Time: 94MS 代码同上,将spfa改成spfa2即可体验飞的感觉 题意找出图中一个环,使环上∑m

guozizheng001的博客 963

TC8:IPv4_HEADER_01-04

IPv4_HEADER_01: Ensure that the DUT generates an IPv4 Packet with a Total Length greater than or equal to 20 目的 确保DUT生成的IPv4报文的IP头部中的Total Length字段值大于等于20 IPv4协议头部中的Total Length字段表示整个IP数据包的长度,包括IP Header和payload之和,IP首部的长度最少就有20个字节,所以Total Length值肯定大于等于20

jasonj333 3969

【WRF-Chem工具】pyVPRM 计算大气与陆地植被之间的 CO₂ 交换通量

pyVPRM 计算大气与陆地植被之间的 CO₂ 交换通量

WW、forever的博客 211

POJ3621 Sightseeing Cows(最优比率环)

题目链接:http://poj.org/problem?id=3621 在一个有向图中选一个环,使得环上的点权和除以边权和最大,求这个比值。 经典的分数规划问题,我觉得这两篇题解写得很清楚,可以参考一下http://blog.youkuaiyun.com/gengmingrui/article/details/47443705,http://blog.youkuaiyun.com/wall_f/article/det

ACMer_quack&cheer 1695

MAC-电脑中的user文件夹在哪?

MAC-电脑中的user文件夹在哪?

兴趣是最好的老师 1万+

poj 3621 Sightseeing Cows(最优比例生成环,01分数规划)

http://poj.org/problem?id=3621 大致题意:给出一个有向图,每个点都有一个点权,每条有向边也都有一个边权,要求出一个环使得环中点权之和与边权之和的比值最大。 思路:和最优比率生成树异曲同工。设点权是v[i],边权是e[i]。不同的是这里一个是点,一个是边。怎么像生成树一样把这两个值放到一起呢?可以把他们都转化到边上。同样的二分λ,每次给边重新赋权为v

1435

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

poj-3621-Sightseeing Cows-01分数规划+spfa判负环

假如最终的答案是re。 那么对每条边进行一定的处理。 然后用spfa判断是否出现了负环。 如果出现了负环。说明所取的re偏小。 就这样二分re。 spfa判断负环的方法是如果某个点入队列超过n次,那么图中存在负环。 #include #include #include #include #include #include #include #include #include #inc

青竹梦 1349

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Sightseeing Cows-POJ3621——01分数规划,SPFA判负环

http://poj.org/problem?id=3621   01规划,二分答案,重组边权,SPFA判负环。 code: 1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 using namespace std; 5 const int N=1000+10; ...

weixin_30614587的博客 137

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

[POJ 3621] Sightseeing Cows

[题目链接] http://poj.org/problem?id=3621 [算法] 01分数规划(最优比率环) [代码] #include <algorithm> #include <bitset> #include <cctype> #include <c...

100

Sightseeing Cows(POJ-3621)

Problem Description Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to spend their free time. Fortunately, the...

Alex_McAvoy的博客 441

poj 3621 Sightseeing Cows(最优比率环)

传送门:QAQ 题意:每个点都有兴趣值,每条边都有花费时间,让你找出一个环,环上的兴趣值和除以总时间和最大,问你最大的值是多少。 思路:很明显,只要你会01分数规划,你就会发现这个只是将check的变在了图上,所以我们列出式子∑Fi-ans∑Ti<=0.然后我们二分ans,然后将图中的边变为Fi-ans*Ti,这样我们只要判断图中是否有正环就可以判断这个式子是否成立,但是这样...

liexss的博客 197

POJ 3621 Sightseeing Cows 01分数规划(参数搜索)+最短路

题目大意:在一个有向图中,点有点权,边有边权,找一个环使环上的点权之和与边权之和的比最大。 ( 学名: 最优比率生成环..? ) 解题思路: 首先必备的知识点是分数规划,还得去看Amber的《最小割模型在信心学竞赛中的应用》....(貌似近期每篇都要提一提...) 可以跳过前面的直接从第十页开始看, 了解分数规划特别是01分数规划。 总的来说,就是求一个环,使得环上经过的点的点权Vi

Veda_的专栏 868

spfa+01分数规划——poj3621 Sightseeing Cows

http://poj.org/problem?id=3621 Description Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to spend their free ti...

weixin_44630656的博客 212

Sightseeing Cows POJ - 3621 01分数划分+SPFA求正环

题目描述 给定一张L个点、P条边的有向图,每个点都有一个权值f[i],每条边都有一个权值t[i]。 求图中的一个环,使“环上各点的权值之和”除以“环上各边的权值之和”最大。 输出这个最大值。 注意:数据保证至少存在一个环。 输入格式 第一行包含两个整数L和P。 接下来L行每行一个整数,表示f[i]。 再接下来P行,每行三个整数a,b,t[i],表示点a和b之间存在一条边,边的权值为t[i]。 输出...

正月看雪花的博客 202

POJ 3621 Sightseeing Cows(01分数规划之最优比率生成环)

原题地址:http://poj.org/problem?id=3621 题意:给定一张图,边上有花费,点上有收益,点可以多次经过,但是收益不叠加,边也可以多次经过,但是费用叠加。求一个环使得收益和/花费和最大,输出这个比值。 思路:说下我遇到的问题和我的理解 vvv是点的集合,eee是边的集合 假设ansansans是最优解,那么ΣV[i]Σe[i]&lt;=ansΣV[i]Σe[i]&l...

yiqzq的博客 414

POJ-3621: Sightseeing Cows【最优比率生成环】

Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10276   Accepted: 3510 Description Farmer John has decided to reward his cows for their hard

swust_Three的博客 709

POJ 3621 Sightseeing Cows 最大密度环 01分数规划

最大密度环 01分数规划    首先的一个结论就是,不会存在环套环的问题,即最优的方案一定是一个单独的环,而不是大环套着小环的形式。这个的证明其实非常的简单,大家可以自己想一下(提示,将大环上的收益和记为x1,花费为y1,小环上的为x2,y2。重叠部分的花费为S。表示出来分类讨论即可)。有了这个结论,我们就可以将花费和收益都转移到边上来了,因为答案最终一定是一个环,所以我们将每一条边

码代码的猿猿 1229

[POJ3621]Sightseeing Cows(01分数规划)

题目描述传送门 题意:一张图,每一个点有价值,每一条边有花费,求一条起点终点相同的路径,满足价值/花费最小。其中点重复经过价值不变,边重复经过代价累加。题解一定不会重复走边对吧…所以是一个最优比率环问题 将边的价值看成是起点或终点的价值,二分R之后,对每一条边计算di=ai−R∗bid_i=a_i-R*b_i 然后用spfa判断是否有正权环就行了,如果有的话一定存在更优解代码#include<

zyf2000 537
上一篇: POJ 2976 Dropping tests 【01分数规划+二分】
下一篇: (第六场)Singing Contest 【模拟】
莜莫oO
博客等级 码龄9年 7粉丝 226原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值