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 ...
随机推荐
- Networking with PHP
PHP Advanced and Object-Oriented Programming 3rd Edition
- Lazarus如何变成XE的界面
先设置: 进入“配置安装的包”,选中 EasyDockMgr 和 easyDockMgrDsgn 这两项,重新编译 Lazarus 吧 修改以后,界面就变成了XE的了:
- Android获取手机制作商,系统版本等
在开发中 我们有时候会需要获取当前手机的系统版本来进行判断,或者需要获取一些当前手机的硬件信息. android.os.Build类中.包括了这样的一些信息.我们可以直接调用 而不需要添加任何的权限和 ...
- ubuntu12.04 安装 php5.4/php5.5
1:修改源(我使用163的源)直接修改/etc/apt/sources.list deb http://mirrors.163.com/ubuntu/ precise main universe re ...
- 蓝牙的SDP协议总结
1.概念 SDP协议让客户机的应用程序发现存在的服务器应用程序提供的服务以及这些服务的属性.SDP只提供发现服务的机制,不提供使用这些服务的方法.每个蓝牙设备都需要一个SDP Service, ...
- Instance of 和getClass()区别概述
instance of 运算符和getClass()方法都可以用来检查一个对象所属的类.instance of 运算符仅测试一个对象的类型:getClass()方法与==或!=结合使用,测试两个对象是 ...
- Socket通信原理探讨(C++为例) good
http://www.cnblogs.com/xufeiyang/articles/4878096.html http://www.cnblogs.com/xufeiyang/articles/453 ...
- Object C语法学习
#synthesize关键字: 根据@property设置,自动生成成员变量相应的存取方法,从而可以使用点操作符来方便的存取该成员变量 . @implementation 关键字,表明类的实现 @en ...
- jQuery插件之Wookmark瀑布流
使用方法: 1.下载wookmark.js 2.构建html <div class="wrapper"> <div id="con1_1"&g ...
- [LeetCode]题解(python):036-Valid Sudoku
题目来源 https://leetcode.com/problems/valid-sudoku/ etermine if a Sudoku is valid, according to: Sudoku ...