Intelligence System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2534 Accepted Submission(s): 1089
Problem Description
After a day, ALPCs finally complete their ultimate intelligence system, the purpose of it is of course for ACM ... ...
Now, kzc_tc, the head of the Intelligence Department (his code is once 48, but now 0), is sudden obtaining important information from one Intelligence personnel. That relates to the strategic direction and future development of the situation of ALPC. So it need for emergency notification to all Intelligence personnel, he decides to use the intelligence system (kzc_tc inform one, and the one inform other one or more, and so on. Finally the information is known to all).
We know this is a dangerous work. Each transmission of the information can only be made through a fixed approach, from a fixed person to another fixed, and cannot be exchanged, but between two persons may have more than one way 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 two people can inform each other, directly or indirectly through someone else, then they belong to the same branch (kzc_tc is in one branch, too!). This case, it’s very easy to inform each other, so that the cost between persons in the same branch will be ignored. The number of branch in intelligence agency is no more than one hundred.
As a result of the current tensions of ALPC’s funds, kzc_tc now has all relationships in his Intelligence system, and he want to write a program to achieve the minimum cost to ensure that everyone knows this intelligence.
It's really annoying!
Now, kzc_tc, the head of the Intelligence Department (his code is once 48, but now 0), is sudden obtaining important information from one Intelligence personnel. That relates to the strategic direction and future development of the situation of ALPC. So it need for emergency notification to all Intelligence personnel, he decides to use the intelligence system (kzc_tc inform one, and the one inform other one or more, and so on. Finally the information is known to all).
We know this is a dangerous work. Each transmission of the information can only be made through a fixed approach, from a fixed person to another fixed, and cannot be exchanged, but between two persons may have more than one way 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 two people can inform each other, directly or indirectly through someone else, then they belong to the same branch (kzc_tc is in one branch, too!). This case, it’s very easy to inform each other, so that the cost between persons in the same branch will be ignored. The number of branch in intelligence agency is no more than one hundred.
As a result of the current tensions of ALPC’s funds, kzc_tc now has all relationships in his Intelligence system, and he want to write a program to achieve the minimum cost to ensure that everyone knows this intelligence.
It's really annoying!
Input
There are several test cases.
In each case, the first line is an Integer N (0< N <= 50000), the number of the intelligence personnel including 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.
In each case, the first line is an Integer N (0< N <= 50000), the number of the intelligence personnel including 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 way for him to communicate with all other intelligence personnel.
Believe kzc_tc’s working! There always is a way for him to communicate with all other intelligence personnel.
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
Source
题意:
n个点,m条带权有向边,在同一个强联通分量中的若干个点可以看做是一个点,问从0点所在的强联通分量出发经过所有点(联通分量)最小的权值
最 小树形图,就是给有向带权图中指定一个特殊的点root,求一棵以root为根的有向生成树T,并且T中所有边的总权值最小。
代码:
//求出强联通分量,然后求一个最小树形图。千万不是最小生成树。 //首先为除根之外的每个点选定一条入边,这条入边一定要是所有入边中最小的。 //现在所有的最小 入边都选择出来了,如果这个入边集不存在有向环的话, //(强连通分量新建的图一定不会有环)这个集合就是该图的最小树形图。 #include<iostream> #include<cstdio> #include<cstring> #include<stack> #include<vector> using namespace std; const int maxn=50004,inf=0x7fffffff; int n,m,a,b,c; int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt; int in[102]; stack<int>s; vector<int>g[maxn]; struct Lu { int x,y,z; }L[100005]; void dfs(int u) { pre[u]=lowlink[u]=++dfs_clock; s.push(u); for(int i=0;i<(int)g[u].size();i++){ int v=g[u][i]; if(!pre[v]){ dfs(v); lowlink[u]=min(lowlink[u],lowlink[v]); } else if(!sccno[v]) lowlink[u]=min(lowlink[u],pre[v]); } if(lowlink[u]==pre[u]){ scc_cnt++; for(;;){ int x=s.top(); s.pop(); sccno[x]=scc_cnt; if(x==u) break; } } } void find_scc() { dfs_clock=scc_cnt=0; memset(pre,0,sizeof(pre)); memset(sccno,0,sizeof(sccno)); for(int i=0;i<n;i++){ if(!pre[i]) dfs(i); } } int main() { while(~scanf("%d%d",&n,&m)){ for(int i=0;i<=n;i++){ g[i].clear(); } for(int i=0;i<m;i++){ scanf("%d%d%d",&a,&b,&c); L[i].x=a;L[i].y=b;L[i].z=c; g[a].push_back(b); } find_scc(); int ans=0; for(int i=1;i<=scc_cnt;i++){ in[i]=inf; } for(int i=0;i<m;i++){ //选最小入边 int nux=L[i].x,nuy=L[i].y; if(sccno[nux]==sccno[nuy]) continue; if(sccno[nuy]==sccno[0]) continue; in[sccno[nuy]]=min(in[sccno[nuy]],L[i].z); } for(int i=1;i<=scc_cnt;i++){ if(in[i]==inf) continue; ans+=in[i]; } printf("%d ",ans); } return 0; }