Sightseeing cows
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 8220 |
|
Accepted: 2757 |
Description
Farmer John had decided to reward he 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 has a detailed city map showing the l (2≤ L ≤1000) Major LAN Dmarks (conveniently numbered 1.. L ) and the P (2≤ P ≤5000) Unidirectional cow paths that join them. Farmer John would drive the cows to a starting landmark of their choice, from which they would walk along the cow paths to a Series of other landmarks, ending back at their starting landmark where Farmer John would pick them up and take them back to the farm. Because space in the city was at a premium, the cow paths was very narrow and so travel along each cow path was only allowed In one fixed direction.
While the cows is spend as much time as they like in the city, they does tend to get bored easily. Visiting each new landmark are 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 has the best possible day off, the cows want to maximize the average fun value per unit time of their trips. Of course, the landmarks is only fun the first time they is visited; The cows may pass through the landmark more than once, but they does not perceive its fun value again. Furthermore, Farmer John is making the cows visit at least-landmarks, so-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 the decimal places (does not perform explicit rounding), the maximum possible average fun per unit time, or 0 if the cows cannot plan any of the 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
Usaco December Gold
The main idea is to find a ring so that the sum of vertex weights and the sum of Benquan is the largest ratio
First of all, it is important to note that the topic requirements can start from any point, the online many problem-solving report default from 1 o'clock, although after this problem, but obviously not quite right
Since the title is Max, then after the edge deformation, use SPFA to find the longest road, see if there is a positive ring, and then based on the two-point search.
AC Code
#include <stdio.h> #include <string.h> #include <queue> #include <iostream> #define EPS 1e-6# Define INF 0xfffffffusing namespace Std;int head[1010],cnt,n,m,vis[1010],c[1010];struct s{int u,v,w,next;} EDGE[100100];d ouble val[1010],d[1010];void Add (int u,int v,int W) {edge[cnt].u=u;edge[cnt].v=v;edge[cnt].w=w;edge[ cnt].next=head[u];head[u]=cnt++;} int SPFA (double mid) {int i;queue<int>q;for (i=1;i<=n;i++) {Vis[i]=c[i]=1;q.push (i);d [i]=0;} while (!q.empty ()) {int U=q.front (); Q.pop (); Vis[u]=0;for (i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v;double w= Val[u]-mid*edge[i].w;if (d[v]<d[u]+w) {d[v]=d[u]+w;if (!vis[v]) {Q.push (v); Vis[v]=1;c[v]++;if (c[v]>n) return 1 ;}}}} return 0;} int main () {//int n,m;while (scanf ("%d%d", &n,&m)!=eof) {int I;cnt=0;memset (head,-1,sizeof (head)); for (i=1;i <=n;i++) {scanf ("%lf", &val[i]);} for (i=0;i<m;i++) {int u,v,w;scanf ("%d%d%d", &u,&v,&w); add (u,v,w);} Double Low=0,high=1000;while (high-low>eps) {double mid= (high+low)/2;if (sPFA (mid)) Low=mid;elsehigh=mid;} printf ("%.2lf\n", Low);}}
POJ topic 3621 Sightseeing Cows (SPFA)