Hdu3072-intelligence System (strong connectivity for minimum value)

Source: Internet
Author: User

After a day, Alpcs finally complete their ultimate intelligence system, the purpose of it are of course for ACM ...Now, KZC_TC, the head of the Intelligence Department (he code is once, and now 0), is sudden obtaining important infor Mation from one Intelligence personnel. That relates to the strategic direction and future development of the situation of ALPC. So it need-emergency notification to all Intelligence personnel, he decides-use the Intelligence system (KZC_TC inf ORM One, and the one inform other one or more, and so on. Finally The information is known to all).  We know this was a dangerous work. Each transmission of the information can is made through a fixed approach, from a fixed person to another fixed, and Cannot is exchanged, but between-persons may has more than one-for-transferring. Each act of the "transmission Cost CI" (1 <= ci <= 100000), the total cost of the transmission if inform some ones in Our ALPC intelligence agency is their costs sum.Something Good, if people can inform each other, directly or indirectly through someone else, then they belong t o the same branch (KZC_TC is in one branch, too!). This case, it's very easy-to-inform each of the other, so-the-between persons in the same branch would be ignored. The number of branch in intelligence agency are no more than one hundred. as a result of the tensions of ALPC ' s funds, KZC_TC now have all relationships in his Intelligence system, an D He want to write a program to achieve the minimum cost to ensure so everyone knows this intelligence. It ' s really annoying!  

Input

There is several test cases.the first line was an Integer n (0< n <= 50000), the number of the intelligence personnel Includi Ng KZC_TC. Their code is numbered from 0 to N-1. And then M (0<= m <= 100000), the number of the transmission approach. the next M lines, each line contains three integers, X, Y and C means person X transfer information to person Y cost C.

Output

The minimum total cost for inform everyone.believe KZC_TC ' s working! There always is a-on-the-him-communicate with all other intelligence personnel.  

Sample Input

3 30 1 1001 2 500 2 1003 30 1 1001 2 502 1 1002 20 1 500 1 100

Sample Output

15010050

Test instructions: The simplest point is to ask for the minimum cost of connecting all the strong connected components together

Analysis: First to find out all the strong connectivity components, and then to find the minimum cost of connecting components, and finally to the 0 of the connected components needed to connect the minimum cost,

Code

#include <cstdio>#include<cstring>#include<string>#include<iostream>#include<sstream>#include<algorithm>#include<utility>#include<vector>#include<Set>#include<map>#include<queue>#include<cmath>#include<iterator>#include<stack>using namespacestd;Const intinf=1e9+7;Const inteps=0.0000001; typedef __int64 LL;Const intmaxn=50005;Const intmaxm=100005;intN,m,ans,id;intDfn[maxn],low[maxn],cost[maxn],resign[maxn];vector<int>G[maxn];stack<int>KK;BOOLINQ[MAXN];structedge{intu,v,w; Edge (intu=0,intv=0,intw=0): U (U), V (v), W (w) {}}E[MAXM];voidinit () {ans=id=0;  while(!Kk.empty ())    Kk.pop ();  for(intI=0; i<=n;i++) {Dfn[i]=low[i]=0; Resign[i]=0; Cost[i]=INF;        G[i].clear (); Inq[i]=false; }}voidTarjan (intx) {Dfn[x]=low[x]=++ID; INQ[X]=true;    Kk.push (x); intT,size=g[x].size ();  for(intI=0; i<size;i++) {T=F[x][i]; if(!Dfn[t])            {Tarjan (t); LOW[X]=min (low[x],low[t]); }        Else if(Inq[t]) low[x]=min (low[x],dfn[t]); }  //The front is almost .    if(dfn[x]==Low[x]) {ans++;  Do{T=Kk.top ();            Kk.pop (); Inq[t]=false; Resign[t]=ans;//this place, labeled connected components} while(t!=x); }    return;}intMain () { while(SCANF ("%d%d", &n,&m)! =EOF)        {init (); intu,v,w;  for(intI=1; i<=m;i++) {scanf ("%d%d%d",&u,&v,&W); E[i]=Edge (U,V,W); G[u].push_back (v); //a map of the direction        }         for(intI=0; i<n;i++)if(!Dfn[i])        Tarjan (i);  for(intI=1; i<=m;i++) {Edge& e=E[i]; intu=e.u,v=e.v,w=E.W; intX=RESIGN[U],Y=RESIGN[V];//connectivity Component Boom number            if(x!=y) cost[y]=min (COST[Y],W);//Update Values        }        intsum=0;  for(intI=1; i<=ans;i++)        {            if(i==resign[0]|| Cost[i]==inf)Continue;//with 0 is the unified connectivity component, regardless ofsum+=Cost[i]; } printf ("%d\n", sum); }    return 0;}
View Code

Hdu3072-intelligence System (strong connectivity for minimum value)

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.