51nod 1640 magic of fine weather

Source: Internet
Author: User

The 51nod School of Witchcraft and Wizardry recently launched a magical exchange themed "sunny Weather".
n the magician Press ZHENFA station, then select the N-1 magic chain to connect the magic of all wizards, forming a magical array.
The magic chain is the key to the success of the practice. Each magic chain has a magic value of V, and the final effect of magic depends on the magic value of all magic chains in the array.
As the magic of the inverse day is too violent, we require the magic chain to be as small as possible, while the magic value is as large as possible.
Now given the number of magicians N, the number of magic chains M. To find the maximum effect of this magic array. Input
Two positive integers of n,m. (1 <= n <= 10^5, n <= m <= 2 * 10^5) next M line, each line has three integers a, B, v. (1 <= A, B <= N, int_min <= V <= Int_max) Ensure that the input data is valid.
Output
Outputs a positive integer r that represents the sum of the magic values of the eligible magic array.
Input example
4 61 2 31 3 11 4 72 3 42 4 53 4 6
Output example
12
Wizmann(topic Provider)Idea: Directly first Kruskal find the smallest side of the spanning tree, and then use this minimum value to find all the edges less than equals to this edge, using these edges to do the maximum spanning tree is the answer.
#include <iostream> #include <algorithm> #include <cstdio> #include <queue> #include <map > #include <vector> #include <cstring> #include <cmath>using namespace std;typedef long Long ll;    const int INF =0x3f3f3f3f;const double pi = ACOs ( -1.0); const int N = 3e5 + 10;int root[n];struct node{int x, y; ll W;} P[n];int find (int x) {return x = = Root[x]?x:root[x] = find (Root[x]);} int CMP1 (Node A, Node B) {return A.W&LT;B.W;} int CMP2 (Node A, Node B) {return A.W&GT;B.W;}    int main () {int n, m;    scanf ("%d%d", &n, &m);    for (int i = 0; i<m; i++) {scanf ("%d%d%i64d", &p[i].x, &p[i].y, &AMP;P[I].W);    } for (int i = 0; i<=n; i++) root[i] = i;    Sort (p, p+m, CMP1);    int k, num = 0;    ll last =-INF*100LL;        for (int i = 0; i<m; i++) {int a = find (p[i].x), B = Find (P[I].Y);            if (a!=b) {num++;            Last = Max (last, P[I].W);        Root[a] = b;       } if (num = = n-1) {break;    }} for (int i = 0; i<=n; i++) root[i] = i;            for (int i = 0; i<m; i++) {if (p[i].w>last) {k = i;        Break    }} sort (P, p+k, CMP2);    ll ans = 0;    num = 0;        for (int i = 0; i<k; i++) {int a = find (p[i].x), B = Find (P[I].Y);            if (a!=b) {num++;            Ans + = P[I].W;        Root[a] = b;    } if (num = = n-1) break;    } cout<<ans<<endl; return 0;}


51nod 1640 magic of fine weather

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.