Give a weighted graph to connect the whole graph. The points in the SCC are spent 0, so the weight of the minimum edge is reduced by the first point, then the weighted value between the two points after the point is shrunk, and the sum of these weights is the answer.
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <vector>6#include <stack>7 using namespacestd;8 9 intN, M;Ten One Const intMAXN =50000+Ten; A -vector<int>G[MAXN], C[MAXN]; - thestack<int>S; - intPRE[MAXN], LOWLINK[MAXN], SCCNO[MAXN]; - intDfs_clock, scc_cnt; - + voidDfsintu) - { +Pre[u] = Lowlink[u] = + +Dfs_clock; A s.push (u); at for(inti =0; I < g[u].size (); i++) - { - intv =G[u][i]; - if(!Pre[v]) - { - Dfs (v); inLowlink[u] =min (Lowlink[u], lowlink[v]); - } to Else if(!sccno[v]) lowlink[u] =min (Lowlink[u], pre[v]); + } - the if(Lowlink[u] = =Pre[u]) * { $scc_cnt++;Panax Notoginseng for(;;) - { the intx =S.top (); S.pop (); +SCCNO[X] =scc_cnt; A if(x = = u) Break; the } + } - } $ $ voidFIND_SCC () - { -Dfs_clock = scc_cnt =0; thememset (PRE,0,sizeof(pre)); -memset (Sccno,0,sizeof(SCCNO));Wuyi for(inti =0; I < n; i++)if(!Pre[i]) DFS (i); the } - Wu intCOST[MAXN]; - About intMain () $ { - while(SCANF ("%d%d", &n, &m) = =2) - { - for(inti =0; I < n; i++) {g[i].clear (); C[i].clear (); } A while(m--) + { the intU, V, D; scanf"%d%d%d", &u, &v, &d); - G[u].push_back (v); C[u].push_back (d); $ } the FIND_SCC (); the thememset (Cost,-1,sizeof(cost)); the for(inti =0; I < n; i++) - for(intj =0; J < G[i].size (); J + +) in { the intU = sccno[i], V =Sccno[g[i][j]]; the if(U = = v)Continue; About if(Cost[v] = =-1) Cost[v] =C[i][j]; the ElseCOST[V] =min (Cost[v], c[i][j]); the } the + intAns =0; - for(inti =1; I <= scc_cnt; i++)if(Cost[i]! =-1) ans + =Cost[i]; theprintf"%d\n", ans);Bayi } the the return 0; -}
code June
HDU 3072 SCC Intelligence System