Sightseeing Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8218   Accepted: 2756

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条道路,道路是有向边,然后给出经过n个城市的每个城市可以获得的欢乐度,给出每条道路需要花费的旅行时间,要求某个人从任一点出发,然后走一圈(至少经过两个点)回到最初的起点,问平均单位时间的欢乐值最高是多少,若不能走一圈,则输出0.
分析:设单位时间欢乐值r=sigma(Vi)/sigma(Ej);其中Vi属于该环的城市的欢乐值,Ej是该环的道路的所花费的时间,设最大平均值是R,即r<=R
即:sigma(Vi)/sigma(Ej)<=R,即:sigma(Vi)-sigma(Ej)*R<=0;
也就是说对于函数H(r)=sigma(Vi)-sigma(Ej)*r,当H(r)==0的时候可以取得最优值
所以接下来就是二分枚举r值,以P[u]-r*edge[i].w作为边权,用dfs判断正环,如果存在正环,则二分增加r,否则即不存在环,应该缩小r,二分逐渐逼近找到一个环且该环的权值和==0,此时枚举的r就是R
程序:
#include"stdio.h"
#include"string.h"
#include"math.h"
#define M 2009
#define eps 1e-10
struct node
{
int u,v,w,next;
}edge[M*20];
int t,head[M],use[M],p[M];
double dis[M];
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
}
int dfs(int u,double r)
{
use[u]=1;
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].v;
if(dis[v]<dis[u]+p[u]-r*edge[i].w)
{
dis[v]=dis[u]+p[u]-r*edge[i].w;
if(use[v])
return 1;
if(dfs(v,r))
return 1;
}
}
use[u]=0;
return 0;
}
int solve(int n,double r)
{
memset(use,0,sizeof(use));
memset(dis,0,sizeof(dis));
for(int i=1;i<=n;i++)
{
if(dfs(i,r))
return 1;
}
return 0;
}
int main()
{
int n,m,a,b,c;
while(scanf("%d%d",&n,&m)!=-1)
{
init();
for(int i=1;i<=n;i++)
scanf("%d",&p[i]);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
double l=0,r=10000000;
double mid;
double ans=0;
while(r-l>eps)
{
mid=(l+r)/2;
int msg=solve(n,mid);
if(msg)
{
ans=mid;
l=mid;
}
else
r=mid;
}
printf("%.2lf\n",ans);
}
return 0;
}

  

01分数规划POJ3621(最优比例生成环)的更多相关文章

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

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

  2. poj 3621 0/1分数规划求最优比率生成环

    思路:以val[u]-ans*edge[i].len最为边权,判断是否有正环存在,若有,那么就是ans小了.否则就是大了. 在spfa判环时,先将所有点进队列. #include<iostrea ...

  3. POJ 2728 Desert King 01分数规划,最优比率生成树

    一个完全图,每两个点之间的cost是海拔差距的绝对值,长度是平面欧式距离, 让你找到一棵生成树,使得树边的的cost的和/距离的和,比例最小 然后就是最优比例生成树,也就是01规划裸题 看这一发:ht ...

  4. 最优比例生成环(dfs判正环或spfa判负环)

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

  5. poj 3621最优比例生成环(01分数规划问题)

    /* 和求最小生成树差不多 转载思路:http://www.cnblogs.com/wally/p/3228171.html 思路:之前做过最小比率生成树,也是属于0/1整数划分问题,这次碰到这道最优 ...

  6. BZOJ 1486 最小圈(01分数规划)

    好像是很normal的01分数规划题.最小比率生成环. u(c)=sigma(E)/k.转化一下就是k*u(c)=sigma(E). sigma(E-u(c))=0. 所以答案对于这个式子是有单调性的 ...

  7. 【转】[Algorithm]01分数规划

    因为搜索关于CFRound277.5E题的题解时发现了这篇文章,很多地方都有值得借鉴的东西,因此转了过来 原文:http://www.cnblogs.com/perseawe/archive/2012 ...

  8. POJ 2976 Dropping tests 01分数规划 模板

    Dropping tests   Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6373   Accepted: 2198 ...

  9. 【poj 2976】Dropping tests(算法效率--01分数规划 模版题+二分){附【转】01分数规划问题}

    P.S.又是一个抽时间学了2个小时的新东西......讲解在上半部分,题解在下半部分. 先说一下转的原文:http://www.cnblogs.com/perseawe/archive/2012/05 ...

随机推荐

  1. IE 的resize事件问题

    window的resize事件,真的让人无语! 我在动态设置元素的HTML内容后,窗口高度变化了,可是却不触发resize事件. 但是我在访问document.documentElement.scro ...

  2. ASP.Net网站程序在编译发布部署后的后期修改

    ASP.Net网站程序在发布部署后的后期修改 作者:东篱南山 这里说的后期修改是指网站编译发布并部署好之后,对程序进行的修改,即在不能更改现有代码的情况下,更改页面的显示或是更改业务逻辑.一般是在程序 ...

  3. nrf51822裸机教程-IIC

    关于IIC总线的核心有以下几点: :时钟线高电平期间必须保持数据线不变. :时钟线低电平期间可以改变数据. :时钟线和数据线上都要接上拉电阻,以使总线不工作时,两根线的电平都处于高电平状态. :每个传 ...

  4. php获得文件夹下所有文件的递归算法

    function my_scandir($dir){ $files=array(); if(is_dir($dir)) { if($handle=opendir($dir)) { while(($fi ...

  5. 关于网站的UV分析

    一:准备 1.统计的维度 guid tracktime provice 2.key与value的设定 key:date+provice_guid value:NullWritable 3.案例分析 表 ...

  6. C++控制程序只运行一个实例

    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { ...

  7. 企业服务总线(ESB)

    思考: 1.ESB的定义到底是什么?是一款产品还是一种架构模式? 2.ESB有何实际用处? 定义ESB 对于企业服务总线(Enterprise Service Bus),目前还没有公认的定义,根据供应 ...

  8. Mongoose中关联查询populate的使用

    MongoDB中没有join的特性,因此无法使用join进行表的连接和关联查询,在Mongoose中封装了populate方法,在定义一个 Schema 的时候可以指定了其中的字段(属性)是另一个Sc ...

  9. How To Install Tinc and Set Up a Basic VPN on Ubuntu 14.04

    Introduction In this tutorial, we will go over how to use Tinc, an open source Virtual Private Netwo ...

  10. HBASE架构解析(一)

    http://www.blogjava.net/DLevin/archive/2015/08/22/426877.html 前记 公司内部使用的是MapR版本的Hadoop生态系统,因而从MapR的官 ...