Analysis: This question is to find out how much money Jun can earn me!
In this question, all the paths constructed by Jun Ye are the shortest, And then he needs to find the maxcost with the largest paths in these Shortest Paths!
Because it is the shortest path, the amount of money I need to pay is: maxcost * (n-1); the actual amount of money spent is actually the sum of the money spent on each path!
How can we find the cost of each path? The Minimum Spanning Tree is used! When creating the minimal spanning tree, you can find the maximum maxcost!
Then the rest is easy to do. It's okay to simply perform a subtraction!
The Code is as follows:
# Include <cstdio> # include <iostream> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 5201314; int P [maxn], CNT, MAS; struct Qiu {int F, T, mon; bool operator <(const Qiu & A) const {return mon <. mon ;}} road [maxn]; int find (int x) {return P [x] =-1? X: P [x] = find (P [x]);} int ans () {int sum = 0; For (INT I = 0; I <CNT; I ++) {int x = find (road [I]. f); int y = find (road [I]. t); If (X! = Y) {sum + = road [I]. mon; P [x] = y; If (MAS <road [I]. mon) Mas = road [I]. mon; // obtain the value of the maximum node in the minimal spanning tree!} Return sum;} int main () {int n, m; while (~ Scanf ("% d", & N, & M) {memset (p,-1, sizeof (p); // initialize the root node! CNT = mas = 0; // Number of initialized nodes and maximum price! While (M --) {int F, T, C; scanf ("% d", & F, & T, & C); road [CNT]. F = f; road [CNT]. T = T; road [CNT ++]. MON = C ;}sort (road, road + CNT); int sum = ans (); // find the minimum cost! // Cout <"the cost should be:" <sum <";" <"maximum cost:" <mas <Endl; cout <Mas * (n-1)-sum <Endl; // how much can Jun earn me ?? 0.0} return 0 ;}
Description
Junye recently invested in a local area network company, which operates not only on the Earth, but also on outer spaces. Because dB planet has nothing but money, dbz, the leader, asks Jun ye to help build a LAN for their planet. Jun ye certainly wants to win the deal. We know that A is connected to B and B is connected to C, so A and C are also connected, dbz now requires that all cities on the DB planet be connected.
Jun ye was a little sad recently. He wanted to take this opportunity to earn more money. He knew that the savvy dbz had learned how to set up the line and the cost was the smallest. However, dbz gave Jun ye a chance to pay for each path as the maximum value of all paths constructed by Jun ye. Now, Jun wants to know how much he can earn dbz? (Of course, if Jun ye deceives dbz, the consequences will be very serious, and dbz will look for other companies to set up ).
Jun ye needs 1 DBB (currency of the DBB planet) to set up a line per unit length)
Input
The test data contains multiple groups.
Each group of test data contains several rows. The first row contains N (1 <= n <= 100000) and M (1 <= m <= 1000000 ). Represents n cities and M paths on the DB planet.
Each row of the next m row contains three integers: U, V (1 <= u, v <= N), C (1 <= C <= 1000 000 000) represents a path from City U to city V with a distance of C.
Output
The output contains only one number, which is the amount of money that Jun can earn.
Sample input3 3 1 2 31 2 52 3 5 sample output2
Problem I: Jun Ye's LAN