您的位置:首页 > 大数据 > 人工智能

HDU 3072 //图的强连通性,缩点后求树形图 //TARJAN算法

2010-09-07 19:02 405 查看

Intelligence System

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


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
 
 

Recommend
lcy

 

#include<stdio.h>
#include<string.h>
#define MAX_SIZE 50010
#define INF 100000000
int stack[MAX_SIZE];
bool instack[MAX_SIZE];
int dist[MAX_SIZE];
int N,M,b_cnt,idx,top;
struct Edge
{
    int adj,val;//adj
    Edge* next;//point to next
};
struct Node
{
    int DFN,LOW;//定义DFN(u)为节点u搜索的次序编号(时间戳),Low(u)为u或u的子树能够追溯到的最早的栈中节点的次序号。
    int belongs;//belongs to B_cnt,属于哪一个强联通分支
    Edge *first;//point to first,形成链状
}node[MAX_SIZE];
int min(int a,int b)
{
    if(a<b)  return a;
    return b;
}
void Init() //initial node[],in[],out[]
{
    for(int i=0; i<=N; i++)
    {
        node[i].first=NULL;
        node[i].DFN=node[i].LOW=0;
        node[i].belongs=0;
        instack[i]=false;
        dist[i]=INF;
    }
}
void InsertEdge(int u,int v,int c) //insert edge u->v
{
    Edge*p=new Edge;
    p->adj=v;//指向的下一个节点
    p->val=c;
    p->next=node[u].first;
    node[u].first=p;
}
void Tarjan(int u) //Tarjan
{
    int v;
    node[u].DFN=node[u].LOW=(++idx);
    instack[u]=true;
    stack[++top]=u;
    for(Edge*p=node[u].first; p; p=p->next)
    {
        v=p->adj;
        if(!node[v].DFN)
        {
            Tarjan(v);
            if(node[v].LOW<node[u].LOW)
                node[u].LOW=node[v].LOW;
        }
        else if(instack[v]&&node[v].DFN<node[u].LOW)
            node[u].LOW=node[v].DFN;
    }
    if(node[u].DFN==node[u].LOW)
    {
        b_cnt++;
        do
        {
            v=stack[top--];
            instack[v]=false;
            node[v].belongs=b_cnt;
        }
        while(u!=v);
    }
}
void Tarjan_SCC() //StronglyConnectedComponent
{
    int i,t1,t2;
    b_cnt=idx=top=0;//b_cnt 新图的点数
    for(i=1; i<=N; i++)
        if(!node[i].DFN)
            Tarjan(i);
}
int main()
{
    int v,w;
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        Init();
        for(int i=0; i<M; i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            InsertEdge(a,b,c);
        }
        Tarjan_SCC();
        for(int i=0;i<N;i++)
        {
            for(Edge *p=node[i].first;p;p=p->next)
            {
                v=p->adj;
                w=p->val;
                if(node[i].belongs!=node[v].belongs) dist[node[v].belongs]=min(dist[node[v].belongs],w);
            }
        }
        int sum=0;
        for(int i=1;i<=b_cnt;i++)
        {
            if(dist[i]==INF) dist[i]=0;
            sum+=dist[i];
        }
        printf("%d/n",sum);

    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息