Bzoj 2654 Tree binary + minimum spanning trees

Source: Internet
Author: User

Main topic

Give some edges, each with a side right and a color. A spanning tree with a need white edge is now required for the minimum edge. Output this edge right.

Ideas

Add a weight to the white edge so that the white edge can be artificially changed to appear in the smallest spanning tree. This thing obviously can be two points. Then take the minimum value to get it.

CODE
#define _crt_secure_no_warnings#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 100010using namespace STD;structedge{intx, y, length, col;BOOL operator< (ConstEdge &a)Const{if(length = = a.length)returnCol > a.col;returnLength < A.length; }voidRead () {scanf("%d%d%d%d", &x, &y, &length, &col);        ++x, ++y; Col ^=1; }}edge[max];intPoints, edges, need;intFather[max];intFind (intx) {if(Father[x] = = x)returnXreturnFATHER[X] = Find (Father[x]);}inlinepair<int,int> MST () { for(inti =1; I <= points;    ++i) Father[i] = i; pair<int,int> Re (0,0); Sort (Edge +1, Edge + edges +1); for(inti =1; I <= edges; ++i) {intFX = Find (edge[i].x), FY = find (EDGE[I].Y);if(FX! = FY)            {Father[fy] = FX;            Re.second + = Edge[i].col;        Re.first + = Edge[i].length; }    }returnRe;}intMain () {Cin>> points >> edges >> need; for(inti =1; I <= edges; ++i) Edge[i]. Read ();intL =- the, r = the, ans; while(L <= R) {intMid = (L + r) >>1; for(inti =1; I <= edges; ++i) Edge[i].length + = Edge[i].col * MID;intWhite = MST (). Second; for(inti =1; I <= edges; ++i) Edge[i].length-= Edge[i].col * MID;if(White >= need) L = mid +1, ans = mid;ElseR = Mid-1; } for(inti =1; I <= edges; ++i) Edge[i].length + = Edge[i].col * ans;cout<< MST (). First-need * ans << endl;return 0;}

Bzoj 2654 Tree binary + minimum spanning trees

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.