Sightseeing Cows

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
题意:求解一个图上的环上点权和除边权和最大值
sigma(val[i])/sigma(w[i])最大
和求最优比率生成树类似 sigma(val[i])/sigma(w[i])>=x;
-->sigma(val[i]-w[i]*x)>=0;
由于题目说的是图上的环 我们可以转化成图上的负权环 用spfa求解
如何转换负权环,我们将u-v边的边权变为x*edge[i].w-val[u];
所以在二分的时候 跑spfa判断是否是负环 是的话则l=mid;否则r=mid;
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.000001;
const int N=+;
const ll mod=1e9+;
int head[N];
int tot;
struct node{
int to,next;
int w;
}edge[N<<];
int vis[N];
double dis[N];
int val[N];
int num[N];
void init(){
memset(head,-,sizeof(head));
tot=;
}
void add(int u,int v,int w){
edge[tot].to=v;
edge[tot].next=head[u];
edge[tot].w=w;
head[u]=tot++;
}
int n,m;
int spfa(double x){
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
for(int i = ;i<=n;i++)dis[i]=1e15;
queue<int>q;
q.push();
dis[]=;
num[]++;
vis[]=;
while(q.empty()==){
int u=q.front();
q.pop();
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
int w=edge[i].w;
if(dis[u]+w*x-val[u]<dis[v]){
dis[v]=dis[u]+x*w-val[u];
if(vis[v]==){
q.push(v);
vis[v]=;
num[v]++;
if(num[v]>n)return ;//存在负权环
}
}
}
}
return ;
}
int main(){
init();
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",&val[i]);
for(int i=;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
//add(v,u,w);
}
double low=0.0;
double high=;
double ans=;
while(low+eps<high){
double mid=(low+high)/2.0;
if(spfa(mid)){
ans=mid;
low=mid;
}else{
high=mid;
}
}
printf("%.2f\n",ans);
}
/*
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
*/

 

poj 3621(最优比率环)的更多相关文章

  1. poj 3621(最优比率环)

    题目链接:http://poj.org/problem?id=3621 思路:之前做过最小比率生成树,也是属于0/1整数划分问题,这次碰到这道最优比率环,很是熟悉,可惜精度没控制好,要不就是wa,要不 ...

  2. POJ 3621-Sightseeing Cows-最优比率环|SPFA+二分

    最优比率环问题.二分答案,对于每一个mid,把节点的happy值归类到边上. 对于每条边,用mid×weight减去happy值,如果不存在负环,说明还可以更大. /*---------------- ...

  3. POJ 3621 最优比率生成环

    题意:      让你求出一个最优比率生成环. 思路:      又是一个01分化基础题目,直接在jude的时候找出一个sigma(d[i] * x[i])大于等于0的环就行了,我是用SPFA跑最长路 ...

  4. Sightseeing Cows(最优比率环)

    Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8915   Accepted: 3000 ...

  5. [转]01分数规划算法 ACM 二分 Dinkelbach 最优比率生成树 最优比率环

    01分数规划 前置技能 二分思想最短路算法一些数学脑细胞? 问题模型1 基本01分数规划问题 给定nn个二元组(valuei,costi)(valuei,costi),valueivaluei是选择此 ...

  6. 【poj3621】最优比率环

    题意: 给定n个点,每个点有一个开心度F[i],每个点有m条单向边,每条边有一个长度d,要求一个环,使得它的 开心度的和/长度和 这个比值最大.n<=1000,m<=5000 题解: 最优 ...

  7. poj 3621最优比例生成环(01分数规划问题)

    /* 和求最小生成树差不多 转载思路:http://www.cnblogs.com/wally/p/3228171.html 思路:之前做过最小比率生成树,也是属于0/1整数划分问题,这次碰到这道最优 ...

  8. POJ 3621:Sightseeing Cows(最优比率环)

    http://poj.org/problem?id=3621 题意:有n个点m条有向边,每个点有一个点权val[i],边有边权w(i, j).找一个环使得Σ(val) / Σ(w)最大,并输出. 思路 ...

  9. POJ 3621 Sightseeing Cows (最优比率环 01分数划分)

    题意: 给定L个点, P条边的有向图, 每个点有一个价值, 但只在第一经过获得, 每条边有一个花费, 每次经过都要付出这个花费, 在图中找出一个环, 使得价值之和/花费之和 最大 分析: 这道题其实并 ...

随机推荐

  1. Zabbix微信告警

    Zabbix微信告警 摘要 Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越多的企业开始使用zabbix结合微信作为主要的告警方式,这样可以及时有效的把告警信 ...

  2. drupal8 用户指南

    一句话概括 - 官方文档 概念- Drupal是个内容管理系统哦 那么,什么是内容管理系统? 就是用户自己编辑自己的网站内容的一个系统. 那么,什么是Drupal呢? Drupal是一个通过模块和主题 ...

  3. ruby on rails 常见配置错误解决

    error:in `require': cannot load such file -- sqlite3/sqlite3_native (LoadError) 先删除 Ruby下的D:\Ruby22- ...

  4. Adversarial Auto-Encoders

    目录 Another Approach: q(z)->p(z) Intuitively comprehend KL(p|q) Minimize KL Divergence How to comp ...

  5. 3.2.11 行 vs 字符串

        了解行(line)与字符串(string)的差异是相当重要的.大部分简易程序都是处理输入数据的行,像 grep 与 egrep,以及 sed 大部分的工作(99%)都是这样.在这些情况下,不会 ...

  6. Vue如何引入icon图标

             1.下载icon图标,推荐icomoon网站,里面有大量的矢量图标,也可以自定义,当然你也可以去阿里巴巴矢量图标库下载你所需要的小图标.点击进入icomoon网站点击右上角“IcoM ...

  7. 关于c# .net爬虫

    刚开始听到爬虫这两个字眼的时候感觉挺稀奇的,之前并没有接触过爬虫,正好这会手上没事,于是便百度了一下. 1.网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种 ...

  8. 「CodePlus 2017 11 月赛」大吉大利,晚上吃鸡!

    n<=50000,m<=50000的图,给s和t,问有多少点对$(a,b)$满足 嗯. 不会. 首先最短路DAG造出来,然后两个条件转述一下:条件一,$N_a$表示从s到t经过a的路径,$ ...

  9. 主席树初探--BZOJ3524: [Poi2014]Couriers

    n<=500000个数,m<=500000个询问,每次问区间里出现次数>(R-L+1)的数字是谁,没有输出0. 写了带修改发现不会不带修改了.... 不带修改的话,n个点,每个点表示 ...

  10. linux 常见名词及命令(六)

    查看软件安装位置 : dpkg -L 软件包名字 ubuntu环境的apache相关命令: 查看是否启动:ps -aux | grep httpd  或者 netstat -an | grep :80 ...