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, 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

题意:给出 n 个点 m 条边的有向图,每个点和每条边都有自己的权值,求一个环,使得环中各点点值与各边边值的和的比率最大,求这个比率

思路:01 分数规划中的最优比率环问题,利用 SPFA 在判断负环的过程中对边进行重赋值,从而利用二分法来二分答案

具体思路:点击这里

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickMultPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL quickPowMod(LL a,LL b,LL mod){ LL res=1; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1; } return res; }
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-6;
const int MOD = 1000000000+7;
const int N = 1000+5;
const int dx[] = {0,0,-1,1,1,-1,1,1};
const int dy[] = {1,-1,0,0,-1,1,-1,1};
using namespace std;struct Edge {int to, next;double cost; //边权
} edge[N * 10];
int head[N], tot;
int n, m;
double value[N]; //点权
double dis[N];
bool vis[N];
int cnt[N]; //进队次数
void addEdge(int x, int y, double w) {edge[tot].to = y;edge[tot].next = head[x];edge[tot].cost = w;head[x] = tot++;
}
int SPFA(double x) {memset(dis, 0x43, sizeof(dis));memset(cnt, 0, sizeof(cnt));memset(vis, false, sizeof(vis));dis[1] = 0;cnt[1]++;queue<int> Q;Q.push(1);while (!Q.empty()) {int u = Q.front();Q.pop();vis[u] = 0;for (int i = head[u]; i != -1; i = edge[i].next) {int v = edge[i].to;double val = edge[i].cost * x - value[v]; //重赋值if (dis[v] > dis[u] + val) {dis[v] = dis[u] + val;if (!vis[v]) {vis[v] = true;cnt[v]++;if (cnt[v] >= n)return true;Q.push(v);}}}}return false;
}
int main() {tot = 0;memset(head, -1, sizeof(head));scanf("%d%d", &n, &m);for (int i = 1; i <= n; i++) //点权scanf("%lf", &value[i]);for (int i = 1; i <= m; i++) {int x, y;double w;scanf("%d%d%lf", &x, &y, &w);addEdge(x, y, w);}double left = 0, right = 10000;while (right - left >= EPS) {double mid = (left + right) / 2.0;if (SPFA(mid)) //存在负环,更改下界left = mid;elseright = mid;}printf("%.2f\n", left);return 0;
}

Sightseeing Cows(POJ-3621)相关推荐

  1. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  2. Bailian2734 十进制到八进制【入门】(POJ NOI0113-45)

    问题链接:POJ NOI0113-45十进制到八进制 2734:十进制到八进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个十进制正整数转化成八进制. 输入 一行,仅含一个十进 ...

  3. Bailian2676 整数的个数【入门】(POJ NOI0105-11)

    问题链接:POJ NOI0105-11 整数的个数 2676:整数的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定k(1 < k < 100)个正整数,其中每个数 ...

  4. Bailian4029 数字反转【进制】(POJ NOI0105-29)

    问题链接:POJ NOI0105-29 数字反转 4029:数字反转 总时间限制: 1000ms 内存限制: 65535kB 描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数 ...

  5. Bailian2735 八进制到十进制【入门】(POJ NOI0113-46)

    问题链接:POJ NOI0113-46 八进制到十进制 2735:八进制到十进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个八进制正整数转化成十进制. 输入 一行,仅含一个八 ...

  6. 吴昊品游戏核心算法 Round 7 —— 熄灯游戏AI(有人性的Brute Force)(POJ 2811)

    暴力分为两种,一种属于毫无人性的暴力,一种属于有人性 的暴力.前面一种就不说了,对于后面一种情况,我们可以只对其中的部分问题进行枚举,而通过这些子问题而推导到整个的问题中.我称之为有人性的Brute ...

  7. 【二分】Best Cow Fences(poj 2018)

    Best Cow Fences poj 2018 题目大意: 给出一个正整数数列,要你求平均数最大,长度不小于M的字串,结果乘1000取整 输入样例 10 6 6 4 2 10 3 8 5 9 4 1 ...

  8. 昂贵的聘礼(poj 1062)

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...

  9. 主席树学习小结(POJ 2104)

    在高中的时候就听到过主席树了,感觉非常高端,在寒假的时候 winter homework中有一题是查找区间第K大的树,当时就开始百度这种网上的博客,发现主席树看不懂,因为那个root[i],还有tx[ ...

  10. A - TOYS(POJ - 2318) 计算几何的一道基础题

    Calculate the number of toys that land in each bin of a partitioned toy box. 计算每一个玩具箱里面玩具的数量 Mom and ...

最新文章

  1. 【工具软件】webstorm如何使用快捷键生成固定代码
  2. win32 创建进程三种方式简单示例 - 使用CFree
  3. matlab 设置legend(比较全面)
  4. JavaScript如何获取/计算页面元素的offset?
  5. P3978 [TJOI2015]概率论
  6. mysql(待完善)
  7. 【剑指offer】面试题10- I:斐波那契数列(Java)
  8. python if and函数_逻辑函数And,OR,IF
  9. 电视节目《宅男改变世界》
  10. 美物理学家称摩尔定律将在十年内崩溃
  11. 【渝粤教育】电大中专公共基础课程 (2)作业 题库
  12. 云呐|固定资产盘点管理办法
  13. 软件测试的艺术(二)
  14. Bitmap、BitSet、RoaringBitmap持久化存储
  15. 华为设备,什么是MAC地址漂移
  16. 用stream流将list集合根据某个字段分组成Map<String,List<T>>类型的集合
  17. arduino学习笔记二十二--模拟交通信号灯
  18. * word 2000 与 word 2003 版本兼容性问题
  19. Decision Model and Notation (DMN)
  20. Win7 更新 80072EFE 错误

热门文章

  1. 4918字,详解商品系统的存储架构设计
  2. 那个陪你聊微信、发自拍的妹子,可能不是人
  3. linux内核源码目录分析
  4. python的map函数求取每个元素的平方根_python的map函数的使用方法详解以及使用案例(处理每个元素的自增、自减、平方等)......
  5. 一文彻底掌握二叉查找树
  6. 58到家运维专家杨经营:业务上云后运维平台的演进之路
  7. 3小时解决头疼的年终报表!
  8. Spring Cloud 入门 之 Eureka 篇(一)
  9. JeeWx捷微2.4.1版本发布,JAVA微信管家平台(支持公众号、企业号)
  10. jeewx 微信管家 - 举办商业版本免费试用活动