Sightseeing Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10306   Accepted: 3519

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

题目链接:POJ 3621

最优比例环的题,也是用01分数规划写,目的是找到一个环,使得该环上${\Sigma w_v \over \Sigma w_e}$最大,那么我们设比例为r,当${\Sigma w_v \over \Sigma w_e}>r$时说明可以找到更大的r'作为答案,由这个式子有可以得到:$\Sigma w_v - r * \Sigma w_e>0$,即存在左边的式子结果>0即可,若要找存在一个数大于0,那么肯定找这个数可能的最大值,那显然把边权重新赋值为$w_{vi}-r*w_{e}$,然后这式子跟SPFA找正环有什么关系?可以发现$\Sigma w_v - r * \Sigma w_e$这个式子代表了这个环上用$w_{vi}-r*w_{ei}$作为新边权的所有边权之和,如果这个和大于0,那显然这个环上存在正环,即有不存在最长路,那每一次SPFA找最长路看看是否存在即可,当然也把式子加个负号,然后用负环检测也可以做

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1010;
const int M = 5010;
const double eps = 1e-6;
struct edge
{
int to, nxt;
double w;
edge() {}
edge(int _to, int _nxt, double _w): to(_to), nxt(_nxt), w(_w) {}
};
edge E[M];
int head[N], tot;
int vis[N], cnt[N];
double d[N];
int arr[N]; void init()
{
CLR(head, -1);
tot = 0;
}
inline void add(int s, int t, double w)
{
E[tot] = edge(t, head[s], w);
head[s] = tot++;
}
int spfa(int n, double g)
{
queue<int>Q;
for (int i = 1; i <= n; ++i)
{
d[i] = 0;
vis[i] = 1;
cnt[i] = 1;
Q.push(i);
}
while (!Q.empty())//找正环
{
int u = Q.front();
Q.pop();
vis[u] = 0;
for (int i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
double w = arr[u] - g * E[i].w;
if (d[v] < d[u] + w)
{
d[v] = d[u] + w;
if (!vis[v])
{
vis[v] = 1;
Q.push(v);
if (++cnt[v] > n)
return 1;
}
}
}
}
return 0;
}
int main(void)
{
int n, m, a, b, i;
double w;
while (~scanf("%d%d", &n, &m))
{
init();
for (i = 1; i <= n; ++i)
scanf("%d", &arr[i]);
for (i = 1; i <= m; ++i)
{
scanf("%d%d%lf", &a, &b, &w);
add(a, b, w);
}
double L = 0, R = 1000;
double ans = 0;
while (fabs(R - L) >= eps)
{
double mid = (L + R) / 2.0;
if (spfa(n, mid))
{
L = mid;
ans = mid;
}
else
R = mid;
}
printf("%.2f\n", ans);
}
return 0;
}

POJ 3621 Sightseeing Cows(最优比例环+SPFA检测)的更多相关文章

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

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

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

    题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total ...

  3. POJ3621 Sightseeing Cows 最优比率环 二分法

    题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total ...

  4. POJ 3621 Sightseeing Cows 01分数规划,最优比例环的问题

    http://www.cnblogs.com/wally/p/3228171.html 题解请戳上面 然后对于01规划的总结 1:对于一个表,求最优比例 这种就是每个点位有benefit和cost,这 ...

  5. [POJ 3621] Sightseeing Cows

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

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

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

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

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

  8. POJ 3621 Sightseeing Cows | 01分数规划

    题目: http://poj.org/problem?id=3621 题解: 二分答案,检查有没有负环 #include<cstdio> #include<algorithm> ...

  9. POJ 3621 Sightseeing Cows (bellman-Ford + 01分数规划)

    题意:给出 n 个点 m 条有向边,要求选出一个环,使得这上面 点权和/边权和 最大. 析:同样转成是01分数规划的形式,F / L 要这个值最大,也就是 G(r) = F - L * r 这个值为0 ...

随机推荐

  1. 【Java-JVM】定量分析解决OutOfMemoryError: PermGen space, 来看科学量化分析

    网络上搜集,有操作有分析. 一.问题 在部署大型的 Java Web项目的时候,或者在 MyEclipse 中进行调试的时候经常出现: OutOfMemoryError: PermGen space ...

  2. Java代码工具箱之链接Oracle

    1. 需要oracle的 odbc  jar包 2. 代码 3. 注意:ps对象和statement对象最好用完立即释放,尤其是读写数据库代码出现在 for 循环语句中时. 否则会出现游标不够的情况, ...

  3. IE脚本调试

    打开IE -- 工具 -- Internet选项 -- 高级 --有4项. 1.禁用脚本调试(Internet Explorer)(去掉对勾) 2.禁用脚本调试(其他)(去掉对勾) 3.显示每个脚本错 ...

  4. java基础面试题:运行时异常与一般异常有何异同?error和exception有什么区别? 请写出你最常见到的5个runtimeexception?

    Throwable是Java错误处理的父类,有两个子类:Error和Exception. Error:无法预期的严重错误,导致JVM虚拟机无法继续执行,几乎无法恢复捕捉的 Exception:可恢复捕 ...

  5. C盘扩展卷是灰色的扩容方法

    当想要扩容C盘的时候可能会发现C盘的扩展卷竟然是灰色的.原因是C盘旁边没有紧挨着的“”未分配空间“”, 只要将D盘的空间分出一些来就可以了. !!!磁盘的分区合并有风险,重要文件等记得先备份  !!! ...

  6. c语言中--typeof--关键字用法

    C语言中 typeof 关键字是用来定义变量数据类型的.在linux内核源代码中广泛使用. 下面是Linux内核源代码中一个关于typeof实例: #define min(x, y) ({ \ typ ...

  7. React学习记录二

    环境基本弄清楚了以后,开始总会写个hello world什么的,开发做了这么久了,就跳过这一步吧. 还是从打开vscode说起吧,这里文件菜单打开一个文件夹Demos,查看菜单打开集成终端,也可以使用 ...

  8. rpc - 接口返回数据结构的设计

    方案一: 系统级状态  .业务级别的状态同用 code要特殊声明保留状态,如若不声明保留状态,一旦业务开发人员用到了系统级的状态,就有必要侵入的改动业务返回的code(新code与业务欲返回的code ...

  9. wampserver怎么设置外网可访问

    wampserver配置httpd.conf允许外网访问? 在电脑上开启wamp服务后,默认是禁止外部网络访问的,如果您想要同一局域网中的设备能够访问PC上的web项目,则需要对httpd.conf文 ...

  10. 常用模块之 os,json,shelve,xml模块

    os 即操作系统 在 os 中提供了很多关于文件,文件夹,路径处理的函数 这是我们学习的重点 os.path 是os模块下专门用于处理路径相关的 python是一门跨平台语言,由于每个平台路径规则不同 ...