POJ3621 Sightseeing Cows 最优比率环 二分法
题目链接:http://poj.org/problem?id=3621
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 10552 | Accepted: 3613 |
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
Source
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 5e3+; int n, m, val[MAXN];
struct edge
{
int to, w, next;
}edge[MAXN];
int cnt, head[MAXN]; void add(int u, int v, int w)
{
edge[cnt].to = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
} double dis[MAXN];
int times[MAXN], inq[MAXN], vis[MAXN];
bool spfa(double L)
{
memset(vis, , sizeof(vis));
memset(inq, , sizeof(inq));
memset(times, , sizeof(times));
for(int i = ; i<=n; i++)
dis[i] = INF; queue<int>Q;
Q.push();
inq[] = ;
vis[] = ;
dis[] = ;
while(!Q.empty())
{
int u = Q.front();
Q.pop(); inq[u] = ;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
// 距离全部取反, 看是否存在正环
if(dis[v]> dis[u]-(val[u]-edge[i].w*L) )
{
dis[v] = dis[u]-(val[u]-edge[i].w*L);
if(!inq[v])
{
Q.push(v);
inq[v] = ;
vis[v] = ;
if(++times[v]>n) return true; //检测到了负环,但因为数值全部取反了,所以实际上为检测到了正环
}
}
}
}
return false;
} void init()
{
cnt = ;
memset(head, -, sizeof(head));
} int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
for(int i = ; i<=n; i++)
scanf("%d", &val[i]); init();
for(int i = ; i<=m; i++)
{
int u, v, w;
scanf("%d%d%d",&u, &v, &w);
add(u,v,w);
} double l = , r = 1e3;
while(l+EPS<=r)
{
double mid = (l+r)/;
if(spfa(mid)) //存在正环,继续增大比率
l = mid + EPS;
else
r = mid - EPS;
}
printf("%.2f\n", r);
}
}
POJ3621 Sightseeing Cows 最优比率环 二分法的更多相关文章
- POJ3621 Sightseeing Cows(最优比率环)
题目链接:id=3621">http://poj.org/problem?id=3621 在一个有向图中选一个环,使得环上的点权和除以边权和最大.求这个比值. 经典的分数规划问题,我认 ...
- POJ 3621 Sightseeing Cows [最优比率环]
感觉去年9月的自己好$naive$ http://www.cnblogs.com/candy99/p/5868948.html 现在不也是嘛 裸题,具体看学习笔记 二分答案之后判负环就行了 $dfs$ ...
- Sightseeing Cows(最优比率环)
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8915 Accepted: 3000 ...
- 【poj3621】最优比率环
题意: 给定n个点,每个点有一个开心度F[i],每个点有m条单向边,每条边有一个长度d,要求一个环,使得它的 开心度的和/长度和 这个比值最大.n<=1000,m<=5000 题解: 最优 ...
- [转]01分数规划算法 ACM 二分 Dinkelbach 最优比率生成树 最优比率环
01分数规划 前置技能 二分思想最短路算法一些数学脑细胞? 问题模型1 基本01分数规划问题 给定nn个二元组(valuei,costi)(valuei,costi),valueivaluei是选择此 ...
- poj 3621(最优比率环)
题目链接:http://poj.org/problem?id=3621 思路:之前做过最小比率生成树,也是属于0/1整数划分问题,这次碰到这道最优比率环,很是熟悉,可惜精度没控制好,要不就是wa,要不 ...
- POJ 3621-Sightseeing Cows-最优比率环|SPFA+二分
最优比率环问题.二分答案,对于每一个mid,把节点的happy值归类到边上. 对于每条边,用mid×weight减去happy值,如果不存在负环,说明还可以更大. /*---------------- ...
- POJ 3621 Sightseeing Cows (最优比率环 01分数划分)
题意: 给定L个点, P条边的有向图, 每个点有一个价值, 但只在第一经过获得, 每条边有一个花费, 每次经过都要付出这个花费, 在图中找出一个环, 使得价值之和/花费之和 最大 分析: 这道题其实并 ...
- POJ 3621:Sightseeing Cows(最优比率环)
http://poj.org/problem?id=3621 题意:有n个点m条有向边,每个点有一个点权val[i],边有边权w(i, j).找一个环使得Σ(val) / Σ(w)最大,并输出. 思路 ...
随机推荐
- MyEclipse6.5增加对Tomcat7的支持
MyEclipse6.5增加对Tomcat7的支持 最近在研究Servlet3.0,它是JavaEE6.0规范中的一部分 而Servlet3.0对服务器是有要求的,比如Tomcat7+(而Tomcat ...
- miller_rabin + pollard_rho模版
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<math.h> #incl ...
- BZOJ4723: [POI2017]Flappy Bird
$n \leq 500000$个水管,每秒横坐标加一,纵坐标如果你点击就+1否则-1,问从$(0,0)$飞到$m$处最少点多少次,或者说明无解. 如果能飞到某个水管的高度区间$[L,R]$,那么答案肯 ...
- GoldenDict词典下载安装
Debian/Ubuntu下载: sudo apt-get install goldendict 添加中文维基百科/维基词典: 选择[词典]->[词典来源]->[维基百科]->[添加 ...
- css3的一些新属性及部分用法
CSS3是CSS(层叠样式表)技术的升级版本,增加了很多新属性,我们在web开发中采用css3技术可以提高程序的性能以及用户体验.而且一般面试中会问到知道哪些新增加的属性,我们不可能将所有东西一一复述 ...
- HDU_4770 Lights Against Dudely 状压+剪枝
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4770 Lights Against Dudely Time Limit: 2000/1000 MS ( ...
- Centos常用命名
1.关机 (系统的关机.重启以及登出 ) 的命令 shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown -h hours: ...
- Android Studio 2.0 Beta 5公布,修复几个与即时执行相关的严重BUG.
Android Studio 2.0 Beta 5公布,修复几个与即时执行相关的严重BUG. This build fixes a couple of important bugs related t ...
- python 工具ScreenShoot
环境:windows python3 # -*- coding: UTF-8 -*- import time import os, win32gui, win32ui, win32con, win32 ...
- ZT:150条毒鸡汤
1.照照镜子吧,还要什么段子? 2.多年过去,再回忆高考,其实本质上没有考到好与坏的说法,重要的是年轻人在一起,做份试题,然后决定去哪座城市做代购. 3.真正努力过的人,就会明白天赋的重要性. 4.转 ...