首页 > 代码库 > 01分数规划+spfa判负环 POJ3621 Sightseeing Cows

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 }

 

 

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