POJ 3621 Sightseeing Cows 最大密度环 01分数规划
来源:互联网 发布:淘宝3c认证证书 编辑:程序博客网 时间:2024/05/12 09:43
最大密度环 01分数规划
首先的一个结论就是,不会存在环套环的问题,即最优的方案一定是一个单独的环,而不是大环套着小环的形式。这个的证明其实非常的简单,大家可以自己想一下(提示,将大环上的收益和记为x1,花费为y1,小环上的为x2,y2。重叠部分的花费为S。表示出来分类讨论即可)。有了这个结论,我们就可以将花费和收益都转移到边上来了,因为答案最终一定是一个环,所以我们将每一条边的收益规定为其终点的收益,这样一个环上所有的花费和收益都能够被正确的统计。
解决了蛋疼的问题之后,就是01分数规划的部分了,我们只需要计算出D数组后找找有没有正权环即可,不过这样不太好,不是我们熟悉的问题,将D数组全部取反之后,问题转换为查找有没有负权环,用spfa或是bellman_ford都可以。这道题目就是典型的不适合用Dinkelbach,记录一个负权环还是比较麻烦的,所以二分搞定。
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 73010105101 2 32 3 23 4 53 5 24 5 55 1 35 2 2
Sample Output
6.00
Source
[Submit] [Go Back] [Status] [Discuss]
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <cmath>using namespace std;const int maxn=5500;const double eps=1e-7;int n,m;double fun[maxn/5];struct Edge{ int to,next; double w;}edge[maxn*2];int Adj[maxn/5],Size;void init(){ memset(Adj,-1,sizeof(Adj));Size=0;}void add_edge(int u,int v,double w){ edge[Size].to=v; edge[Size].w=w; edge[Size].next=Adj[u]; Adj[u]=Size++;}double dist[maxn/5];int cq[maxn/5];bool inq[maxn/5];bool spfa(double r){ for(int i=0;i<=n+1;i++) dist[i]=1e30; memset(cq,0,sizeof(cq)); memset(inq,false,sizeof(inq)); dist[1]=0; queue<int> q; inq[1]=true; q.push(1); cq[1]=1; while(!q.empty()) { int u=q.front(); q.pop(); inq[u]=false; for(int i=Adj[u];~i;i=edge[i].next) { int v=edge[i].to; double W=edge[i].w*r-fun[v]; if(dist[v]>dist[u]+W) { dist[v]=dist[u]+W; if(!inq[v]) { inq[v]=true; cq[v]++; if(cq[v]>=n) return true; q.push(v); } } } } return false;}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { init(); for(int i=1;i<=n;i++) scanf("%lf",fun+i); for(int i=0;i<m;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); add_edge(a,b,c); } double low=0.,high=1000.,mid,ans; while(fabs(low-high)>eps) { mid=(low+high)/2.; if(spfa(mid)) { ans=low=mid; } else high=mid; } printf("%.2lf\n",ans); } return 0;}
- POJ 3621 Sightseeing Cows 最大密度环 01分数规划
- POJ 3621 Sightseeing Cows 01分数规划
- POJ 3621--Sightseeing Cows(0-1规划求最大密度)
- poj3621 Sightseeing Cows 【最大比例环 01分数规划】
- POJ 3621 Sightseeing Cows(01分数规划)
- poj-3621-Sightseeing Cows-01分数规划+spfa判负环
- poj 3621 Sightseeing Cows(最优比例生成环,01分数规划)
- poj 3621 Sightseeing Cows ( 求最优比例生成环/01分数规划 )
- poj 3621 Sightseeing Cows (最优比率生成环 01分数规划问题 )
- POJ 3621 Sightseeing Cows(最优比率环/01分数规划)
- 【分数规划-最大比例环】POJ3621[Sightseeing Cows]题解
- POJ 3621(0/1分数规划,二分) Sightseeing Cows
- poj Sightseeing Cows 分数规划+spfa
- poj 3621 Sightseeing Cows 【最优比例环】 【0-1分数规划 + SPFA判负环】
- POJ 3621 Sightseeing Cows 01分数规划(参数搜索)+最短路
- POJ 3621 Sightseeing Cows(01分数规划+二分+Spfa判负环)
- pku 3621 01分数规划 sightseeing cows 解题报告
- poj3621 Sightseeing Cows --- 01分数规划
- Google Protocol Buffer 的使用和原理
- Android ListView 隔11行重复选中的问题
- java泛型:有关ArrayList用泛型和不用泛型的一个添加名言和删除名言的对比例子
- Output argument "fuse" (and maybe others) not assigned during call to
- EXT弹出不同对话框
- POJ 3621 Sightseeing Cows 最大密度环 01分数规划
- Android录音时指针摆动的实现
- 五大常用算法之四:回溯法
- android 日期选择器
- 不可重入函数与线程安全函数
- 环境搭建、部署备忘
- UVA - 662 Fast Food 区间DP
- hiho一下 第二十一周 离散化与线段树回顾
- Snandy Stop, thinking is the essence of progress. RequireJS入门(二)