HDU 4424 and check the collection

Source: Internet
Author: User

Click to open link

Test instructions: Given to n towns, and then to the value of every two towns, but the value of u->v is the minimum value u->v this path, asking for the maximum value from any point to the other position

Idea: Because the constraint is the minimum value on the path, then we sort by value, from the big to the small row, you can avoid this problem, and then use and check set to complete, for two sets, the value of the left set is a, the value of the right set of the sum of B, now there is a path to join these two sets, So what we're thinking about is merging A into B or merging B into a, so we can compare that the path to the two sets is definitely the smallest value at the moment, and if the number of a sets of elements is Ai, then merging a into B will need to add A*ai, in turn a adds B*bi, and the bigger one. Because they must be merged into a piece, the value of a large left

#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm>using namespace std;typedef long Long ll;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const ll inf=0x3f3f3f3f3f3f3f3fll;const int Maxn=200010;int f[maxn];ll nu    m[maxn],r[maxn];struct edge{int u,v; ll cost;} Es[maxn];bool cmp (const edge &a,const edge &b) {return a.cost>b.cost;}    int find1 (int x) {if (x!=f[x]) f[x]=find1 (f[x]); return f[x];}    ll Slove (int n,int k) {for (int i=0;i<=n;i++) num[i]=0;        for (int i=0;i<k;i++) {int u=find1 (ES[I].U);        int V=find1 (ES[I].V);            if (num[u]+r[v]* (es[i].cost) >=num[v]+r[u]* (es[i].cost)) {f[v]=u;num[u]+=r[v]*es[i].cost;        R[U]+=R[V];            }else{F[u]=v;num[v]+=r[u]*es[i].cost;        R[v]+=r[u]; }} return Num[find1 (1)];}    int main () {int n,u,v;    ll cost; while (sCANF ("%d", &n)!=-1) {for (int i=0;i<=n;i++) {f[i]=i;        R[i]=1;        } int k=0;            for (int i=0;i<n-1;i++) {scanf ("%d%d%i64d", &u,&v,&cost);        Es[k].u=u;es[k].v=v;es[k++].cost=cost;        } sort (es,es+k,cmp);        ll Ans=slove (n,k);    printf ("%i64d\n", ans); } return 0;}

HDU 4424 and check the collection

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.