博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ3621 Sightseeing Cows 最优比率环 二分法
阅读量:5047 次
发布时间:2019-06-12

本文共 4348 字,大约阅读时间需要 14 分钟。

题目链接:

 

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 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 73010105101 2 32 3 23 4 53 5 24 5 55 1 35 2 2

Sample Output

6.00

Source

 
 
 
题解:
 
 

代码如下:

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
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
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 else102 r = mid - EPS;103 }104 printf("%.2f\n", r);105 }106 }
View Code

 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/7581161.html

你可能感兴趣的文章
Web框架和Django基础
查看>>
python中的逻辑操作符
查看>>
CSS兼容性常见问题总结
查看>>
HDU 1548 A strange lift (Dijkstra)
查看>>
每天一个小程序—0005题(批量处理图片大小)
查看>>
IIS的各种身份验证详细测试
查看>>
JavaScript特效源码(3、菜单特效)
查看>>
Linux常用命令总结
查看>>
yii模型ar中备忘
查看>>
C#线程入门
查看>>
CSS清除浮动方法
查看>>
JVM内存回收机制简述
查看>>
洛咕 P2480 [SDOI2010]古代猪文
查看>>
js-创建对象的几种方式
查看>>
JDK JRE Java虚拟机的关系
查看>>
2018.11.20
查看>>
word20161215
查看>>
12th week blog
查看>>
dijkstra (模板)
查看>>
python小记(3)
查看>>