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过不了

OpenJ_Bailian3375的更多相关文章

随机推荐

  1. bat 符号说明

    netstat -an|findstr 139 ipconfig/all findstr IP ipconfig/all |findstr   物理地址             定值选行 ipconf ...

  2. UVA - 1252 Twenty Questions (状压dp)

    状压dp,用s表示已经询问过的特征,a表示W具有的特征. 当满足条件的物体只有一个的时候就不用再猜测了.对于满足条件的物体个数可以预处理出来 转移的时候应该枚举询问的k,因为实际上要猜的物品是不确定的 ...

  3. solr scheme配置简介

    solr 字段配置,和数据库数据索引配置 配置solr字段. schema.xml 文件里配置 先讲解一下,里面的一些字段 1. <types> ... </types> 表示 ...

  4. js 两个数组进行去重处理,返回去重后的数组

    1.去重的方法为: array_diff(a, b) { for (var i = 0; i < b.length; i++) { for (var j = 0; j < a.length ...

  5. CAD控件界面显示与隐藏(网页版)

    控件界面工具栏的显示或隐藏,js代码实现如下: 1 2 3 4 5 6 7 8 9 //隐藏/显示工具栏       mxOcx.ShowToolBar("常用工具",isShow ...

  6. xheditor的实例程序—类似word的编辑器

    编辑器工具栏:类似word的编辑器 1.1.下载,兼容性 xhEditor官方网站地址为:http://xheditor.com/,打开右上角的免费下载 | 参数向导链接,即可找到最新版本的下载地址. ...

  7. Vue构建项目

    构建Vue项目 按照官网教程安装 //先安装脚手架 cnpm i -g vue-cli //查看项目目标列表: webpack browserify pwa 等项目模板 vue list //使用we ...

  8. python中enumerate()函数的用法

    描述: enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中.其英文意为:枚举,列举. 函数说明: 语法 ...

  9. (39)zabbix snmp自定义OID nginx监控实例

    为什么要自定义OID? 前面的文章已经讲过zabbix如何使用snmp监控服务器,但是他有一个很明显的局限性:只能监控定义好的OID项目 假如我们想知道nginx进程是否在运行?在没有zabbix a ...

  10. 【css】【动画】【转发】旋转动画

    <!DOCTYPE HTML> <html> <head>     <meta charset="utf-8">     <s ...