http://poj.org/problem?id=3621

Sightseeing Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7649   Accepted: 2567

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个景点和一些单项道路,到达一个顶点会获得一定的快乐值,经过道路会消耗一定的时间,一个人可以任意选择一个顶点作为开始的地方,然后经过一系列的景点返回原地;每个景点可以经过多次,但是只有经过第一次景点的时候才可以获得欢乐值,并且要旅游至少两个顶点,以保证得到足够的锻炼;问单位时间的欢乐值最大是多少;

分析:该题思路和最优比例生成树有些类似,设第i个点的欢乐值f[i],边权值是w[u][v];

对于一个环比率:r=(f[1]*x1+f[2]*x2+f[3]*x3+……f[n]*xn)/(w[1][2]*x1+w[2][3]*x2+……w[n][1]*xn);

构造一个函数z(l)=(f[1]*x1+f[2]*x2+f[3]*x3+……f[n]*xn)-l*(w[1][2]*x1+w[2][3]*x2+……w[n][1]*xn);

简化为z(l)=sigma(f[i]*xi)-l*sigma(w[i][j]*xi);

变形得:r=sigma(f[i]*xi)/sigma(w[i][j]*xi)=l+z(l)/sigma(w[i][j]*xi);

当存在比l还大的比率的冲要条件是z(l)>0;即z(l)存在正环值就行,所以转化成了求正环的问题;

应该把点权和边权融合成关于边的量:K=f[u]-mid*w[u][v];然后用二分枚举比率mid,当有向连通图中存在正环,就把mid增大,否者减小;

程序:

#include"stdio.h"
#include"string.h"
#include"queue"
#include"stdlib.h"
#include"iostream"
#include"algorithm"
#include"string"
#include"iostream"
#include"map"
#include"math.h"
#define M 1005
#define eps 1e-8
#define inf 100000000
using namespace std;
struct node
{
int v;
double w;
node(int vv,double ww)
{
v=vv;
w=ww;
}
};
vector<node>edge[M];
double dis[M],f[M];
int use[M],n;
double mid;
int dfs(int u)
{
use[u]=1;
for(int i=0;i<(int)edge[u].size();i++)
{
int v=edge[u][i].v;
if(dis[v]<dis[u]+f[u]-mid*edge[u][i].w)
{
dis[v]=dis[u]+f[u]-mid*edge[u][i].w;
if(use[v])
return 1;
if(dfs(v))
return 1;
}
}
use[u]=0;
return 0;
}
int ok()
{
memset(dis,0,sizeof(dis));
memset(use,0,sizeof(use));
for(int i=1;i<=n;i++)
if(dfs(i))
return 1;
return 0;
}
int main()
{
int m,i;
while(scanf("%d%d",&n,&m)!=-1)
{
for(i=1;i<=n;i++)
scanf("%lf",&f[i]);
for(i=1;i<=n;i++)
edge[i].clear();
for(i=1;i<=m;i++)
{
int a,b;
double c;
scanf("%d%d%lf",&a,&b,&c);
edge[a].push_back(node(b,c));
}
double left,right;
left=0;
right=100000;
while(right-left>eps)
{
mid=(right+left)/2;
if(ok())
left=mid;
else
right=mid;
}
printf("%.2lf\n",left);
}
return 0;
}

最优比例生成环(dfs判正环或spfa判负环)的更多相关文章

  1. 01分数规划POJ3621(最优比例生成环)

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

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

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

  3. POJ 3621 最优比率生成环

    题意:      让你求出一个最优比率生成环. 思路:      又是一个01分化基础题目,直接在jude的时候找出一个sigma(d[i] * x[i])大于等于0的环就行了,我是用SPFA跑最长路 ...

  4. SPFA找负环(DFS) luogu3385

    SPFA找负环的基本思路就是如果一个点被访问两次说明成环,如果第二次访问时所用路径比第一次短说明可以通过一直跑这个圈将权值减为负无穷,存在负环 有bfs和dfs两种写法,看了一些博客,在bfs和dfs ...

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

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

  6. L - The Shortest Path Gym - 101498L (dfs式spfa判断负环)

    题目链接:https://cn.vjudge.net/contest/283066#problem/L 题目大意:T组测试样例,n个点,m条边,每一条边的信息是起点,终点,边权.问你是不是存在负环,如 ...

  7. 递归型SPFA判负环 + 最优比例环 || [Usaco2007 Dec]奶牛的旅行 || BZOJ 1690 || Luogu P2868

    题外话:最近差不多要退役,复赛打完就退役回去认真读文化课. 题面:P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解:最优比例环 题目实际是要求一个ans,使得对于图中 ...

  8. 【BZOJ1486】【HNOI2009】最小圈 分数规划 dfs判负环。

    链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

  9. POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】

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

随机推荐

  1. Batch Normalization原理及其TensorFlow实现——为了减少深度神经网络中的internal covariate shift,论文中提出了Batch Normalization算法,首先是对”每一层“的输入做一个Batch Normalization 变换

    批标准化(Bactch Normalization,BN)是为了克服神经网络加深导致难以训练而诞生的,随着神经网络深度加深,训练起来就会越来越困难,收敛速度回很慢,常常会导致梯度弥散问题(Vanish ...

  2. 第二百九十二节,RabbitMQ多设备消息队列-Python开发

    RabbitMQ多设备消息队列-Python开发 首先安装Python开发连接RabbitMQ的API,pika模块 pika模块为第三方模块  对于RabbitMQ来说,生产和消费不再针对内存里的一 ...

  3. (转)typedef 函数指针的用法

    typedef 函数指针的用法   在网上搜索函数指针,看到一个例子.开始没看懂,想放弃,可是转念一想,这个用法迟早要弄懂的,现在多花点时间看懂它,好过以后碰到了要再花一倍时间来弄懂它.其实很多时候都 ...

  4. TinyOS节点间通信相关接口和组件介绍

    一.基本通信接口:   Packet:提供了对message_t抽象数据类型的基本访问.这个接口的命令有:清空消息内容,获得消息的有效载荷区长度,获得消息有效载荷区的指针. //tos/interfa ...

  5. Spring-注入外部值

    Spring注入需要初始化,但前面均使用硬编码注入,如: JavaConfig配置: package soundSystem; import org.springframework.stereotyp ...

  6. 如何Request客户端的传值的Data

    我们在做B/S的项目,客户端向服务端传值的时候,一般都是request接受. Request常用三个接受方式为:Request.QueryString,Request.Form,Request.Par ...

  7. doDBA 监控用法

    https://yq.aliyun.com/articles/67051 doDBA tools是什么 doDBA tools是一个基于控制台的远程监控工具,它不需要在本地/远程系统上安装任何软件,它 ...

  8. mysqldump如何针对某些数据库进行备份?针对某个数据库进行备份?

    需求描述: 通过mysqldump工具对mysql服务器中的某几个数据库进行备份. 或者就对其中的一个数据库进行备份. 操作过程: 1.通过--databases参数后面加上数据库的名字进行备份 [m ...

  9. Activity、Window和View三者间的关系有一定的见解

    一.简述如何将Activity展现在手机上 Tips: Activity本身是没办法处理显示什么控件(view)的,是通过PhoneWindow进行显示的 换句话说:activity就是在造Phone ...

  10. 使用reduce的方法实现对象数组去重

    在开发中和面试当中,数组去重问题往往是受宠儿,那用最短的代码解决这个问题会使效率得到更大的提升.普通的数组,我们可以通过filter过滤方法进行去重,详情见本人博客:http://www.cnblog ...