POJ3621 Sightseeing Cows 最優比率環 二分法
題目鏈接:http://poj.org/problem?id=3621
Sightseeing Cows
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10552 | Accepted: 3613 |
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 L
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
USACO 2007 December Gold 題解:代碼如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 2e18; 18 const int MAXN = 5e3+10; 19 20 int n, m, val[MAXN]; 21 struct edge 22 { 23 int to, w, next; 24 }edge[MAXN]; 25 int cnt, head[MAXN]; 26 27 void add(int u, int v, int w) 28 { 29 edge[cnt].to = v; 30 edge[cnt].w = w; 31 edge[cnt].next = head[u]; 32 head[u] = cnt++; 33 } 34 35 double dis[MAXN]; 36 int times[MAXN], inq[MAXN], vis[MAXN]; 37 bool spfa(double L) 38 { 39 memset(vis, 0, sizeof(vis)); 40 memset(inq, 0, sizeof(inq)); 41 memset(times, 0, sizeof(times)); 42 for(int i = 1; i<=n; i++) 43 dis[i] = INF; 44 45 queue<int>Q; 46 Q.push(1); 47 inq[1] = 1; 48 vis[1] = 1; 49 dis[1] = 0; 50 while(!Q.empty()) 51 { 52 int u = Q.front(); 53 Q.pop(); inq[u] = 0; 54 for(int i = head[u]; i!=-1; i = edge[i].next) 55 { 56 int v = edge[i].to; 57 // 距離全部取反, 看是否存在正環 58 if(dis[v]> dis[u]-(val[u]-edge[i].w*L) ) 59 { 60 dis[v] = dis[u]-(val[u]-edge[i].w*L); 61 if(!inq[v]) 62 { 63 Q.push(v); 64 inq[v] = 1; 65 vis[v] = 1; 66 if(++times[v]>n) return true; //檢測到了負環,但因為數值全部取反了,所以實際上為檢測到了正環 67 } 68 } 69 } 70 } 71 return false; 72 } 73 74 void init() 75 { 76 cnt = 0; 77 memset(head, -1, sizeof(head)); 78 } 79 80 int main() 81 { 82 while(scanf("%d%d", &n, &m)!=EOF) 83 { 84 for(int i = 1; i<=n; i++) 85 scanf("%d", &val[i]); 86 87 init(); 88 for(int i = 1; i<=m; i++) 89 { 90 int u, v, w; 91 scanf("%d%d%d",&u, &v, &w); 92 add(u,v,w); 93 } 94 95 double l = 0, r = 1e3; 96 while(l+EPS<=r) 97 { 98 double mid = (l+r)/2; 99 if(spfa(mid)) //存在正環,繼續增大比率 100 l = mid + EPS; 101 else 102 r = mid - EPS; 103 } 104 printf("%.2f\n", r); 105 } 106 }View Code
POJ3621 Sightseeing Cows 最優比率環 二分法