【牛客 - 370E】Rinne Loves Gift(Bellman_Ford判负环,二分,分数规划)

题干:

链接:https://ac.nowcoder.com/acm/contest/370/E
来源:牛客网
 

Rinne 喜欢礼物,也喜欢送礼物
圣诞节快到了,Rinne 要去给给住在城市里的人送礼物
城市的交通可以抽象成一个 n 个点 m 条边的有向图
每条边上有 didi 个居民,Rinne 经过这条边的时候就会给她们每个人都送礼物
由于 Rinne 的礼物并不是很多,她只在城市平均居民数最少的路上送礼物
Rinne 不想破坏交通规则,于是她会选择一个能回到出发点的路
由于 Rinne 十分可爱,你需要求出这个平均值

输入描述:

第一个两个整数 n 和 m
接下来 m 行,每行三个整数 u,v,diu,v,di,表示一条从 u 到 v 居民数为 didi 的有向道路。

输出描述:

如果问题无解,也就是 Rinne 找不到一个能回到出发点的道路,则输出一行一个字符串`Rinne is cute`
否则,输出一行一个浮点数,表示平均损失值最小的回路的平均值大小,输出保留两位小数

示例1

输入

复制

2 2
1 2 2
2 1 3

输出

复制

2.50

示例2

输入

复制

2 1
1 2 1

输出

复制

Rinne is cute

备注:

1≤n≤2000,di≤109,m<=50001≤n≤2000,di≤109,m<=5000

解题报告:

  首先可以明确的是:如果图不存在环那么肯定无解(因为走不回去啊)。(但是对于这道题,可以直接融合在二分中了,以为你如果没有环,那就ans = -1,直接就输出 “Rinne is cute” 了)

那么我们可以把一种路径的答案表示为:( n 表示经过边的数量)

考虑枚举答案 ans,可以得到判断式,通过移项可以得到

那么每次二分这个答案 ans,然后把所有的边权都减去 ans,找一遍图中有没有负环就可以了。如果有的话说明 ans 还可以更低。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
const double eps = 1e-4;
const double INF = 1e9 + 2333;
int n,m;
double dis[MAX];
struct Edge {int u,v;double w;
} e[MAX],ee[MAX];
bool bell() {for(int i = 1; i<=n; i++) dis[i] = INF;for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(dis[e[j].u] + e[j].w < dis[e[j].v]) {dis[e[j].v] = dis[e[j].u] + e[j].w;}}}for(int j = 1; j<=m; j++) {if(dis[e[j].u] + e[j].w < dis[e[j].v]) return 1;//有负环}return 0 ;
}
bool ok(double x) {for(int i = 1; i<=m; i++) e[i] = ee[i];for(int i = 1; i<=m; i++) e[i].w -= x;bool res = bell();//for(int i = 1; i<=m; i++) e[i].w += x;//还原return res;
}int main()
{cin>>n>>m;double l = 0,r = INF;for(int i = 1; i<=m; i++) {scanf("%d%d%lf",&e[i].u,&e[i].v,&e[i].w);ee[i]=e[i];}double mid = (l+r)/2,ans = -1;while(l+eps < r) {mid = (l+r)/2;if(ok(mid)) r = mid,ans = mid;else l = mid;}if(ans < 0) printf("Rinne is cute\n");else printf("%.2lf\n",ans-eps);return 0 ;}

最后这个答案输出l也对,输出(l+ans)/2也对,,就是直接输出ans不对

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440782.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【POJ - 2976】【ZOJ - 3068】【SCU - 2992】Dropping tests (01分数规划)

题干&#xff1a; In a certain course, you take n tests. If you get ai out of bi questions correct on test i, your cumulative average is defined to be . Given your test scores and a positive integer k, determine how high you can make your cumulative aver…

重写过的url 怎么获取当前页面url java_网站URL重写(Java UrlRewrite 的使用)

现在大部分的网站和商城都会使用到URL重写&#xff0c;接触到这个&#xff0c;也是因为正在做的电子商务商城。URL重写&#xff0c;是将原有的URL采用另一种规则来显示&#xff0c;使得用户方便访问同时也屏蔽一些信息。在此说下它的好处&#xff0c;在开发过程中&#xff0c;经…

Java行业情景分析_Java 设计模式情景分析——单例模式

单例模式可以说是应用最广的模式之一&#xff0c;在应用单例模式时&#xff0c;单例对象的类必须保证只有一个实例存在&#xff0c;而且可以自行实例化并向整个系统提供这个实例。一般在不能自由构造对象的情况下&#xff0c;就会使用单例设计模式&#xff0c;例如创建一个对象…

php实现播放直播_PHP直播技术分享(一:实现直播)

推流服务器采用的是自搭的推流服务器 , 自己玩玩 做外包项目还是去搞七牛云/阿里这样的服务器吧,开始bb-----1:技术栈image.jpeg2:开发中业务(1)主播申请时创建个秘钥 , 这个秘钥随时字符串即可到时候根据字符串找到拉流的直播位置存数据库包括推流地址image.png3:配置nginx-rt…

php获取location,php获取header[‘location’]信息常见问题

15/01/31本文关键字: 302, header, location//初始化url信息$host “#8221;;$url$host.”l/rzTf7ap2viw/&iid222004556&resourceId0_04_05_99/v.swf”;//按照字段获取header响应信息$headers get_headers($url, TRUE);//获取这个土豆的302跳转地址$u302 $headers[“Lo…

