poj 3621(最优比率环)

                    Sightseeing Cows

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
题意:求解一个图上的环上点权和除边权和最大值
sigma(val[i])/sigma(w[i])最大
和求最优比率生成树类似 sigma(val[i])/sigma(w[i])>=x;
-->sigma(val[i]-w[i]*x)>=0;
由于题目说的是图上的环 我们可以转化成图上的负权环 用spfa求解
如何转换负权环,我们将u-v边的边权变为x*edge[i].w-val[u];
所以在二分的时候 跑spfa判断是否是负环 是的话则l=mid;否则r=mid;
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstdlib>
  4 #include<cctype>
  5 #include<cmath>
  6 #include<cstring>
  7 #include<map>
  8 #include<queue>
  9 #include<stack>
 10 #include<set>
 11 #include<vector>
 12 #include<algorithm>
 13 #include<string.h>
 14 typedef long long ll;
 15 typedef unsigned long long LL;
 16 using namespace std;
 17 const int INF=0x3f3f3f3f;
 18 const double eps=0.000001;
 19 const int N=10000+10;
 20 const ll mod=1e9+7;
 21 int head[N];
 22 int tot;
 23 struct node{
 24     int to,next;
 25     int w;
 26 }edge[N<<1];
 27 int vis[N];
 28 double dis[N];
 29 int val[N];
 30 int num[N];
 31 void init(){
 32     memset(head,-1,sizeof(head));
 33     tot=0;
 34 }
 35 void add(int u,int v,int w){
 36     edge[tot].to=v;
 37     edge[tot].next=head[u];
 38     edge[tot].w=w;
 39     head[u]=tot++;
 40 }
 41 int n,m;
 42 int spfa(double x){
 43     memset(vis,0,sizeof(vis));
 44     memset(num,0,sizeof(num));
 45     for(int i = 1;i<=n;i++)dis[i]=1e15;
 46     queue<int>q;
 47     q.push(1);
 48     dis[1]=0;
 49     num[1]++;
 50     vis[1]=1;
 51     while(q.empty()==0){
 52         int u=q.front();
 53         q.pop();
 54         vis[u]=0;
 55         for(int i=head[u];i!=-1;i=edge[i].next){
 56             int v=edge[i].to;
 57             int w=edge[i].w;
 58             if(dis[u]+w*x-val[u]<dis[v]){
 59                 dis[v]=dis[u]+x*w-val[u];
 60                 if(vis[v]==0){
 61                     q.push(v);
 62                     vis[v]=1;
 63                     num[v]++;
 64                     if(num[v]>n)return 1;//存在负权环
 65                 }
 66             }
 67         }
 68     }
 69     return 0;
 70 }
 71 int main(){
 72     init();
 73     scanf("%d%d",&n,&m);
 74     for(int i=1;i<=n;i++)scanf("%d",&val[i]);
 75     for(int i=1;i<=m;i++){
 76         int u,v,w;
 77         scanf("%d%d%d",&u,&v,&w);
 78         add(u,v,w);
 79         //add(v,u,w);
 80     }
 81     double low=0.0;
 82     double high=10000;
 83     double ans=0;
 84     while(low+eps<high){
 85         double mid=(low+high)/2.0;
 86         if(spfa(mid)){
 87             ans=mid;
 88             low=mid;
 89         }else{
 90             high=mid;
 91         }
 92     }
 93     printf("%.2f\n",ans);
 94 }
 95 /*
 96 5 7
 97 30
 98 10
 99 10
100 5
101 10
102 1 2 3
103 2 3 2
104 3 4 5
105 3 5 2
106 4 5 5
107 5 1 3
108 5 2 2
109 */

 

 

 

posted on 2018-06-01 15:23  见字如面  阅读(173)  评论(1编辑  收藏  举报

导航