Intelligence System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 434 Accepted Submission(s): 196
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!
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.
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.
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
Source
2009 Multi-University Training Contest 17 - Host by NUDT
Recommend
lcy
题目: http://acm.hdu.edu.cn/showproblem.php?pid=3072
分析:这题要求缩点之后的最小树形图,由于缩点后已经没有环了,所以直接去最小值就行
代码:
#include<cstdio>
#define min(a,b) a<b?a:b
using namespace std;
const int mm=111111;
const int mn=55555;
const int oo=1000000000;
int fro[mm],ver[mm],cost[mm],next[mm];
int head[mn],dfn[mn],low[mn],q[mn],id[mn];
int i,u,v,c,n,m,tms,qe,ans;
void dfs(int u)
{
int i,v;
dfn[u]=low[u]=++tms;
q[qe++]=u;
for(i=head[u];i>=0;i=next[i])
if(!dfn[v=ver[i]])
dfs(v),low[u]=min(low[u],low[v]);
else if(id[v]<0)low[u]=min(low[u],dfn[v]);
if(low[u]==dfn[u])
{
id[u]=u;
while((v=q[--qe])!=u)id[v]=u;
}
}
void tarjan()
{
int i;
for(qe=tms=i=0;i<n;++i)dfn[i]=0,id[i]=-1;
for(i=0;i<n;++i)
if(!dfn[i])dfs(i);
}
int main()
{
while(scanf("%d%d",&n,&m)!=-1)
{
for(i=0;i<n;++i)head[i]=-1;
for(i=0;i<m;++i)
{
scanf("%d%d%d",&u,&v,&c);
fro[i]=u,ver[i]=v,cost[i]=c,next[i]=head[u],head[u]=i;
}
tarjan();
for(i=0;i<n;++i)low[i]=oo;
for(i=0;i<m;++i)
if(id[fro[i]]!=(v=id[ver[i]]))low[v]=min(low[v],cost[i]);
for(i=ans=0;i<n;++i)
if(low[i]<oo)ans+=low[i];
printf("%d\n",ans);
}
return 0;
}