HDU 3072 2009多校联合17 强连通

来源:互联网 发布:鼠标宏编程有什么用 编辑:程序博客网 时间:2024/06/07 03:59
 

Intelligence System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 466    Accepted Submission(s): 208


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 30 1 1001 2 500 2 1003 30 1 1001 2 502 1 1002 20 1 500 1 100
 

Sample Output
15010050
 

Source
2009 Multi-University Training Contest 17 - Host by NUDT
 
题目说的很清楚,一个强连通分量内部的点之间不会产生任何费用
所以第一步先缩点。
由于数据保证0可以连通到其他任何点
所以入度为0的点只可能有0这个节点一个。
 
缩点完成后,下面一步就是去统计每一个点的最小花费。
最小花费之和就是所求答案。恩
 
我的代码:
#include<stdio.h>#include<vector>#include<algorithm>#include<string.h>#include<stack>#define maxn 50005#define inf 99999999using namespace std;struct node{int v;int len;};int n,m,Index,cnt;int belong[maxn],dfn[maxn],low[maxn];bool used[maxn],instack[maxn];stack<int>s;vector<node>map[maxn];int cost[105];void init(){int i;for(i=0;i<=n;i++)map[i].clear();while(!s.empty())s.pop();memset(used,0,sizeof(used));memset(instack,0,sizeof(instack));memset(dfn,-1,sizeof(dfn));memset(low,0,sizeof(low));memset(belong,0,sizeof(belong));Index=0,cnt=0;}int min(int a,int b){if(a>b)return b;elsereturn a;}void tarjan(int u){int i,v;node p;Index++;dfn[u]=Index;low[u]=Index;used[u]=true;instack[u]=true;s.push(u);for(i=0;i<map[u].size();i++){p=map[u][i];if(!used[p.v]){tarjan(p.v);low[u]=min(low[u],low[p.v]);}else if(instack[p.v]){low[u]=min(low[u],dfn[p.v]);}}if(dfn[u]==low[u]){cnt++;do{v=s.top();s.pop();belong[v]=cnt;instack[v]=false;}while(u!=v);}}int main(){int i,j,a,b,c;node p;while(scanf("%d%d",&n,&m)!=EOF){init();for(i=1;i<=m;i++){scanf("%d%d%d",&a,&b,&c);p.v=b;p.len=c;map[a].push_back(p);}for(i=0;i<n;i++)if(dfn[i]==-1)tarjan(i);for(i=1;i<=cnt;i++)cost[i]=inf;int x,y;for(i=0;i<n;i++){for(j=0;j<map[i].size();j++){p=map[i][j];x=belong[i],y=belong[p.v];if(x!=y)cost[y]=min(cost[y],p.len);}}int ans=0;for(i=1;i<=cnt;i++){if(cost[i]!=inf)ans=ans+cost[i];}printf("%d\n",ans);}return 0;}

原创粉丝点击