Codeforces Gym 2016-2017 Neerc, Moscow subregional K. Knights of the old Republic min spanning tree +DP

Source: Internet
Author: User
Tags min
Note: Part of this article is from the solution of bird God
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <
Iostream> using namespace std;
const int N = 3E6+5;
typedef __int64 LL;


ll A[n], B[n], s[n], fa[n]; struct Edge {int u, V, Val;}

Ed[n];

BOOL CMP (Edge A, Edge b) {return a.val < B.val;}
    int root (int x) {if (fa[x] = = x) return x;
        else{fa[x] = root (Fa[x]);
    return fa[x];
    }} int main () {int n, m, I;
    CIN >> n >> m;
    for (i = 1; I <= n; i + +) fa[i] = i;
    for (i = 1; I <= n; i + +) scanf ("%i64d%i64d", &a[i], &b[i]), s[i] = a[i]*b[i];
    for (i = 1; I <= m; i + +) {scanf ("%d%d%d", &ed[i].u, &AMP;ED[I].V, &ed[i].val);
    } sort (ed+1, ed+m+1, CMP);
        for (i = 1; I <= m; i + +) {int x = root (ed[i].u), y = root (ED[I].V);
            if (x! = y) {ll A = max (a[x], Max (A[y], (LL) ed[i].val)), B = min (b[x], b[y]); A takes three maximum values, B takes two points set within the minimum single cost s[x] = min (s[x]+s[y], a*b);
            A[X] = a;//Note modification, maintenance is the point set internal energy how many free-moving points b[x] = b;
        Fa[y] = x;
    }} ll ans = 0;
for (i = 1; I <= n; i + +) {if (fa[i] = = i) ans + = s[i];//There are different connected blocks} cout << ans << endl; }/* Test instructions: Give n points, each point need to send a big brother, wear a big Brother need B cost, each side on the condition is on both sides of the elder brother number is greater than the side right as long as each point has, notice, have a big brother can, ask the minimum cost idea: Honey juice minimum spanning tree so we can maintain a dp,st, DP[ST] represents the minimum cost of all ST's requirements for the two point sets P and Q if not even the answer for this edge is dp[p]+dp[q] if even, then the two points set within the edge of the edge than the right side of the edge will inevitably be conduction, because the big since all can move freely, small not still very easy ( There is no need to consider the specific edge of the point set in the case) that way, you might as well, from small to large enumeration edge, because so that two points in the set of points will inevitably guide the general Krukal implementation of such a process, make a minimum spanning tree so write may have a lot of places feel not necessarily right, but carefully observe the merger process,

 Most of the situation is considered to be a bit like the diode conduction AH ~ * *

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.