php对联广告,html左右对联代码 cms网站对联广告html代码

实现网页左右两侧居中的对联广告代码你只记得自己有好多朋友却从没想过小编只有你一人。无标题文档 #left{ width:200px;height:450px; position:absolute; left:0px; background:red; border:1px #000 solid} #right{width:200px;height:450px; position:absolute; right:0px;…

php 分页 url重写 分页问题,解决千古难题,wordpress分页URL问题,wordpress给分页加链接...

原本的wordpress文章如果分页会成如下的结构&#xff1a;http://www.xyqzmt.cn/1.html/2通常固定链接都是这样的结构&#xff0c;设为/%postname%.html或者/%post_id%.html 以前我一直无法解决如上的问题&#xff0c;最后放弃挣扎&#xff0c;如果遇到很长的文章全都放在一个页…

【牛客 - 181F】子序列(容斥,组合数,费马小定理)

题干&#xff1a; 题目描述 给出一个长度为n的序列&#xff0c;你需要计算出所有长度为k的子序列中&#xff0c;除最大最小数之外所有数的乘积相乘的结果 输入描述: 第一行一个整数T&#xff0c;表示数据组数。 对于每组数据&#xff0c;第一行两个整数N&#xff0c;k&#…

【CodeForces - 485C】Bits (二进制相关,数学,贪心)

题干&#xff1a; Lets denote as the number of bits set (1 bits) in the binary representation of the non-negative integer x. You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, an…

php工程导致系统蓝屏,经常蓝屏是什么原因

经常蓝屏的原因&#xff1a;1、错误安装或更新显卡驱动后导致电脑蓝屏&#xff1b;2、超频过度是导致蓝屏&#xff1b;3、安装的软件存在不兼容&#xff1b;4、电脑内部硬件温度过高&#xff1b;5、内存条接触不良或内存损坏。错误更新显卡驱动错误安装或更新显卡驱动后导致电脑…

【CodeForces - 485A】Factory (水题,抽屉原理,tricks)

题干&#xff1a; One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to p…

matlab调用哈希表,ros与matlab联动使用

ros与matlab联动使用联动使用教程1 ubuntu18.04或16.04下安装matlab R2017b参考链接2 ubuntu下基于matlab启动rosmatlab中常用命令如下&#xff1a;查看robotics system toolbox工具箱是否安装成功help robotics.rosrosinit ——表示启动ROS&#xff0c;相当于roscorerosshutdo…

【CodeForces - 1020A】New Building for SIS(模拟)

题干&#xff1a; You are looking at the floor plan of the Summer Informatics Schools new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get …

【CodeForces - 1020B】Badge(模拟,图,环)

题干&#xff1a; In Summer Informatics School, if a student doesnt behave well, teachers make a hole in his badge. And today one of the teachers caught a group of nn students doing yet another trick. Lets assume that all these students are numbered from …

【Hihocoder - 1723】子树统计(线性基合并)

题干&#xff1a; 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵N个节点的有根树&#xff0c;树上每个节点有一个非负整数权重Wi。定义节点集合S{i1, i2, ……, ir}的总权重为&#xff1a;(⊕是异或运算) 每次询问一棵子树内所有可能出现的总权重数量&a…

【CodeForces - 485D】Maximum Value (枚举,用数组离散化,数学,取模运算,因子,筛法)

题干&#xff1a; You are given a sequence a consisting of n integers. Find the maximum possible value of (integer remainder of ai divided by aj), where 1 ≤ i, j ≤ n and ai ≥ aj. Input The first line contains integer n — the length of the seq…

【牛客 - 371牛客OI周赛7-提高组A】小睿睿的等式(dp,暴力 )

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/371/A 来源&#xff1a;牛客网 小睿睿在游戏开始时有n根火柴棒&#xff0c;他想知道能摆成形如“ABn”的等式且使用的火柴棒数也恰好等于n/k的等式有多少种(BAn与ABn看作一种) 注&#xff1a; “”与…

oracle用户新增数据文件,[数据库]20200722_Oracle添加表空间、用户,用户授权

[数据库]20200722_Oracle添加表空间、用户&#xff0c;用户授权0 2020-07-25 17:00:30--创建表空间CREATE TABLESPACE aifu --表空间名 aifu LOGGING DATAFILE D:\dev_config\OracleTableSpaces\aifu.DBF SIZE 5M --数据文件路径 D:\dev_config\OracleTableSpaces AUTOEXTEND O…

【牛客 - 327牛客寒假算法基础集训营2 I】处女座的测验(二)(积性函数性质,数论,素数唯一性分解,STL)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/327/I 来源&#xff1a;牛客网 现在处女座顺利的完成了测验&#xff0c;处女座想要知道知道自己输出的结果是否正确。他希望知道自己有自己输出的数中有多少对是不满足要求的。 更具体的&#xff0c…

linux监控命令执行,你可能不知道的 即时监控 Linux 使用者执行指令的三种方法...

原标题&#xff1a;你可能不知道的 即时监控 Linux 使用者执行指令的三种方法这里介绍如何在 Linux 系统上以管理者权限即时监控一般使用者所执行的任何指令。Linux 的 root 管理者可对系统进行任何的管理与操作&#xff0c;如果想要即时监控特定使用者在主机上所执行的指令&am…