首页 > 代码库 > hdu 3072 Intelligence System(强连通分量)
hdu 3072 Intelligence System(强连通分量)
Intelligence System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1386 Accepted Submission(s): 618
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
强连通缩点得到有向无环图,除了给定的点外,每个点的入度均为1,然后求入边的最小值,求和即可。
#include"stdio.h" #include"string.h" #include"stack" #include"algorithm" #include"iostream" using namespace std; #define N 50005 #define min(a,b) (a<b?a:b) const int inf=0x7fffffff; stack<int>s; int head[N],dfn[N],low[N],mark[N],be[N]; int n,m,t,index,bcnt,stop,dis[N],stap[N]; struct node { int u,v,w,next; }e[N*3]; struct st { int u,v,w; }g[N*3]; bool cmp(st a,st b) { return a.w<b.w; } void add(int u,int v,int w) { e[t].u=u; e[t].v=v; e[t].w=w; e[t].next=head[u]; head[u]=t++; } void tarjan(int u) { int i,v; dfn[u]=low[u]=++index; stap[++stop]=u; mark[u]=1; for(i=head[u];i!=-1;i=e[i].next) { v=e[i].v; if(!dfn[v]) { tarjan(v); low[u]=min(low[u],low[v]); } else if(mark[v]) { low[u]=min(low[u],dfn[v]); } } if(dfn[u]==low[u]) { bcnt++; do { v=stap[stop--]; mark[v]=0; be[v]=bcnt; }while(u!=v); } } void solve() { int i; memset(dfn,0,sizeof(dfn)); index=bcnt=stop=0; for(i=0;i<n;i++) { if(!dfn[i]) tarjan(i); } } void work() { int i,u,v,ans=0; for(i=1;i<=bcnt;i++) dis[i]=inf; for(i=0;i<m;i++) //缩点后图就是一个有向无环图(deg) { //除定点外,每个点的入度均为1 u=be[g[i].u]; v=be[g[i].v]; if(u!=v) { dis[v]=min(dis[v],g[i].w); } } for(i=1;i<=bcnt;i++) { if(dis[i]!=inf) ans+=dis[i]; } printf("%d\n",ans); } int main() { int i,num,u,v,w; while(~scanf("%d%d",&n,&m)) { memset(head,-1,sizeof(head)); num=t=0; for(i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&w); add(u,v,w); g[num].u=u; g[num].v=v; g[num++].w=w; } solve(); work(); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。