HDU 3072 图论scc

Posted poler

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 3072 图论scc相关的知识,希望对你有一定的参考价值。

 1 #include<bits/stdc++.h>
 2 #define INF 0x7fffffff
 3 using namespace std;
 4 const int MAXN = 50010;//点数 
 5 const int MAXM = 100100;//边数 
 6 struct Edge {  
 7     int to,next,val; 
 8 }edge[MAXM]; 
 9 int head[MAXN],tot,a,b; 
10 int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong数组的值是1~scc 
11 int Index,top; int scc;//强连通分量的个数 
12 int in[MAXN],out[MAXN],val[MAXN],d[MAXN];
13 bool Instack[MAXN]; 
14 int num[MAXN];//各个强连通分量包含点的个数,数组编号1~scc //num数组不一定需要,结合实际情况 
15 int cnt=0,ans=0;
16 void addedge(int u,int v,int w){  
17     edge[tot].to = v;
18     edge[tot].next = head[u];
19     edge[tot].val=w;
20     head[u] = tot++; 
21 } 
22 void Tarjan(int u){
23     int v;  
24     Low[u] = DFN[u] = ++Index; 
25     Stack[top++] = u;  
26     Instack[u] = true;  
27     for(int i = head[u];i != -1;i = edge[i].next) {  
28         v = edge[i].to;   
29         if( !DFN[v] ){
30             Tarjan(v);   
31             if( Low[u] > Low[v] )
32                 Low[u] = Low[v];   
33         }   
34         else if(Instack[v] && Low[u] > DFN[v])
35             Low[u] = DFN[v];  
36         }  
37         if(Low[u] == DFN[u]){ 
38             scc++; 
39             do{    
40                 v = Stack[--top];
41                 Instack[v] = false;    
42                 Belong[v] = scc;    
43                 num[scc]++;   
44             }while( v != u);  
45         } 
46 } 
47 void find_val(int u){
48     int v;
49     for(int i = head[u];i != -1;i = edge[i].next){
50         v = edge[i].to;
51         if(Belong[u]==Belong[v]) continue;
52         val[Belong[v]]=min(val[Belong[v]],edge[i].val);
53         //out[Belong[u]]++;
54     }
55 }
56 void solve(int N){
57     memset(DFN,0,sizeof(DFN));
58     memset(Instack,false,sizeof(Instack));
59     memset(num,0,sizeof(num));  
60     Index = scc = top = 0;  
61     for(int i=1;i<=N;i++) val[i]=INF;
62     for(int i = 1;i <= N;i++)
63         if(!DFN[i])    
64         Tarjan(i); 
65     for(int i=1;i<=N;i++){
66     //    val[Belong[i]]=min(val[Belong[i]],d[i]);
67         find_val(i);
68     }
69     val[Belong[1]]=0;
70     for(int i=1;i<=scc;i++){
71         ans+=val[i];
72     }
73 } 
74 void init(){
75     tot = 0; 
76     cnt=0,ans=0;
77     memset(head,-1,sizeof(head)); 
78     memset(in,0,sizeof(in));
79     memset(out,0,sizeof(out));
80 } 
81 int main(){
82     int n,m,u,v,w,T;
83     while(scanf("%d%d",&n,&m)!=EOF){ 
84         init();
85         //for(int i=1;i<=n;i++) scanf("%d",&d[i]);
86         for(int i=1;i<=m;i++){
87             scanf("%d%d%d",&u,&v,&w);
88             u++,v++;
89             addedge(u,v,w);    
90         }
91         solve(n);
92         cout<<ans<<endl;
93         //cout<<cnt<<‘ ‘<<ans<<endl;
94     }
95     return 0;
96 }

 

以上是关于HDU 3072 图论scc的主要内容,如果未能解决你的问题,请参考以下文章

HDU-3072-IntelligenceSystem(tarjan,贪心)

HDU——T 3072 Intelligence System

Intelligence System HDU - 3072(强连通分量)

HDU 3072 Intelligence System

hdu 3072 Intelligence System

HDU - 3072 Intelligence System