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. 从汇编看c++中临时对象的析构时机

    http://www.cnblogs.com/chaoguo1234/archive/2013/05/12/3074425.html c++中,临时对象一旦不需要,就会调用析构函数,释放其占有的资源: ...

  2. HDU 6166 Senior Pan(多校第九场 二进制分组最短路)

    题意:给出n个点和m条有向边(有向边!!!!我还以为是无向查了半天),然后给出K个点,问这k个点中最近的两点的距离 思路:比赛时以为有询问,就直接丢了,然后这题感觉思路很棒,加入把所有点分成起点和终点 ...

  3. 小白安装python软件

    首先下载:anaconda3.x          下载方式:百度搜索 清华镜像anaconda   https://mirrors.tuna.tsinghua.edu.cn/help/anacond ...

  4. vue引用文件

    1)css引入在vue页面中<style scoped> @import url("../assets/css/home.css");</style>2)j ...

  5. Shell脚本调用Oralce数据库SQL文生产日志

    #!/bin/shexport LANG="zh.CN.GBK" echo -n "******************************************* ...

  6. ios多线程原理及runloop介绍

    一.线程概述 有些程序是一条直线,起点到终点:有些程序是一个圆,不断循环,直到将它切断.直线的如简单的Hello World,运行打印完,它的生命周期便结束了,像昙花一现那样:圆如操作系统,一直运行直 ...

  7. 【贪心】「poj1328」Radar Installation

    建模:二维转一维:贪心 Description Assume the coasting is an infinite straight line. Land is in one side of coa ...

  8. Beautiful Soup 4.2.0 doc_tag、Name、Attributes、多值属性

    找到了bs4的中文文档,对昨天爬虫程序里所涉及的bs4库进行学习.这篇代码涉及到tag.Name.Attributes以及多值属性. ''' 对象的种类 Beautiful Soup将复杂HTML文档 ...

  9. Linux服务器硬件设备信息查看

    一.cpu信息 cpu信息存储在/proc文件系统的cpuinfo(/proc/cpuinfo)文件里,可以直接查看这个文件以获得cpu信息,所列字段解释如下: processor : 核心编号,如: ...

  10. Google 超分辨率技术 RAISR

    每天都有数以百万计的图片在网络上被分享.储存,用户借此探索世界,研究感兴趣的话题,或者与朋友家人分享假期照片.问题是,大量的图片要嘛被照相设备的像素所限制,要嘛在手机.平板或网络限制下被人为压缩,降低 ...