HDU 4313 Matrix (greedy + and collection)

Source: Internet
Author: User

HDU 4313

Test instructions

There are n nodes, n-1, where k nodes are dangerous nodes, there are weapons of mass destruction, which roads can be cut off so that the dangerous nodes of these weapons of mass destruction are not connected to each other, and the Benquan value of the cut is minimized.

Ideas:

Initializes each node to a collection and records the number of dangerous nodes (0 or 1) in each collection.

To realize the sum of the weights as small as possible, the weights are as small as possible, so the N-1 bar edge is first sorted in ascending order of weights.

Enumerate these edges after sorting:

If there are weapons of mass destruction in the set of both ends of the edge, delete it and accumulate its weights.

If there is only one side, merge the two collections (using the and check set), merging the dangerous nodes to the parent node where possible.

If not, merge the two collections.

The end of the enumeration, the value of the output weights can be accumulated.

Ps. Very good and look at the set of detailed: http://blog.csdn.net/dellaserss/article/details/7724401


Code

/** @author novicer* language:c++/c*/#include <iostream> #include <sstream> #include <fstream># include<vector> #include <list> #include <deque> #include <queue> #include <stack># include<map> #include <set> #include <bitset> #include <algorithm> #include <cstdio># include<cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime># Include<iomanip> #define INF 2147483647#define CLS (x) memset (x,0,sizeof (x)) #define RISE (I,A,B) for (int i = A; I < = b; i++) using namespace Std;const double eps (1e-8); typedef long LONG lint;const int MAXN = 100000 + 5;struct edge{int u, V, C;} Rd[maxn];int n,k;int flag[maxn];int Father[maxn];bool cmp (Edge A, Edge b) {return A.C > B.C;} void Init () {CLS (flag); for (int i = 0; i < MAXN; i++) {father[i] = i;}} int find (int x) {if (x = Father[x]) {father[x] = find (Father[x]);} return father[x];} void Mix (int x, int y) {father[x] = y;} void input () {init (); CIN &Gt;> n >> k;for (int i = 0; i < n-1; i++) {scanf ("%d%d%d", &rd[i].u,&rd[i].v,&rd[i].c);} for (int i = 0; i < K; i++) {int tmp;scanf ("%d", &tmp); flag[tmp] = 1;}} void Solve () {Sort (rd, rd+n-1, CMP); lint ans = 0; for (int i = 0; i < n-1; i++) {if (Flag[find (RD[I].U)] && FLA G[find (RD[I].V)]) ans + = Rd[i].c;else if (Flag[find (RD[I].U)]) Mix (Find (RD[I].V), find (RD[I].U)), Elsemix (Find (rd[i].u ), find (RD[I].V));} cout << ans << endl;} int main () {int t; cin >> T;while (t--) {input (); Solve ();} return 0;}


Copyright notice: Bo Master said authorized all reproduced:)

HDU 4313 Matrix (greedy + and 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.