poj 3621(最优比率环)
Sightseeing Cows
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
题意:求解一个图上的环上点权和除边权和最大值
sigma(val[i])/sigma(w[i])最大
和求最优比率生成树类似 sigma(val[i])/sigma(w[i])>=x;
-->sigma(val[i]-w[i]*x)>=0;
由于题目说的是图上的环 我们可以转化成图上的负权环 用spfa求解
如何转换负权环,我们将u-v边的边权变为x*edge[i].w-val[u];
所以在二分的时候 跑spfa判断是否是负环 是的话则l=mid;否则r=mid;
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.000001;
const int N=+;
const ll mod=1e9+;
int head[N];
int tot;
struct node{
int to,next;
int w;
}edge[N<<];
int vis[N];
double dis[N];
int val[N];
int num[N];
void init(){
memset(head,-,sizeof(head));
tot=;
}
void add(int u,int v,int w){
edge[tot].to=v;
edge[tot].next=head[u];
edge[tot].w=w;
head[u]=tot++;
}
int n,m;
int spfa(double x){
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
for(int i = ;i<=n;i++)dis[i]=1e15;
queue<int>q;
q.push();
dis[]=;
num[]++;
vis[]=;
while(q.empty()==){
int u=q.front();
q.pop();
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
int w=edge[i].w;
if(dis[u]+w*x-val[u]<dis[v]){
dis[v]=dis[u]+x*w-val[u];
if(vis[v]==){
q.push(v);
vis[v]=;
num[v]++;
if(num[v]>n)return ;//存在负权环
}
}
}
}
return ;
}
int main(){
init();
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",&val[i]);
for(int i=;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
//add(v,u,w);
}
double low=0.0;
double high=;
double ans=;
while(low+eps<high){
double mid=(low+high)/2.0;
if(spfa(mid)){
ans=mid;
low=mid;
}else{
high=mid;
}
}
printf("%.2f\n",ans);
}
/*
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
*/
poj 3621(最优比率环)的更多相关文章
- poj 3621(最优比率环)
题目链接:http://poj.org/problem?id=3621 思路:之前做过最小比率生成树,也是属于0/1整数划分问题,这次碰到这道最优比率环,很是熟悉,可惜精度没控制好,要不就是wa,要不 ...
- POJ 3621-Sightseeing Cows-最优比率环|SPFA+二分
最优比率环问题.二分答案,对于每一个mid,把节点的happy值归类到边上. 对于每条边,用mid×weight减去happy值,如果不存在负环,说明还可以更大. /*---------------- ...
- POJ 3621 最优比率生成环
题意: 让你求出一个最优比率生成环. 思路: 又是一个01分化基础题目,直接在jude的时候找出一个sigma(d[i] * x[i])大于等于0的环就行了,我是用SPFA跑最长路 ...
- Sightseeing Cows(最优比率环)
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8915 Accepted: 3000 ...
- [转]01分数规划算法 ACM 二分 Dinkelbach 最优比率生成树 最优比率环
01分数规划 前置技能 二分思想最短路算法一些数学脑细胞? 问题模型1 基本01分数规划问题 给定nn个二元组(valuei,costi)(valuei,costi),valueivaluei是选择此 ...
- 【poj3621】最优比率环
题意: 给定n个点,每个点有一个开心度F[i],每个点有m条单向边,每条边有一个长度d,要求一个环,使得它的 开心度的和/长度和 这个比值最大.n<=1000,m<=5000 题解: 最优 ...
- poj 3621最优比例生成环(01分数规划问题)
/* 和求最小生成树差不多 转载思路:http://www.cnblogs.com/wally/p/3228171.html 思路:之前做过最小比率生成树,也是属于0/1整数划分问题,这次碰到这道最优 ...
- POJ 3621:Sightseeing Cows(最优比率环)
http://poj.org/problem?id=3621 题意:有n个点m条有向边,每个点有一个点权val[i],边有边权w(i, j).找一个环使得Σ(val) / Σ(w)最大,并输出. 思路 ...
- POJ 3621 Sightseeing Cows (最优比率环 01分数划分)
题意: 给定L个点, P条边的有向图, 每个点有一个价值, 但只在第一经过获得, 每条边有一个花费, 每次经过都要付出这个花费, 在图中找出一个环, 使得价值之和/花费之和 最大 分析: 这道题其实并 ...
随机推荐
- (八)python3 迭代
迭代:如果给定一个 list 或 tuple,我们可以通过 for 循环来遍历这个 list 或tuple,这种遍历我们称为迭代(Iteration) 字典: >>> d = {'a ...
- YOLOv3配置(win10+opencv3.40+cuda9.1+cudnn7.1+vs2015)
最近心血来潮想学一下YOLOv3,于是就去网上看了YOLOv3在win10下的配置教程.在配置过程中塌坑无数,花了很多时间和精力,所以我想就此写一篇博客来介绍在在win10+vs2015的环境下如何配 ...
- 洛谷 3871 [TJOI2010]中位数
[题解] 平衡树模板题,不过因为可以离线,所以有别的做法.把询问倒着做,变成删掉数字.求中位数,于是可以二分+树状数组. #include<cstdio> #include<cstr ...
- GitHub总结
1) 工作原理 2) 工作流程 clone资源到本地 更新本地资源 新增或修改clone的资源 查看状态 资源推送回github
- STL map的用法介绍!
map对象的定义 使用前添加map头文件,必须分别指明键和值的类型: map<string,int>word_count; map的构造函数: map<k,v>m; 创建一 ...
- mysql和Oracle 备份表
1.SQL Server中,如果目标表存在: insert into 目标表 select * from 原表; 2.SQL Server中,,如果目标表不存在: select * into 目标表 ...
- POJ3169 差分约束 线性
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12522 Accepted: 6032 Descripti ...
- Django用法补充
1. 自定义Admin from django.contrib import admin from xx import models # 自定义操作 class CustomerAdmin(admin ...
- Redis 命令与连接【十一】
---------------------Redis 命令--------------- Redis 命令用于在 redis 服务上执行操作. 要在 redis 服务上执行命令需要一个 redis 客 ...
- AOJ 0118 Property Distribution (DFS)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=46522 简单DFS,题目翻译参考 http://blog.csdn.net ...