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. MillWheel: Fault-Tolerant Stream Processing at Internet Scale

    http://static.googleusercontent.com/media/research.google.com/zh-CN//pubs/archive/41378.pdf   为什么要做M ...

  2. Oracle存储过程java 调用

    1.nest表组合成结果集,然后以游标变量的形式返回 --创建类型 create or replace package mytest is -- Author  : ADMINISTRATOR  -- ...

  3. 【ZBar】ios错误ignoring file xxx missing required architecture x86_64 in file

    解决方法: 1.在Project target里"Architectures"设置为:Standard (armv7,armv7s)或者  Standard (armv7,arm6 ...

  4. 【总结】使用jdbc+servlet开发一个bug管理系统的经验总结

    开发背景: 公司目前使用Teambition里面的task作为bug管理系统,既没有bug的当前状态,也不能写上bug的详细复现步骤,被assign了任务(该修复bug或者验证bug是否被修复)也没有 ...

  5. 如何获取并分析L2CAP包

    本文中的分析与软件相关的内容,都是以WinCE中的 Microsoft Bluetooth Core Stack为例进行分析:与协议有关的内容,是基于Bluetooth Core 2.1 + EDR ...

  6. centos常用命令

    应用程序->附件->终端 一:使用CentOS常用命令查看cpumore /proc/cpuinfo | grep "model name"   grep " ...

  7. 设计模式:桥连模式(Bridge)

    定   义:将抽象部分和它的实现部分分离,使它们可以独立的变化. 结构图: 实现类: //Implementor(实现)类 public abstract class Implementor { pu ...

  8. 毕老师的Editplus

    简介 EditPlus是一款由韩国 Sangil Kim (ES-Computing)出品的小巧但是功能强大的可处理文本.HTML和程序语言的Windows编辑器,你甚至可以通过设置用户工具将其作为C ...

  9. 在css中定义滚动条样式

    1,Overflow内容溢出时的设置 overflow 水平及垂直方向内容溢出时的设置 overflow-x 水平方向内容溢出时的设置 overflow-y 垂直方向内容溢出时的设置 以上三个属性设置 ...

  10. TCP/IP和HTTP的举例理解

    闲暇中逛博客园,看到TCP/IP和HTTP关键词,就突然想深刻理解他们(以前真的是只知皮毛),于是看了关于TCP/IP和HTTP的博文,就有了此文. 首先要引出开放系统互连参考模型(OSI:Open ...