Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2909 Accepted Submission(s):
1259
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
150
100
50
Source
2009
Multi-University Training Contest 17 - Host by NUDT
Tarjan缩点
题意:给定一个有向图,求点连通的最小代价,如果几个点构成强连通,则花费为0。
因为缩点后是一个有向无环图(DAG) 所以入度为0的点就不用管了 (用它去连其他的)
所以缩点后统计最小代价然后累加就好了
#include <algorithm> #include <ctype.h> #include <cstring> #include <cstdio> #define N 100005 #define INF 0x7fffffff using namespace std; struct graph { graph * next; int to,dis; }Graph[N<<2],*head[N]; inline void Read(int &x) { register char ch=getchar(); for(x=0;!isdigit(ch);ch=getchar()); for(;isdigit(ch);x=x*10+ch-‘0‘,ch=getchar()); } bool instack[N]; int cost[N],cnt,n,m,low[N],dfn[N],stack[N],top,sumcol,col[N],tim; int min(int a,int b) {return a>b?b:a;} void tarjan(int now) { dfn[now]=low[now]=++tim; instack[now]=true; stack[++top]=now; for(graph * u=head[now];u;u=u->next) { int v=u->to; if(!dfn[v]) tarjan(v),low[now]=min(low[now],low[v]); else if(instack[v]) low[now]=min(low[now],dfn[v]); } if(dfn[now]==low[now]) { int k; sumcol++; do { k=stack[top--]; instack[k]=false; col[k]=sumcol; }while(k!=now); } } inline void init() { cnt=sumcol=top=tim=0; memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(col,0,sizeof(col)); memset(head,0,sizeof(head)); memset(Graph,0,sizeof(Graph)); } inline void ins(int u,int v,int w) { Graph[++cnt].next=head[u]; Graph[cnt].to=v; Graph[cnt].dis=w; head[u]=Graph+cnt; } int main() { for(;scanf("%d%d",&n,&m)!=EOF;) { for(int x,y,z;m--;) { scanf("%d%d%d",&x,&y,&z); ins(x,y,z); } for(int i=0;i<n;++i) { if(!dfn[i]) tarjan(i); cost[i]=INF;cost[i+1]=INF; } for(int i=0;i<n;++i) { for(graph * u=head[i];u;u=u->next) { int v=u->to; if(col[i]!=col[v]) cost[col[v]]=min(cost[col[v]],u->dis); } } int ans=0; for(int i=1;i<=sumcol;++i) if(cost[i]!=INF) ans+=cost[i]; printf("%d\n",ans); init(); } return 0; }