HDU 3072(强连通+缩点)

Intelligence System
Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
    
       

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

  

题意:实现某确定一点向其他所有的点直接或者间接地传递消息。如果某几个点之间消息可以互达,则不收取费用,求所有点接受到消息所花费的最小值。

思路:求出连通分量,连通分量内不收取费用,因此缩点。再找出连通分量之间连通的最小值(枚举即可)。

AC代码:

/*
  HDU 3072
  BY mowenwen
  2015.8.31
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 50000+10
#define maxm 100000+10
#define inf 0x7fffffff
int t, n, m;
int head[maxn],dfn[maxn],low[maxn],Stack[maxn],belong[maxn];
int ans[maxn];
int tot,top,index,scc;
bool instack[maxn];
struct Edge
{
    int to, next,cost;
}edge[maxm];
void init()
{
    memset(head, -1, sizeof(head));
    memset(dfn, 0, sizeof(dfn));
    memset(instack, 0, sizeof(instack));

    tot = top = index = scc= 0;
}
void addedge(int u, int v,int c)
{
    edge[tot].next = head[u];
    edge[tot].to = v;
    edge[tot].cost = c;
    head[u] = tot ++;
}
void tarjan(int u)
{
    int v, j;
    dfn[u] = low[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);
            low[u] = min(low[u], low[v]);
        }
        else if(instack[v])
        {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(dfn[u] == low[u])
    {
        scc ++;
        do
        {
            j = Stack[--top];
            instack[j] = false;
            belong[j] = scc;
        }
        while(j != u);
    }
    return ;
}
int main()
{
    while(scanf("%d%d", &n, &m) != EOF)
    {
        init();
        while(m --)
        {
            int a, b, c;

            scanf("%d%d%d", &a, &b, &c);
            a++;
            b++;
            addedge(a, b, c);
        }
        for(int i = 1;i <= n;i ++)
        {
            if(!dfn[i])
            tarjan(i);
        }
        for(int i = 1;i <= scc;i ++)
        ans[i] = inf;

        for(int i = 1;i <= n;i ++)
         for(int j = head[i]; j != -1; j = edge[j].next)
         {
                int v = edge[j].to;
                if(belong[i] != belong[v])
                {
                    //cout << i << " "<< v <<endl;
                    //cout << belong[i]<<" "<<belong[v] <<endl;
                    ans[belong[v]] = min(ans[belong[v]], edge[j].cost);
                }
         }
        int sum = 0;
        for(int i = 1;i < scc;i ++)
        sum += ans[i];
        printf("%d\n", sum);
    }
    return 0;
}


 

 

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值