poj 3621 Sightseeing Cows

Sightseeing Cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5767 Accepted: 1901

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

// 题意是在一个有向图中找一条回路,使得
//maxmum A = ∑F/∑T
//这显然是一个分数规划问题
//图中的边权全部为正,而F只有在第一次到达节点时才会增加,所以可以肯定最优解一定是圈,而不是闭链,根据圈的性质|V|=|E|
//所以可以把点F的值对应到由该点出发的边上
//这就又可以简化成为一个01分数规划问题 maxmum A= ∑F(i)/∑T(i) (xi == 1) x为其解向量
//分数规划分析详见(最小割模型在信息学竞赛中的应用 胡伯涛)
//F表示∑F(i),T表示∑T(i)
//设g(A) = min(A*T-F)
//证明其单调性 设 A1 > A2 并设 i1为使得g(A) = min(A*T-F) 的解向量
// g(A1) = A1*T(i1)-F(i1) > A2*T(i1)-F(i1) >= min(A2*T-F) = g(A2)
//所g(A)严格单调递增
//并且有g(A) = 0时规划取得最优解(证明见 最小割模型在信息学竞赛中的应用 胡伯涛)
//所以可以二分答案,判断g(A)的正负,由于起点的任意性所以用bellman_ford判断是否有负权圈即可
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits>
#include <set>

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;

const int MAXN(1010);
const int MAXE(5010);
const int INFI((INT_MAX-1) >> 1);
const double EPS(1e-7);


int L, P;
int F[MAXN];
int first[MAXN];
int T[MAXE], u[MAXE], v[MAXE], next[MAXE];
double w[MAXE];
int rear;

void init()
{
	memset(first+1, -1, sizeof(first[0])*L);
	rear = 0;
}

void insert(int tu, int tv, int tw)
{
	u[rear] = tu;
	v[rear] = tv;
	T[rear] = tw;
	next[rear] = first[tu];
	first[tu] = rear++;
}

double dist[MAXN];

bool bf()
{
	memset(dist+1, 0, sizeof(dist[0])*L);
	for(int i = 1; i < L; ++i)
	{
		bool flag(true);
		for(int j = 0; j < rear; ++j)
			if(dist[u[j]]+w[j] < dist[v[j]])
			{
				dist[v[j]] = dist[u[j]]+w[j];
				flag = false;
			}
		if(flag)
			return false;
	}
	for(int j = 0; j < rear; ++j)
		if(dist[u[j]]+w[j] < dist[v[j]])
			return true;
	return false;
}

int main()
{
	while(~scanf("%d%d", &L, &P))
	{
		double l = 0;
		double r = 0;
		for(int i = 1; i <= L; ++i)
		{
			scanf("%d", F+i);
			r += F[i];
		}
		int tu, tv, tw;
		int tmin = INFI;
		for(int j = 0; j < P; ++j)
		{
			scanf("%d%d%d", &tu, &tv, &tw);
			insert(tu, tv, tw);
			tmin = min(tmin, tw);
		}
		r /= tmin;
		double temp = r;
		while(r-l > EPS)
		{
			double m = (l+r)/2.;
			for(int i = 0; i < rear; ++i)
				w[i] =  m*T[i]-F[u[i]];     //把点和边对应构造A*T-F
			if(bf())                        
			{
				l = m;	
			}
			else
			{
				r = m;
			}
		}
		if(l == 0.)
			printf("0\n");
		else
			printf("%.2lf\n", l);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值