Sightseeing Cows(最优比率环)

Sightseeing Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8915   Accepted: 3000

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
题解:
题意就是,这个人带牛旅行,旅行每个城市会有幸福度,通过每个城市会花费时间,让找平均每秒的最大幸福度;
最短路01分数规划,也就是最优比率环问题;刚开始用了Bellman各种超时,换了SPFA也是,最后把INF改大了,SPFA过了,最后想想BELLMAN的时间复杂度N*M又改了种写法,就各种wa了,真心疲惫;
SPFA:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=10000000000000;
typedef long long LL;
const int MAXN=1010;
const int MAXM=100010;
/*struct Node{
	int u,v;
	double t;
}; 
Node dt[MAXM];*/
struct Edge{
	int from,to,next,t;
};
Edge edg[MAXM];
int head[MAXM];
int edgnum;
void add(int u,int v,int t){
	Edge E={u,v,head[u],t};
	edg[edgnum]=E;
	head[u]=edgnum++;
}
int L,P;
double hp[MAXN],dis[MAXN];
int usd[MAXN],vis[MAXN];
/*void add(int u,int v,double t){
	Node E={u,v,t};
	dt[edgnum++]=E;
}*/
//double R;
/*bool Bellman(){
	mem(dis,INF);
	mem(usd,0);
	dis[1]=0;
	while(1){
		int temp=0;
		for(int j=0;j<edgnum;j++){
			int u=dt[j].u,v=dt[j].v;
			double t=dt[j].t;
			//dis[v]=min(dis[v],dis[u]+R*t-hp[u]);//应该是R*t-hp[u]; 
			if(dis[v]>dis[u]+R*t-hp[u])usd[v]++,dis[v]=dis[u]+R*t-hp[u],temp=1;
			if(usd[v]>L)return false;
		}
		if(!temp)return true;
	}
}*/
bool SPFA(double R){
	queue<int>dl;
	while(!dl.empty())dl.pop();
	for(int i=1;i<=L;i++){
        dis[i]=INF;
        vis[i]=0;
        usd[i]=0;
    }
	dl.push(1);
	vis[1]=1;
	usd[1]++;
	dis[1]=0;
	while(!dl.empty()){
		int u=dl.front();
		dl.pop();
		vis[u]=0;
		for(int i=head[u];i!=-1;i=edg[i].next){
			int v=edg[i].to,t=edg[i].t;
			if(dis[v]>dis[u]+R*t-hp[u]){
				dis[v]=dis[u]+R*t-hp[u];
				if(!vis[v]){
					vis[v]=1;
					usd[v]++;
					dl.push(v);
				//	printf("%d\n",usd[v]);
					if(usd[v]>=L)return false;
				}
			}
		}
	}
	return true;
}
int main(){
	int a,b;
	int c;
	while(~scanf("%d%d",&L,&P)){
		edgnum=0;
		double mih=INF,mxh=-INF;
		int mit=INF,mxt=-INF;
		mem(head,-1);
		for(int i=1;i<=L;i++){
			scanf("%lf",hp+i);
			mih=min(mih,hp[i]);
			mxh=max(mxh,hp[i]);
		}
		while(P--){
			scanf("%d%d%d",&a,&b,&c);
			add(a,b,c);
			mit=min(mit,c);
			mxt=max(mxt,c);
		}
		double l=mih/mxt,r=mxh/mit;
	//	printf("%f %f\n",l,r);
		double R;
		while(r-l>=0.001){
			R=(l+r)/2;
			if(SPFA(R))r=R;
			else l=R;
		}
		printf("%.2f\n",l);
	}
	return 0;
}

  

  Bellman还在wa,贴上求大神帮忙看看;

