Intelligence System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
/*
分析:
首先,因为在同一个环中的点可以马上相互到达,
所以对一个环中的点,他只要到达其中的一个点便可以了
所以,我们经过强连通缩点之后,会形成一个有向无环图,
给的那个定点显然是 DAG 中入度为 0 的点,并且入度为 0 的点肯定只有一个
对于到达每个点u所需要的最小费用,便是从另外一个点到这个点u的边值的最小值
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF=10000000;
const int MAXN = 50100;
const int MAXM = 101000;
struct Edge
{
int to,next;
int cost;
}edge[MAXM];
int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];
int Index,top;
int scc;
bool Instack[MAXN];
void addedge(int u,int v,int cost)
{
edge[tot].to = v;edge[tot].next = head[u];
edge[tot].cost=cost;
head[u] = tot++;
}
void Tarjan(int u)
{
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
for(int i = head[u];i != -1;i = edge[i].next)
{
v = edge[i].to;
if(!DFN[v])
{
Tarjan(v);
if(Low[u] > Low[v])
Low[u] = Low[v];
}
else if(Instack[v] && Low[u] > DFN[v])
Low[u] = DFN[v];
}
if(Low[u] == DFN[u])
{
scc++;
do
{
v = Stack[--top];
Belong[v] = scc;
Instack[v] = false;
}
while( v!= u);
}
}
int dis[MAXN];
void solve(int N)
{
memset(DFN,0,sizeof(DFN));
memset(Instack,false,sizeof(Instack));
Index = scc = top = 0;
int start=1;
for(int i = 1;i <= N;i++){
if(!DFN[i])
Tarjan(i);
}
for(int u = 1;u <= N;u++)
{
for(int i = head[u];i != -1;i = edge[i].next)
{
int v = edge[i].to;
if(Belong[u] != Belong[v])
dis[Belong[v]]=min(dis[Belong[v]],edge[i].cost);
}
}
__int64 ans=0;
for(int i=1;i<=scc;i++){
if(dis[i]!=INF)
ans+=dis[i];
}
printf("%I64d\n",ans);
}
void init()
{
tot = 0;
memset(head,-1,sizeof(head));
}
int main()
{
int n,m,u;
int v,cost;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=1;i<=n;i++)
dis[i]=INF;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&u,&v,&cost);
u++,v++;
addedge(u,v,cost);
}
solve(n);
}
return 0;
}