Sightseeing Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8915   Accepted: 3000

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
题解:
题意就是,这个人带牛旅行,旅行每个城市会有幸福度,通过每个城市会花费时间,让找平均每秒的最大幸福度;
最短路01分数规划,也就是最优比率环问题;刚开始用了Bellman各种超时,换了SPFA也是,最后把INF改大了,SPFA过了,最后想想BELLMAN的时间复杂度N*M又改了种写法,就各种wa了,真心疲惫;
SPFA:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=10000000000000;
typedef long long LL;
const int MAXN=1010;
const int MAXM=100010;
/*struct Node{
int u,v;
double t;
};
Node dt[MAXM];*/
struct Edge{
int from,to,next,t;
};
Edge edg[MAXM];
int head[MAXM];
int edgnum;
void add(int u,int v,int t){
Edge E={u,v,head[u],t};
edg[edgnum]=E;
head[u]=edgnum++;
}
int L,P;
double hp[MAXN],dis[MAXN];
int usd[MAXN],vis[MAXN];
/*void add(int u,int v,double t){
Node E={u,v,t};
dt[edgnum++]=E;
}*/
//double R;
/*bool Bellman(){
mem(dis,INF);
mem(usd,0);
dis[1]=0;
while(1){
int temp=0;
for(int j=0;j<edgnum;j++){
int u=dt[j].u,v=dt[j].v;
double t=dt[j].t;
//dis[v]=min(dis[v],dis[u]+R*t-hp[u]);//应该是R*t-hp[u];
if(dis[v]>dis[u]+R*t-hp[u])usd[v]++,dis[v]=dis[u]+R*t-hp[u],temp=1;
if(usd[v]>L)return false;
}
if(!temp)return true;
}
}*/
bool SPFA(double R){
queue<int>dl;
while(!dl.empty())dl.pop();
for(int i=1;i<=L;i++){
dis[i]=INF;
vis[i]=0;
usd[i]=0;
}
dl.push(1);
vis[1]=1;
usd[1]++;
dis[1]=0;
while(!dl.empty()){
int u=dl.front();
dl.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=edg[i].next){
int v=edg[i].to,t=edg[i].t;
if(dis[v]>dis[u]+R*t-hp[u]){
dis[v]=dis[u]+R*t-hp[u];
if(!vis[v]){
vis[v]=1;
usd[v]++;
dl.push(v);
// printf("%d\n",usd[v]);
if(usd[v]>=L)return false;
}
}
}
}
return true;
}
int main(){
int a,b;
int c;
while(~scanf("%d%d",&L,&P)){
edgnum=0;
double mih=INF,mxh=-INF;
int mit=INF,mxt=-INF;
mem(head,-1);
for(int i=1;i<=L;i++){
scanf("%lf",hp+i);
mih=min(mih,hp[i]);
mxh=max(mxh,hp[i]);
}
while(P--){
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
mit=min(mit,c);
mxt=max(mxt,c);
}
double l=mih/mxt,r=mxh/mit;
// printf("%f %f\n",l,r);
double R;
while(r-l>=0.001){
R=(l+r)/2;
if(SPFA(R))r=R;
else l=R;
}
printf("%.2f\n",l);
}
return 0;
}

  

  Bellman还在wa,贴上求大神帮忙看看;

wa代码:

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=;
typedef long long LL;
const int MAXN=;
const int MAXM=;
struct Node{
int u,v;
int t;
};
Node dt[MAXM];
/*struct Edge{
int from,to,next,t;
};
Edge edg[MAXM];
int head[MAXM];*/
int edgnum;
/*void add(int u,int v,int t){
Edge E={u,v,head[u],t};
edg[edgnum]=E;
head[u]=edgnum++;
}*/
int L,P;
double hp[MAXN],dis[MAXN];
int usd[MAXN],vis[MAXN];
void add(int u,int v,int t){
Node E={u,v,t};
dt[edgnum++]=E;
}
//double R;
bool Bellman(double R){
for(int i=;i<=L;i++){
usd[i]=;
dis[i]=INF;
}
dis[]=;
usd[]++;
while(){
int temp=;
for(int j=;j<edgnum;j++){
int u=dt[j].u,v=dt[j].v;
int t=dt[j].t;
//dis[v]=min(dis[v],dis[u]+R*t-hp[u]);//应该是R*t-hp[u];
if(dis[v]>dis[u]+R*t-hp[u])dis[v]=dis[u]+R*t-hp[u],temp=,usd[v]++;
if(usd[v]>=L)return false;
}
if(!temp)return true;
}
/*for(int j=0;j<edgnum;j++){
int u=dt[j].u,v=dt[j].v;
int t=dt[j].t;
if(dis[v]>dis[u]+R*t-hp[u])return false;
}
return true;*/
}
/*bool SPFA(double R){
queue<int>dl;
while(!dl.empty())dl.pop();
for(int i=1;i<=L;i++){
dis[i]=INF;
vis[i]=0;
usd[i]=0;
}
dl.push(1);
vis[1]=1;
usd[1]++;
dis[1]=0;
while(!dl.empty()){
int u=dl.front();
dl.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=edg[i].next){
int v=edg[i].to,t=edg[i].t;
if(dis[v]>dis[u]+R*t-hp[u]){
dis[v]=dis[u]+R*t-hp[u];
if(!vis[v]){
vis[v]=1;
usd[v]++;
dl.push(v);
// printf("%d\n",usd[v]);
if(usd[v]>=L)return false;
}
}
}
}
return true;
}*/
int main(){
int a,b;
int c;
while(~scanf("%d%d",&L,&P)){
edgnum=;
// double mih=INF,mxh=-INF,mit=INF,mxt=-INF;
//mem(head,-1);
for(int i=;i<=L;i++){
scanf("%lf",hp+i);
// mih=min(mih,hp[i]);
// mxh=max(mxh,hp[i]);
}
while(P--){
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
//mit=min(mit,c);
// mxt=max(mxt,c);
}
double l=,r=;
// printf("%f %f\n",l,r);
double ans=;
double R;
while(r-l>=0.001){
R=(l+r)/;
if(Bellman(R))r=R;
else ans=R,l=R;
}
printf("%.2f\n",ans);
}
return ;
}