wa代码:

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<algorithm>
  6 #include<queue>
  7 #define mem(x,y) memset(x,y,sizeof(x))
  8 using namespace std;
  9 const int INF=10000000000000;
 10 typedef long long LL;
 11 const int MAXN=1010;
 12 const int MAXM=100010;
 13 struct Node{
 14     int u,v;
 15     int t;
 16 };
 17 Node dt[MAXM];
 18 /*struct Edge{
 19     int from,to,next,t;
 20 };
 21 Edge edg[MAXM];
 22 int head[MAXM];*/
 23 int edgnum;
 24 /*void add(int u,int v,int t){
 25     Edge E={u,v,head[u],t};
 26     edg[edgnum]=E;
 27     head[u]=edgnum++;
 28 }*/
 29 int L,P;
 30 double hp[MAXN],dis[MAXN];
 31 int usd[MAXN],vis[MAXN];
 32 void add(int u,int v,int t){
 33     Node E={u,v,t};
 34     dt[edgnum++]=E;
 35 }
 36 //double R;
 37 bool Bellman(double R){
 38     for(int i=1;i<=L;i++){
 39         usd[i]=0;
 40         dis[i]=INF;
 41     }
 42     dis[1]=0;
 43     usd[1]++;
 44     while(1){
 45         int temp=0;
 46         for(int j=0;j<edgnum;j++){
 47             int u=dt[j].u,v=dt[j].v;
 48             int t=dt[j].t;
 49             //dis[v]=min(dis[v],dis[u]+R*t-hp[u]);//应该是R*t-hp[u]; 
 50             if(dis[v]>dis[u]+R*t-hp[u])dis[v]=dis[u]+R*t-hp[u],temp=1,usd[v]++;
 51             if(usd[v]>=L)return false;
 52         }
 53         if(!temp)return true;
 54     }
 55     /*for(int j=0;j<edgnum;j++){
 56         int u=dt[j].u,v=dt[j].v;
 57             int t=dt[j].t;
 58         if(dis[v]>dis[u]+R*t-hp[u])return false;
 59     }
 60     return true;*/
 61 }
 62 /*bool SPFA(double R){
 63     queue<int>dl;
 64     while(!dl.empty())dl.pop();
 65     for(int i=1;i<=L;i++){
 66         dis[i]=INF;
 67         vis[i]=0;
 68         usd[i]=0;
 69     }
 70     dl.push(1);
 71     vis[1]=1;
 72     usd[1]++;
 73     dis[1]=0;
 74     while(!dl.empty()){
 75         int u=dl.front();
 76         dl.pop();
 77         vis[u]=0;
 78         for(int i=head[u];i!=-1;i=edg[i].next){
 79             int v=edg[i].to,t=edg[i].t;
 80             if(dis[v]>dis[u]+R*t-hp[u]){
 81                 dis[v]=dis[u]+R*t-hp[u];
 82                 if(!vis[v]){
 83                     vis[v]=1;
 84                     usd[v]++;
 85                     dl.push(v);
 86                 //    printf("%d\n",usd[v]);
 87                     if(usd[v]>=L)return false;
 88                 }
 89             }
 90         }
 91     }
 92     return true;
 93 }*/
 94 int main(){
 95     int a,b;
 96     int c;
 97     while(~scanf("%d%d",&L,&P)){
 98         edgnum=0;
 99     //    double mih=INF,mxh=-INF,mit=INF,mxt=-INF;
100         //mem(head,-1);
101         for(int i=1;i<=L;i++){
102             scanf("%lf",hp+i);
103         //    mih=min(mih,hp[i]);
104         //    mxh=max(mxh,hp[i]);
105         }
106         while(P--){
107             scanf("%d%d%d",&a,&b,&c);
108             add(a,b,c);
109             //mit=min(mit,c);
110         //    mxt=max(mxt,c);
111         }
112         double l=0,r=10000;
113     //    printf("%f %f\n",l,r);
114         double ans=0;
115         double R;
116         while(r-l>=0.001){
117             R=(l+r)/2;
118             if(Bellman(R))r=R;
119             else ans=R,l=R;
120         }
121         printf("%.2f\n",ans);
122     }
123     return 0;
124 }

 

posted @ 2015-11-17 22:09  handsomecui  阅读(900)  评论(0编辑  收藏  举报