Description
Input
Output
Sample Input
3 3 0 1 100 1 2 50 0 2 100 3 3 0 1 100 1 2 50 2 1 100 2 2 0 1 50 0 1 100
Sample Output
150 100 50思路;强连通缩点后的图进行一次最小树形图就是答案,因为缩点后是DAG,所以不用再缩点了,#include<cstdio> #include<cstring> #include<iostream> #define FOR(i,a,b) for(int i=a;i<=b;++i) #define clr(f,z) memset(f,z,sizeof(f)) typedef long long type; using namespace std; const int nn=50009; const int mm=1e5+9; const long long oo=1e18; int head[nn]; class Edge { public:int u,v,next;type w; }e[mm]; int pre[mm],edge,e_to[mm],dfn[mm],dfs_clock,bcc_no,stak[nn],top; type in[nn]; void data() { clr(head,-1);edge=0; } void add(int u,int v,type w) { e[edge].u=u;e[edge].v=v;e[edge].w=w;e[edge].next=head[u];head[u]=edge++; } int tarjan(int u) { int lowu,lowv,v; lowu=dfn[u]=++dfs_clock; stak[top++]=u; for(int i=head[u];~i;i=e[i].next) { v=e[i].v; if(dfn[v]==-1) { lowv=tarjan(v); lowu=min(lowu,lowv); } else if(e_to[v]==-1) lowu=min(lowu,dfn[v]); } if(lowu==dfn[u]) { ++bcc_no; do { v=stak[--top]; e_to[v]=bcc_no; }while(v!=u); } return lowu; } type find_bcc(int n) { clr(dfn,-1);clr(e_to,-1);dfs_clock=bcc_no=top=0; FOR(i,0,n-1) if(dfn[i]==-1) tarjan(i); FOR(i,1,bcc_no)in[i]=oo; int u,v; FOR(i,0,edge-1) { u=e_to[e[i].u];v=e_to[e[i].v]; if(u^v) { if(e[i].w<in[v])pre[v]=u,in[v]=e[i].w; } } in[e_to[0]]=0;//root; type ans=0; FOR(i,1,bcc_no) ans+=in[i]; return ans; } int main() { int n,m,u,v;type w; while(~scanf("%d%d",&n,&m)) { data(); FOR(i,0,m-1) { scanf("%d%d%I64d",&u,&v,&w); add(u,v,w); } printf("%I64d\n",find_bcc(n)); } return 0; }