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

Sightseeing Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10552   Accepted: 3613

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

 
 
 
题解:
 
 

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 5e3+; int n, m, val[MAXN];
struct edge
{
int to, w, next;
}edge[MAXN];
int cnt, head[MAXN]; void add(int u, int v, int w)
{
edge[cnt].to = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
} double dis[MAXN];
int times[MAXN], inq[MAXN], vis[MAXN];
bool spfa(double L)
{
memset(vis, , sizeof(vis));
memset(inq, , sizeof(inq));
memset(times, , sizeof(times));
for(int i = ; i<=n; i++)
dis[i] = INF; queue<int>Q;
Q.push();
inq[] = ;
vis[] = ;
dis[] = ;
while(!Q.empty())
{
int u = Q.front();
Q.pop(); inq[u] = ;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
// 距离全部取反, 看是否存在正环
if(dis[v]> dis[u]-(val[u]-edge[i].w*L) )
{
dis[v] = dis[u]-(val[u]-edge[i].w*L);
if(!inq[v])
{
Q.push(v);
inq[v] = ;
vis[v] = ;
if(++times[v]>n) return true; //检测到了负环,但因为数值全部取反了,所以实际上为检测到了正环
}
}
}
}
return false;
} void init()
{
cnt = ;
memset(head, -, sizeof(head));
} int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
for(int i = ; i<=n; i++)
scanf("%d", &val[i]); init();
for(int i = ; i<=m; i++)
{
int u, v, w;
scanf("%d%d%d",&u, &v, &w);
add(u,v,w);
} double l = , r = 1e3;
while(l+EPS<=r)
{
double mid = (l+r)/;
if(spfa(mid)) //存在正环,继续增大比率
l = mid + EPS;
else
r = mid - EPS;
}
printf("%.2f\n", r);
}
}

POJ3621 Sightseeing Cows 最优比率环 二分法的更多相关文章

  1. POJ3621 Sightseeing Cows(最优比率环)

    题目链接:id=3621">http://poj.org/problem?id=3621 在一个有向图中选一个环,使得环上的点权和除以边权和最大.求这个比值. 经典的分数规划问题,我认 ...

  2. POJ 3621 Sightseeing Cows [最优比率环]

    感觉去年9月的自己好$naive$ http://www.cnblogs.com/candy99/p/5868948.html 现在不也是嘛 裸题,具体看学习笔记 二分答案之后判负环就行了 $dfs$ ...

  3. Sightseeing Cows(最优比率环)

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

  4. 【poj3621】最优比率环

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

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

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

  6. poj 3621(最优比率环)

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

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

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

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

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

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

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

随机推荐

  1. linux文件夹作用

    linux下的文件结构,看看每个文件夹都是干吗用的/bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基 ...

  2. LA 2218 半平面交

     题目大意:n名选手参加铁人三项赛,比赛按照选手在三个赛段中所用的总时间排定名次.已知每名选手在三个项目中的速度Ui.Vi.Wi.问对于选手i,能否通过适当的安排三个赛段的长度(但每个赛段的长度都不能 ...

  3. 使用Eclipse+axis2一步一步发布webservice

    1.下载axis2相关软件http://axis.apache.org/axis2/java/core/download.html 2.Java环境配置:JAVA_HOME.JRE_HONE.PATH ...

  4. PAT (Advanced Level) 1087. All Roads Lead to Rome (30)

    暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  5. python文件追加及时间获取

    一.python:文件的读取.创建.追加.删除.清空 2011-10-24 11:36:35|  分类: python |举报 |字号 订阅   一.用Python创建一个新文件,内容是从0到9的整数 ...

  6. 10.Java web—JavaBean

    定义一个类,然后在jsp页面通过<jsp:useBean>标签调用 重点是类属性名要起得规则,一般是setXXX  getXXXX 新建一个类UserInfo public class U ...

  7. java并发编程阻塞队列

    在前面我们接触的队列都是非阻塞队列,比如PriorityQueue.LinkedList(LinkedList是双向链表,它实现了Dequeue接口). 使用非阻塞队列的时候有一个很大问题就是:它不会 ...

  8. 安装ftp服务器

    Linux安装ftp组件 1  安装vsftpd组件 安装完后,有/etc/vsftpd/vsftpd.conf文件,是vsftp的配置文件. [root@bogon ~]# yum -y insta ...

  9. 封装算法: 模板方法(Template Method)模式

    template method(模板方法)模式是一种行为型设计模式.它在一个方法中定义了算法的骨架(这种方法被称为template method.模板方法),并将算法的详细步骤放到子类中去实现.tem ...

  10. C#语言 数组