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)最大,并输出. 思路 ...
随机推荐
- getParameter 与 getAttribute的区别
request.getAttribute():是request时设置的变量的值,用request.setAttribute("name","您自己的值");来设 ...
- MySql将查询结果插入到另外一张表
今天遇到一个业务需求是这样的:对在职员工超过55岁提醒.我想的思路是查询员工表,然后将超过55岁的人的信息存到另一个表,并且以消息的形式给用户提示,用户处理掉之后此消息失效(在数据库做标记). 不管是 ...
- Codeforces Round #291 (Div. 2) C. Watto and Mechanism [字典树]
传送门 C. Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- 标准C程序设计七---23
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- 图片裁剪上传插件——jquery.photoClip.js
想要裁剪图片上传: 需要依赖的的插件为: [jquery.photoClip.js] 插件[iscroll-zoom.js] 插件[hammer.js] 插件 [lrz.all.bundle.js] ...
- API调用开发demo
package fastjson; import java.io.BufferedReader;import java.io.IOException;import java.io.InputStrea ...
- linux 每天备份mysql数据
原文:http://www.open-open.com/code/view/1433587311890 前言: 如果你的数据库文件较大,可能备份的时候需要占用很多服务器资源,所以站长要尽量选择访客最少 ...
- ubuntu compile php from source code
10down vote Assuming that you already have the OpenSSL libraries and header files (on rpm systems th ...
- ivy在eclipse中的重新加载
ivy在eclipse中的重新加载 如果由于网速的原因,导致了ivy没有正常的加载,可以进行ivy的重新加载: 1,右键点击项目,选择属性->Java Build Path->Librat ...
- 关于HTML中文乱码问题
系统:ubuntu 14.04 软件:bluefish 一.乱码原因 1.不同编码内容混杂:HTML乱码是因为html编码问题照成(常见gb2312与utf-8两种编码内容同一时候存在照成) 2.未设 ...