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

【题目大意】

给出一个有向图,问求一个回路,使得回路上的点权之和/边权之和 最大。

【解题思路】

此题是对01分数规划的应用,那么首先明白01分数规划的思想,用基础分数规划,就是第三类,最优比率环裸题

spfa判正环。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#define N 1007
#define M 5007
using namespace std; int n,m;
int num[N];double w[N],dis[N];bool vis[N];
int cnt,head[N],Next[M],rea[M];double val[M]; void Add(int u,int v,double fee)
{Next[++cnt]=head[u],head[u]=cnt,rea[cnt]=v,val[cnt]=fee;}
bool spfa(double rate)
{
for (int i=;i<=n;i++)
dis[i]=,vis[i]=,num[i]=;
queue<int>q;
for (int i=;i<=n;i++)
q.push(i);
while(!q.empty())
{
int u=q.front();q.pop();
for (int i=head[u];i!=-;i=Next[i])
{
int v=rea[i];double fee=w[u]-rate*val[i];
if (dis[u]+fee>dis[v])
{
dis[v]=dis[u]+fee;
if (!vis[v])
{
q.push(v);
num[v]++;vis[v]=;
if (num[v]>n) return true;
}
}
}
vis[u]=;
}
return false;
}
int main()
{
memset(head,-,sizeof(head));
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++)
scanf("%lf",&w[i]);
int x,y;double z;
for (int i=;i<=m;i++)
{
scanf("%d%d%lf",&x,&y,&z);
Add(x,y,z);
}
double l=0.0,r=1000.0;
while (r-l>0.0001)
{
double mid=(l+r)/;
if (spfa(mid)) l=mid;
else r=mid;
}
printf("%.2f\n",l);
}

POJ过不了

最新文章

  1. ubuntu16.04 orbslam ./build.sh 出错eigen
  2. 菜单导航/URHere/面包屑,通过CSS中的content简洁表达代码
  3. 无法连接远程SQL Server 数据库的原因
  4. POJ 3286 How many 0&#39;s?(几多0?)
  5. 【Linux高频命令专题(20)】du
  6. Spark Streaming揭秘 Day5 初步贯通源码
  7. 求小于等于n的所有素数
  8. bnuoj 4187 GCC (数论)
  9. TabControl控件的美化
  10. JPush 极光推送 消息推送 实例
  11. Introduction to REST #Reprinted#
  12. 10 Super Useful Tools for Web Designers
  13. 分享html5的一个拖拽手法
  14. diff和patch命令(1)
  15. iOS开发-即时通信XMPP
  16. Vue和后台交互的方式
  17. BAT面试上机题从3亿个ip中找出访问次数最多的IP详解
  18. LNMP 1.x升级到LNMP 1.4教程及注意事项和多PHP版本使用教程
  19. (转)MySQL排序原理与案例分析
  20. 关于springMVC 传递 对象参数的问题

热门文章

  1. Caused by: java.lang.ClassNotFoundException: org.springframework.boot.system.JavaVersion
  2. spring框架的总结
  3. html输入框去除记忆功能
  4. JavaScript -- 语法和数据类型
  5. java后台验证码的生成
  6. shell脚本,编程题练习。
  7. Quartz监听的端口
  8. 总结:JavaScript异步、事件循环与消息队列、微任务与宏任务
  9. Mycat主从分离
  10. 【php】Windows PHP及xdebug安装 安装