列表

详情


NC25032. [USACO 2007 Dec G]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 L1_i to L2_i (in the direction L1_i -> L2_i ) 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.

输入描述

* 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: L1_i , L2_i , and Ti

输出描述

* 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.

示例1

输入:

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

输出:

6.00

说明:

The trip 1 -> 2 -> 3 -> 5 -> 1 has a total fun value of 60 and a length of 10 units for an average fun per unit time of 6. The trip 2 -> 3 -> 5 -> 2 only has an average fun per unit time of 30/6 = 5, and any trip involving landmark 4 has an average fun per unit time of less than 4.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 44ms, 内存消耗: 560K, 提交时间: 2019-07-14 10:46:57

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct edge{
	int to,w,nex;
}ed[5005];
int n,m,a[1005],u,v,w,head[1005],cnt,in[1005],mark[1005];
double dis[1005];
void add(int u,int v,int w){
	ed[++cnt].to=v;
	ed[cnt].w=w;
	ed[cnt].nex=head[u];
	head[u]=cnt;
}
queue<int>q;
bool SPFA(double x){
	while(!q.empty())q.pop();
	for(int i=1;i<=n;i++){
		mark[i]=in[i]=1,dis[i]=0;
		q.push(i);
	}
	while(!q.empty()){
		int t=q.front();
		q.pop();
		mark[t]=0;
		for(int i=head[t];i;i=ed[i].nex){
			int tp=ed[i].to;
			double w=a[t]-x*ed[i].w;
			if(dis[tp]<dis[t]+w){
				dis[tp]=dis[t]+w;
				if(++in[tp]>n)return true;
				if(mark[tp])continue;
				q.push(tp);
				mark[tp]=1;
			}
		}
	}
	return false;
}
int main(){
	while(~scanf("%d%d",&n,&m)){
		memset(head,0,sizeof(head));
		cnt=0;
		for(int i=1;i<=n;i++)scanf("%d",&a[i]);
		for(int i=1;i<=m;i++){
			scanf("%d%d%d",&u,&v,&w);
			add(u,v,w);
		}
		double l=0,r=1000,mid;
		while(r-l>1e-4){
			mid=(l+r)/2;
			if(SPFA(mid))l=mid;
			else r=mid;
		}
		printf("%.2f\n",mid);
	}
    return 0;
}

C++(clang++11) 解法, 执行用时: 124ms, 内存消耗: 504K, 提交时间: 2020-11-01 09:33:17

#include<cstdio>
#include<algorithm>
#include<queue>
#define db double
#define rep(i,x,y) for( int i=x;i<=y;i++)
using namespace std; 
const int N=1005; 
struct node{
	int y,z,next; 
}a[N*10];
int tot,head[N]; 
int n,m,b[N]; 
void add(int x,int y,int z){
	a[++tot]=(node){y,z,head[x]}; head[x]=tot; 
}
bool v[N]; db d[N]; int w[N]; 
bool check(db k){
	queue<int>q; 
	rep(i,1,n) q.push(i),d[i]=0,w[i]=1,v[i]=1; 
	while (q.size()){
		int x=q.front(); q.pop(); v[x]=0;
		for(int i=head[x];i;i=a[i].next){
			int y=a[i].y; db dis=(db)a[i].z; 
			if (d[y]>d[x]+k*dis-(db)b[x]){
				d[y]=d[x]+k*dis-(db)b[x]; 
				if (!v[y]){
					q.push(y),v[y]=1; 
					if (++w[y]>=n) return 1; 
				} 
			}
		}
	}
	return 0; 
}
int main(){
	scanf("%d%d",&n,&m); 
	rep(i,1,n) scanf("%d",&b[i]); 
	rep(i,1,m){
		int x,y,z; 
		scanf("%d%d%d",&x,&y,&z); 
		add(x,y,z); 
	}
	db l=0,r=1000010,mid; 
	while (r-l>1e-4){
		mid=(l+r)/2; 
		if (check(mid)) l=mid; else r=mid; 
	}
	printf("%.2lf",l); 
	return 0; 
}

上一题