"POJ 3621" sightseeing cows

Source: Internet
Author: User

Sightseeing cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7984 Accepted: 2685

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

01 Fractional planning.


The optimal proportional ring is obtained.


When you encounter Sigma (AI)/sigma (BI), think of the 01 score plan.


Set the positive solution to ans.


Two, one answer, K, if K is bigger than ans.


It indicates that the K value of two points should be reduced when there is no negative ring in the graph;

The k value should be increased when there is a negative ring.


The method of judging negative ring is consistent with "Bzoj 1486".


#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include < cstdlib> #include <cmath> #define EPS 1e-4using namespace std;double d[1005];bool f;int tot=0,v[1005],a[1005],h [1005],n,m;struct edge{int Y,ne,v;} E[5005];void addedge (int x,int y,int v) {Tot++;e[tot].y=y;e[tot].v=v;e[tot].ne=h[x];h[x]=tot;} void Dfs (int x,double k) {v[x]=1;for (int i=h[x];i;i=e[i].ne) {int y=e[i].y;if (d[y]>d[x]-(double) a[y]+k* (double) e[i ].V) {if (V[y]) {F=true;return;} d[y]=d[x]-(Double) a[y]+k* (double) E[i].v;dfs (y,k), if (f) return;}} v[x]=0;} BOOL Youfh (double k) {f=false;for (int i=1;i<=n;i++) d[i]=0.0,v[i]=0;for (int i=1;i<=n;i++) {DFS (i,k); if (f) return true;} return false;} void Solve () {double L=0.0,r=2000.0;while (r-l>eps) {double m= (l+r)/(double) 2;if (YOUFH (m)) L=m;else r=m;} printf ("%.2lf\n", L);} int main () {scanf ("%d%d", &n,&m), for (Int. i=1;i<=n;i++) scanf ("%d", &a[i]); for (int. i=1;i<=m;i++) {in T x,y,v;scanf ("%d%d%d", &x,&amP;Y,&AMP;V); Addedge (x,y,v);} Solve (); return 0;}




Sentiment:

1.c++, ac,g++, WA.


2. The negative ring method for DFS judgment is worth learning


3. For the 01 fractional programming problem, there are two methods: dichotomy and Dinkelbach algorithm.

The latter is arbitrarily given a k, according to the value obtained by K to move K, so that he approximation of the optimal solution.


Reference:

Blog: 01 Score Planning

Baidu Library: 01 Score Planning

"POJ 3621" sightseeing cows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.