Rush to Wang Du [practices of brute force fraud]

Source: Internet
Author: User

In the midst of a crisis in the kingdom of the libel kingdom, John and John are planning to rush there to block the conspiracy. But before their departure, they had a disagreement, and Tom hoped to take the shortest short circuit to arrive at Wang Du as soon as possible, while John hoped to go another way to collect intelligence. Later, they thought of a compromise. They chose a path to minimize the total distance divided by the number of roads (that is, the mean value of edge weight ).

Enter: a map of the Liber kingdom.

The first behavior is two integers n, m. It indicates a total of N locations, of which 1 is the starting point of John and John, and N is the king capital. M other roads are connected to these locations.

In the next m row, three integers x, y, and z in each row describe a directed path. It indicates that X to Y has a path with a length of Z. Data guarantee, no loops, and at least one path from 1 to n. But there may be multiple roads between the two locations.

Output: there is only one real number, that is, the mean value of the smallest Edge Weight. The answer is left with two decimal places.

Example input: 4 6

1 2 1

2 4 6

1 3 2

3 4 4

2 3 3

1 4 8

Sample output: 2.67

 

Data range: 30% of data 1 <= n <= 20, 1 <= m <= 20

100% of the data is 1 <= n <= 10 ^ 3, 1 <= m <= 20000, and the remaining data is in [^ 3].

 

According to 40 great gods, the score plan is 01. = no weak people> <

However, I learned the violent practices of this question and thought it was still enlightening;

 

Dist [I] [J] indicates from 1 ~ I. Walk through the minimum value of J edge;

Vis [I] [J] and so on;

Spfa again, but since dist is two-dimensional, it also records the number of edges that have elapsed and the number of edges to be transferred, there is also a small difference in writing;

First of all, queue is defined as a node because you have the number of nodes and the number of edges that you walk in the queue.

Similarly, the number of more transfer edges is enough during the relaxation operation;

Specific program:

# Include <cstdio> # include <cstring> # include <algorithm> # include <vector> # include <queue> using namespace STD; const int maxn = 1001, maxm = 20001; int Dist [maxn] [maxm], vis [maxn] [maxm]; int n, m; struct edge {int to, W; edge (INT _ to, int _ w) {to = _ to; W = _ w ;}; vector <edge> G [maxm]; int x, y, z; double Minx = 1234567844.0; struct node {int X, num; node (INT _ x, int _ num) {x = _ x; num = _ num ;}}; queue <node> q; void spfa (int x) {memset (DIS T, 63, sizeof (DIST); Dist [1] [0] = 0; vis [1] [0] = 1; q. push (node (1, 0); While (! Q. empty () {node temp = Q. front (); // pay attention to this; q. pop (); int u = temp. x; vis [u] [temp. num] = 0; int L = G [u]. size (); For (INT I = 0; I <L; I ++) {int v = G [u] [I]. to; If (Dist [v] [temp. num + 1]> Dist [u] [temp. num] + G [u] [I]. w) {Dist [v] [temp. num + 1] = DIST [u] [temp. num] + G [u] [I]. w; If (! Vis [v] [temp. num + 1]) {q. push (node (v, temp. num + 1) ;}}}} int main () {freopen ("troops. in "," r ", stdin); freopen (" troops. out "," W ", stdout); scanf (" % d ", & N, & M); For (INT I = 1; I <= m; I ++) {scanf ("% d", & X, & Y, & Z); G [X]. push_back (edge (y, z);} spfa (1); For (INT I = 1; I <= m; I ++) {double ans = DIST [N] [I]/(I + 0.0); If (ANS <Minx) Minx = ans;} printf ("%. 2f ", Minx); Return 0 ;}

 

Rush to Wang Du [practices of brute force fraud]

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.