01分数规划+spfa判负环 POJ3621 Sightseeing Cows

Sightseeing Cows

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10348   Accepted: 3539

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

USACO 2007 December Gold

最优比例环问题,求一个环的 点权和 除以 边权和,使得那个环在所有环中 点权和 除以 边权和 最大

spfa的时候要先push啊,哈哈哈哈哈哈哈哈哈,我蠢得能忘了!

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 const double acc=1e-7;
 8 int n,m,cnt;
 9 double l,r,mid;
10 double f[1010],w[1010];
11 int head[1010],ct[1010];
12 bool check[1010];
13 struct data{
14     int nex,to;
15     double dis;
16 }edge[5010];
17 void add(int start,int end,double d){
18     edge[++cnt].nex=head[start];
19     edge[cnt].to=end;
20     edge[cnt].dis=d;
21     head[start]=cnt;
22 }
23 bool spfa(double x){
24     memset(check,0,sizeof(check));
25     memset(ct,0,sizeof(ct));
26     for(int i=1;i<=n;i++) w[i]=100000000.0;
27     queue<int>q;
28     q.push(1);//!!!!
29     w[1]=0.0;
30     ct[1]=1;
31     check[1]=1;
32     while(!q.empty()){
33         int p=q.front();
34         q.pop();
35         check[p]=0;
36         for(int i=head[p];i;i=edge[i].nex)
37             if(w[edge[i].to]>w[p]+x*edge[i].dis-f[edge[i].to]){
38                 w[edge[i].to]=w[p]+x*edge[i].dis-f[edge[i].to];
39                 if(!check[edge[i].to]){
40                     check[edge[i].to]=1;
41                     q.push(edge[i].to);
42                     ++ct[edge[i].to];
43                     if(ct[edge[i].to]>n) return 1;
44                 }
45             }
46     }
47     return 0;
48 }
49 int main(){
50     scanf("%d%d",&n,&m);
51     for(int i=1;i<=n;i++) scanf("%lf",&f[i]);
52     int a,b;
53     double dd;
54     for(int i=1;i<=m;i++){
55         scanf("%d%d%lf",&a,&b,&dd);
56         add(a,b,dd);
57     }
58     l=0.0;
59     r=10000.0;
60     while(r-l>acc){
61         mid=(l+r)*1.0/2;
62         if(spfa(mid)) l=mid;
63         else r=mid;
64     }
65     printf("%.2f\n",l);
66     return 0;
67 }
时间: 2024-11-05 11:35:57

01分数规划+spfa判负环 POJ3621 Sightseeing Cows的相关文章

POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】

题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11526   Accepted: 3930 Description Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big ci

bzoj1690:[Usaco2007 Dec]奶牛的旅行(分数规划+spfa判负环)

前段时间准备省选没更,后段(?)时间省选考砸没心情更,最近终于开始恢复刷题了... 题目大意:有n个点m条有向边的图,边上有花费,点上有收益,点可以多次经过,但是收益不叠加,边也可以多次经过,但是费用叠加.求一个环使得收益和/花费和最大,输出这个比值. 显然这就是经典的分数规划题啊,就是最优比率环,那么就二分答案,将所有边(u,v)的边权改为[v的点权-(u,v)原边权*mid],这可以算是最优比率环的公式了吧,然后判一下是否有正环,有的话就说明答案可行.判正环有够别扭的,那就全部改成相反数然后

bzoj 1690: [Usaco2007 Dec]奶牛的旅行——分数规划+spfa判负环

Description 作为对奶牛们辛勤工作的回报,Farmer John决定带她们去附近的大城市玩一天.旅行的前夜,奶牛们在兴奋地讨论如何最好地享受这难得的闲暇. 很幸运地,奶牛们找到了一张详细的城市地图,上面标注了城市中所有L(2 <= L <= 1000)座标志性建筑物(建筑物按1..L顺次编号),以及连接这些建筑物的P(2 <= P <= 5000)条道路. 按照计划,那天早上Farmer John会开车将奶牛们送到某个她们指定的建筑物旁边,等奶牛们完成她们的整个旅行并回到

【BZOJ1486】【HNOI2009】最小圈 分数规划 dfs判负环。

链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/46348771"); } 题解: 分数规划Qwq. 然而它卡判点入n次的那种spfa推断负环. 于是有了一种黑科技: 我们从枚举点 i 開始 dfs .然后扫到点 j 时.保持 i~j 这一条链上的点被标记,然后强行推

Poj3259--Wormholes(Spfa 判负环)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36836   Accepted: 13495 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way p

LightOj 1221 - Travel Company(spfa判负环)

1221 - Travel Company PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB A travel company is planning to launch their bus service in a new route. So they conducted a survey and made a list of all possible roads connecting diff

poj3259 Wormholes --- spfa判负环

又写了个bellman模板一直RE求解啊... #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x

uva11090 Going in Cycle!! --- 二分+spfa判负环

给一个带权有向图,求其中是否存在环,若存在,输出环上边权的平均值最小的那个的平均值. 点的范围就50,感觉可以很暴力..但显然超时了 感觉方法好巧妙,二分平均值,将所有边权减去二分的那个值,然后spfa判断是否有负环 若有负环,则图中存在的所有环的边权平均值一定比枚举值大 反之则小,要是无论枚举值多大都没有负环,说明图中没有环. #include <iostream> #include <cstring> #include <string> #include <c

POJ-1860 Currency Exchange 【spfa判负环】

Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the