Graph theory--spanning tree

Source: Internet
Author: User

TYVJ 3737

Describe

The Ping Jin battlefield of the three major battles, Fu Zuoyi Group in Peiping, Tianjin as the center, east from Tangshan West to Zhangjiakou railway line on the screwdriver Higo, and attempt to rout in the south from the sea or fled to the west. In order to local annihilate not let it escape, Mzd developed a first cut off the enemy east Sprinkle two escape route and then destroy the enemy's strategic approach.

Adhering to the strategic thinking of the great strategist, you, as a wise commander, have encountered a similar battlefield situation:

There are now N cities, of which K is occupied by the enemy Corps, N cities are connected by the N-1 Highway, the cost of destroying one of the roads is known, now, to tell you the city of the K Enemy Corps, and all the cost of the destruction of the road, please calculate the cost of the K-Regional Corps to isolate each other, So that the second step is to break the enemy.

Input format

The first line consists of two positive integers n and K.

The second line contains k integers that indicate which city the enemy is occupying.

Next n-1 line, each line contains three positive integer a,b,c, indicating a highway from city A to City B, and the cost of destruction C.

The number of the city counts starting from 0.

which

2<=n<=100000

2<=k<=n

1<=c<=1000000

Output format

Contains an integer that represents the cost of the least cost.

Test Sample 1 input
3 3
0 1 2
0 1 1
1 2 2
Output
3
Test Sample 2 Input
5 3
1 2 4
1 0 4
1 3 8
2 1 1
2 4 3
Output
4
Idea: The least amount of damage = left up to the maximum spanning tree, so that at most one enemy station is contained within the code:
1#include <iostream>2#include <algorithm>3 #defineMX 1000054 using namespacestd;5 structedge{6        intu;7        intv;8        Long LongW;9 };Ten intN,K,J[MX],VIS[MX],PRE[MX],FA,FB; One Edge g[mx],tmp; A intFINDF (intx) { -      intt = X,k,r,sign =0; -       while(x = pre[x]) x =Pre[x]; theR =x; -      if(J[r]) sign =1; -       while(t! =R) { -J[T] =Sign ; +K =Pre[t]; -PRE[T] =R; +t =K; A      } at      returnR; - } - BOOLcmp (Edge A,edge b) { -      returnA.W >B.W; - } - intMain () { inCin>>n>>K; -     Long LongU,v,w,ans =0, sum =0;  to      for(inti =1; I <= k;i++){ +Cin>>u; -J[u] =1; the     } *      for(inti =1; I < n;i++){ $Cin>>u>>v>>W;Panax NotoginsengSum + =W; -TMP.U =u; theTMP.V =v; +TMP.W =W; AG[i] =tmp; the     } +      for(inti =0; I <= n;i++){ -Pre[i] =i; $     } $Sort (g+1, g+n-1, CMP); -  -      the      for(inti =1; I < n;i++){ -FA =findf (g[i].u);WuyiFB =findf (G[I].V); the         if(J[fa] && J[FB])Continue; -J[FB] = (J[fa] | |J[FB]); WuPRE[FA] =PRE[FB]; -Ans + =G[I].W; About          $     } -cout<<sum-ans<<Endl; -     return 0; -}
View Code

Graph theory--spanning tree

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.