POJ 3621 Sightseeing Cows
Sightseeing Cows
64-bit integer IO format: %lld Java class name: Main
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 1010; 18 struct arc{ 19 int to,w,next; 20 arc(int x = 0,int y = 0,int z = -1){ 21 to = x; 22 w = y; 23 next = z; 24 } 25 }; 26 arc e[100000]; 27 int head[maxn],cnt[maxn],happy[maxn],tot,n,p; 28 double d[maxn]; 29 bool in[maxn]; 30 void add(int u,int v,int w){ 31 e[tot] = arc(v,w,head[u]); 32 head[u] = tot++; 33 } 34 bool spfa(double cur){ 35 queue<int>q; 36 for(int i = 1; i <= n; i++){ 37 d[i] = 0; 38 in[i] = true; 39 cnt[i] = 0; 40 q.push(i); 41 } 42 while(!q.empty()){ 43 int u = q.front(); 44 q.pop(); 45 in[u] = false; 46 for(int i = head[u]; ~i; i = e[i].next){ 47 if(d[e[i].to] > d[u] - happy[e[i].to] + cur*e[i].w){ 48 d[e[i].to] = d[u] - happy[e[i].to] + cur*e[i].w; 49 if(!in[e[i].to]){ 50 q.push(e[i].to); 51 in[e[i].to] = true; 52 if(++cnt[e[i].to] > n) return false; 53 } 54 } 55 } 56 } 57 return true; 58 } 59 int main() { 60 int u,v,w; 61 double high,low,mid; 62 while(~scanf("%d %d",&n,&p)){ 63 for(int i = 1; i <= n; i++) 64 scanf("%d",happy+i); 65 memset(head,-1,sizeof(head)); 66 for(int i = tot = 0; i < p; i++){ 67 scanf("%d %d %d",&u,&v,&w); 68 add(u,v,w); 69 } 70 low = 0; 71 high = 100; 72 double ans = 0; 73 while(fabs(high - low) > 1e-3){ 74 mid = (high+low)/2.0; 75 if(spfa(mid)) high = mid; 76 else low = mid; 77 } 78 printf("%.2f\n",low); 79 } 80 return 0; 81 }
相关文章
- POJ 1679 判断最小树是否唯一
- 【POJ 3071】 Football(DP)
- poj-2996 Help Me with the Game
- POJ 3104 Drying
- POJ 2886 Who Gets the Most Candies?
- POJ 2229 Sumsets
- POJ 2375 Cow Ski Area
- POJ 3422 Kaka's Matrix Travels
- POJ 1144 Network
- POJ 3974 Palindrome
- poj解题报告——poj 2575 Jolly Jumpers
- POJ 2096 Collecting Bugs (概率DP)
- POJ 3172 Scales (01背包暴力)
- POJ 3130 How I Mathematician Wonder What You Are! /POJ 3335 Rotating Scoreboard 初涉半平面交
- Poj 4227 反正切函数的应用
- POJ 3070 Fibonacci
- POJ 2503-Babelfish(map)
- POJ 2481 Cows (线段树)
- poj 3041 Asteroids (最小点覆盖)
- nyoj 349 (poj 1094) (拓扑排序)