Sightseeing Cows(最优比率环)的更多相关文章

  1. POJ3621 Sightseeing Cows 最优比率环 二分法

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

  2. POJ3621 Sightseeing Cows(最优比率环)

    题目链接:id=3621">http://poj.org/problem?id=3621 在一个有向图中选一个环,使得环上的点权和除以边权和最大.求这个比值. 经典的分数规划问题,我认 ...

  3. POJ 3621 Sightseeing Cows [最优比率环]

    感觉去年9月的自己好$naive$ http://www.cnblogs.com/candy99/p/5868948.html 现在不也是嘛 裸题,具体看学习笔记 二分答案之后判负环就行了 $dfs$ ...

  4. [转]01分数规划算法 ACM 二分 Dinkelbach 最优比率生成树 最优比率环

    01分数规划 前置技能 二分思想最短路算法一些数学脑细胞? 问题模型1 基本01分数规划问题 给定nn个二元组(valuei,costi)(valuei,costi),valueivaluei是选择此 ...

  5. poj 3621(最优比率环)

    题目链接:http://poj.org/problem?id=3621 思路:之前做过最小比率生成树,也是属于0/1整数划分问题,这次碰到这道最优比率环,很是熟悉,可惜精度没控制好,要不就是wa,要不 ...

  6. POJ 3621-Sightseeing Cows-最优比率环|SPFA+二分

    最优比率环问题.二分答案,对于每一个mid,把节点的happy值归类到边上. 对于每条边,用mid×weight减去happy值,如果不存在负环,说明还可以更大. /*---------------- ...

  7. 【poj3621】最优比率环

    题意: 给定n个点,每个点有一个开心度F[i],每个点有m条单向边,每条边有一个长度d,要求一个环,使得它的 开心度的和/长度和 这个比值最大.n<=1000,m<=5000 题解: 最优 ...

  8. POJ 3621 Sightseeing Cows (最优比率环 01分数划分)

    题意: 给定L个点, P条边的有向图, 每个点有一个价值, 但只在第一经过获得, 每条边有一个花费, 每次经过都要付出这个花费, 在图中找出一个环, 使得价值之和/花费之和 最大 分析: 这道题其实并 ...

  9. POJ 3621:Sightseeing Cows(最优比率环)

    http://poj.org/problem?id=3621 题意:有n个点m条有向边,每个点有一个点权val[i],边有边权w(i, j).找一个环使得Σ(val) / Σ(w)最大,并输出. 思路 ...

随机推荐

  1. 用例图(UseCase Diagram)—UML图(一)

      从上面的用例图模型,我们可以大致了解用例图所描述的是什么.下面进行详细介绍. 用例图,即用来描述什么角色通过某某系统能做什么事情的图,用例图关注的是系统的外在表现,系统与人的交互,系统与其它系统的 ...

  2. A - FatMouse' Trade

    Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the wareho ...

  3. day5_python学习笔记_chapter6_字符串列表元组

    1. 序列:seq[n], seq[x:y], seq * n序列重复n次,切片, 序列翻转 s=”abcde", s[::-1]="edcba" 内建函数:1. 类型转 ...

  4. [转] jQuery 操作 JSON 数据

    jquery下json数组的操作用法实例: jquery中操作JSON数组的情况中遍历方法用的比较多,但用添加移除这些好像就不是太多了. 试过json[i].remove(),json.remove( ...

  5. Let's Format Css Documents

    每次想参考一些好看网站的时候,打开css文档都是一行的,琢磨了下就自己写了块短短的代码,各路Java大神别笑我呀.^_^ 复制粘贴控制台的输出就好了.(瞬间觉得跟上大神的脚步了←_←) package ...

  6. 循环-21. 求交错序列前N项和

    /* * Main.c * C21-循环-21. 求交错序列前N项和 * Created on: 2014年8月18日 * Author: Boomkeeper ***********测试通过**** ...

  7. .NET 条件查询实现--类似网上商城宝贝搜索

    需要实现的效果:点击表格列头:弹出一个层,用户可以输入当前列头的查询条件,点击确定之后,把该列头的查询信息显示在页面顶部,用户可以叉掉这个查询条件,恢复到查询之前的数据. 大致实现的效果图: 项目背景 ...

  8. Linux例行工作crontab

    第一步编辑要定时执行的脚本: myScript.sh myScript.sh的内容为:touch /root/`date +%F' '%T`.txt 为myScript.sh增加可执行权限:chmod ...

  9. css的repaint和reflow

    css的repaint和reflow 浏览器为了重新渲染部分或整个页面,重新计算页面元素位置和几何结构(geometries)的进程叫做 reflow. 由于 reflow 是一种浏览器中的用户拦截( ...

  10. [置顶] C++基础之六:运算符的重载

    网上太多有关运算符的重载了,但是写的太过的详细,不适合新手入门,特别是那什么++和--的前增量后增量重载,一元二元运算符重载,特殊运算符,下标运算符,new和delete,甚至是指针运算符的重载,吓退 ...