01分数规划POJ3621(最优比例生成环)
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(最优比例生成环)的更多相关文章
- POJ 3621 Sightseeing Cows 01分数规划,最优比例环的问题
http://www.cnblogs.com/wally/p/3228171.html 题解请戳上面 然后对于01规划的总结 1:对于一个表,求最优比例 这种就是每个点位有benefit和cost,这 ...
- poj 3621 0/1分数规划求最优比率生成环
思路:以val[u]-ans*edge[i].len最为边权,判断是否有正环存在,若有,那么就是ans小了.否则就是大了. 在spfa判环时,先将所有点进队列. #include<iostrea ...
- POJ 2728 Desert King 01分数规划,最优比率生成树
一个完全图,每两个点之间的cost是海拔差距的绝对值,长度是平面欧式距离, 让你找到一棵生成树,使得树边的的cost的和/距离的和,比例最小 然后就是最优比例生成树,也就是01规划裸题 看这一发:ht ...
- 最优比例生成环(dfs判正环或spfa判负环)
http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- poj 3621最优比例生成环(01分数规划问题)
/* 和求最小生成树差不多 转载思路:http://www.cnblogs.com/wally/p/3228171.html 思路:之前做过最小比率生成树,也是属于0/1整数划分问题,这次碰到这道最优 ...
- BZOJ 1486 最小圈(01分数规划)
好像是很normal的01分数规划题.最小比率生成环. u(c)=sigma(E)/k.转化一下就是k*u(c)=sigma(E). sigma(E-u(c))=0. 所以答案对于这个式子是有单调性的 ...
- 【转】[Algorithm]01分数规划
因为搜索关于CFRound277.5E题的题解时发现了这篇文章,很多地方都有值得借鉴的东西,因此转了过来 原文:http://www.cnblogs.com/perseawe/archive/2012 ...
- POJ 2976 Dropping tests 01分数规划 模板
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6373 Accepted: 2198 ...
- 【poj 2976】Dropping tests(算法效率--01分数规划 模版题+二分){附【转】01分数规划问题}
P.S.又是一个抽时间学了2个小时的新东西......讲解在上半部分,题解在下半部分. 先说一下转的原文:http://www.cnblogs.com/perseawe/archive/2012/05 ...
随机推荐
- simplify the design of the hardware forming the interface between the processor and thememory system
Computer Systems A Programmer's Perspective Second Edition Many computer systems place restrictions ...
- Python在windows下的安装与配置
安装python 文件准备: A. python安装文件:我用的是python-3.4.3.amd64.msi: 安装很简单,直接双击点下一步即可: 配置环境变量,在windows系统变量中找到pat ...
- linux 输入java 出现中文乱码
http://jingyan.baidu.com/article/ab69b270de8b4f2ca7189f1d.html 1.查看当前linux的编码 没设置之前是:zh_CN.UTF_8 输入 ...
- 【Demo】 生成二维码 和 条形码
条形码 和 二维码 对比 一维条形码只是在一个方向(一般是水平方向)表达信息,而在垂直方向则不表达任何信息,其一定的高度通常是为了便于阅读器的对准. 在水平和垂直方向的二维空间存储信息的条形码, 称为 ...
- nrf51822裸机教程-PPI
Programmable Peripheral Interconnect即可编程外设互联 系统,该模块是51822 提供的一个特性. 目的是为了让51822 的外围模块可以不通过处理器而自动相互作用. ...
- ubuntu cpus 共享打印
下载工具 axel 打印机 hp-setup http://blog.x1986.com/t/18.think lsusb wkhtmltopdf/0.12.2.1 ubuntu 14.01 x64下 ...
- HTML文件基本结构
固定结构: <html> <head>...</head> <body>...</body> </html>1,<html ...
- 闭包 Clousure
闭包 Clousure 参考:http://caibaojian.com/javascript-closures.html?fid=776%230-tsina-1-25974-397232819ff9 ...
- js获取div中的文本框数据
通过div得到div里的所有数据 大神的世界无需解释,当然不是说我,当我看到这些代码的时候我惊呆了! 这是一个工具方法js: js: /* * 获取指定对象下的所有input.textarea值 * ...
- Java: Difference between ArrayList and LinkedList
Basically, they are just two different implementations of List interface. LinkedList is implemented ...