POJ-3621: Sightseeing Cows【最优比率生成环】
来源:互联网 发布:家电销售数据 编辑:程序博客网 时间:2024/05/16 14:20
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
题意:n个点,m条边,每个点有自己的值p[i],m条单向边,每条边有自己的值val[i],求一个环,环中的点的值之和比上边值的和最大,即Σp[i]/Σval[i]最大,求这个最大值。
之前写过的01分数规划的更高级的应用吧……中间的check函数变成一个spfa,用于判断是否存在一个具有更优答案的环。【简单01分数规划中,check函数用于判断是否存在一个更优解,若存在就继续二分,这个地方也是类似的】
要得到Σp[i]/Σval[i]的最大值,那么设有函数f[i]=val[i]*r-p[i],若f[i]<=0,则有p[i]/val[i]>=r
f[i]<=0,那么就是说存在一个负环,判断是否存在负环,即为cnt[i]>=n?存在:不存在;
#include<stdio.h>#include<string.h>#include<queue>#define eps 0.0000001using namespace std;const int maxn=1111;double p[maxn]; //pointdouble dis[maxn];int cnt[maxn],vis[maxn];int pos[maxn];int num;int n,m;struct node{ int en,next; double val; node(int en=0,int next=0,double val=0.0):en(en),next(next),val(val){}}E[maxn*10];void add(int st,int en,double val){ E[num]=node(en,pos[st],val); pos[st]=num++;}void init(){ memset(dis,0x43,sizeof(dis)); memset(cnt,0,sizeof(cnt)); memset(vis,0,sizeof(vis));}int spfa(double r) //f[i]=val[i]*r-p[i] f[i]<=0 -> p[i]/val[i]>=r{ //若有负环出现,则说明有f[i]<=0 queue<int>q; init(); dis[1]=0; cnt[1]++; q.push(1); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(int i=pos[u];i!=-1;i=E[i].next) { int v=E[i].en; double val=E[i].val*r-p[v]; if(dis[v]>dis[u]+val) { dis[v]=dis[u]+val; if(!vis[v]) { vis[v]=1; cnt[v]++; if(cnt[v]>=n) return 1; q.push(v); } } } } return 0;}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { num=0; memset(pos,-1,sizeof(pos)); for(int i=1;i<=n;i++) scanf("%lf",&p[i]); //point int x,y; double z; while(m--) { scanf("%d%d%lf",&x,&y,&z); add(x,y,z); } double l=0,r=10000; while(r-l>=eps) { double mid=(l+r)/2.0; if(spfa(mid)) l=mid; else r=mid; } printf("%.2f\n",l); } return 0;}
- POJ-3621: Sightseeing Cows【最优比率生成环】
- POJ 3621 Sightseeing Cows 最优比率环
- POJ 3621 Sightseeing Cows 最优比率环
- poj 3621 Sightseeing Cows(最优比率环)
- poj 3621 Sightseeing Cows (最优比率环)
- poj 3621 Sightseeing Cows (最优比率生成环 01分数规划问题 )
- POJ3621 Sightseeing Cows 最短路求最优比率生成环
- 【分数规划】【最优比率环 & 最优比率生成树】poj3621 Sightseeing Cows && poj2728 Desert King
- POJ 3621 Sightseeing Cows(最优比率环/01分数规划)
- POJ3621 Sightseeing Cows(最优比率环)
- POJ 3621 最优比率生成环
- poj 3621 Sightseeing Cows(最优比例生成环,01分数规划)
- poj 3621 Sightseeing Cows ( 求最优比例生成环/01分数规划 )
- poj 3621(最优比率环)
- POJ 3621 最优比率环
- POJ 3621 最优比率生成环 01分数规划问题
- POJ 3621 最优比率生成环 二分求解
- poj 3621 Sightseeing Cows 【最优比例环】 【0-1分数规划 + SPFA判负环】
- java实现遍历树形菜单方法——struts.xml实现
- [leetcode]Remove Duplicates from Sorted Array(Java实现)
- 14多校 B-A simple dynamic programming problem
- 东莞市2011年信息学特长生测试题 花店布置(dp)
- 记录一些面试问题吧。
- POJ-3621: Sightseeing Cows【最优比率生成环】
- java I/O系统(8)-文件压缩
- main(int argc,char *argv[])
- 菜鸟初学maven,pom标签解释
- ninja ripper新版教程
- Maven-sonarqube-jenkins-git 持续集成开发环境的搭建
- 理解一般指针和指向指针的指针
- 12.OP-TEE OS启动(三)--service_init
- Eclipse自动注册Servlet:web.xml注册和@Servlet注解 实现注册