hdu 3702 Intelligence System 【强连通Kosaraju+缩点染色+贪心建树】

本文介绍了一个关于情报传递的问题,通过构建有向图并利用Kosaraju算法找到强连通分量,进而求解最小成本。具体地,文章详细阐述了如何处理信息在特定条件下免费传递的情况,并给出了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Intelligence System

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

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

 

 题目大意:有一个度为0的人,从他开始传递信息,如果其中有这样的传递关系:a能够传递给b信息,b也能够传递给a信息,那么ab间信息传递是不需要消耗的,问从这个人开始传递关系,传递到让所有人都知道了消息之后的最小花费。

 

思路:

1、引入强连通的概念:如果a能够有有向边路径到达b,b也能够有有向边路径到达a,那么ab则是强连通的,其之间传递信息因为是没有消耗的,所以对于这些个人,我们可以看成一个人,只要有其他人传递过来信息给这个环里边的任意一个人,那么这个环里边的所有人就都知道了消息,而且在环内的传递是没有消耗的,所以这里我们直接缩点染色即可。我的代码实现使用的是Kosaraju算法,可能相对比较繁杂一点0.0


2、对于一个DAG图上的以某点为根开始建树的树,我一开始觉得一个DAG图,就当无向图使用克鲁斯卡尔算法贪心取边入树去做就好了,交了一发,wa了,后来自己搞了搞数据,发现这样一组数据输出了56,正确值应该是61

7 9
0 1 20
1 2 100
2 3 100
3 1 100
2 4 30
3 4 50
4 5 9
5 6 2
4 6 4

其56不对的原因是5号节点并不知道消息,但是我们贪心取边入树取了一条从5到6的边,但是这个时候5号节点不知道消息,所以这样的思路是不对的。


3、正确的思路是这样贪心建树的:

设定一个数组ans【i】表示到节点i的一条最短边。维护ans【i】的最小值,那么output=sum(ans【i】)【0<=i<n】。这样入树的话,就避免了有刚才那样情况的出现,这样我们每个点都知道信息,所以无论ans【i】的值是从哪个节点到i的边权,都能保证这个节点是知道消息的。


AC代码:


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct path
{
    int x,y,w;
}a[1000000];
int head[250050];
int head2[250050];
struct EdgeNode
{
    int from;
    int to;
    int w;
    int next;
}e[250050*5],ee[250050*5];
int in[250050];
int out[250050];
int vis[250050];
int num[250050];
int color[250050];
int ans[250050];
int n,m,cont,cont2,sig;
int cmp(path a,path b)
{
    return a.w<b.w;
}
void add(int from,int to,int w)
{
    e[cont].from=from;
    e[cont].w=w;
    e[cont].to=to;
    e[cont].next=head[from];
    head[from]=cont++;
}
void add2(int from,int to,int w)
{
    ee[cont2].w=w;
    ee[cont2].from=from;
    ee[cont2].to=to;
    ee[cont2].next=head2[from];
    head2[from]=cont2++;
}
void init()
{
    cont=0;
    cont2=0;
    memset(vis,0,sizeof(vis));
    memset(out,0,sizeof(out));
    memset(in,0,sizeof(in));
    memset(head,-1,sizeof(head));
    memset(head2,-1,sizeof(head2));
    memset(color,0,sizeof(color));
    memset(num,0,sizeof(num));
}
void Dfs(int u)
{
    vis[u]=1;
    for(int k=head[u];k!=-1;k=e[k].next)
    {
        int v=e[k].to;
        if(vis[v]==0)Dfs(v);
    }
    num[sig++]=u;
}
void Dfs2(int u)
{
    vis[u]=1;
    color[u]=sig;
    for(int k=head2[u];k!=-1;k=ee[k].next)
    {
        int v=ee[k].to;
        if(vis[v]==0)Dfs2(v);
    }
}
void Kosaraju()
{
    sig=1;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==0)
        Dfs(i);
    }
    sig=0;
    memset(vis,0,sizeof(vis));
    for(int i=n;i>=1;i--)
    {
        if(vis[num[i]]==0)
        {
            sig++;
            Dfs2(num[i]);
        }
    }
    for(int i=1;i<=sig;i++)ans[i]=0x3f3f3f3f;
    for(int i=1;i<=n;i++)
    {
        for(int k=head[i];k!=-1;k=e[k].next)
        {
            int u=i,v=e[k].to,w=e[k].w;
            if(color[u]!=color[v])
            {
                ans[color[v]]=min(ans[color[v]],w);
            }
        }
    }
    int output=0;
    for(int i=1;i<=sig;i++)
    {
        if(ans[i]==0x3f3f3f3f)continue;
        else output+=ans[i];
    }
    printf("%d\n",output);
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=0;i<m;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            x++;y++;
            add(x,y,w);
            add2(y,x,w);
        }
        Kosaraju();
    }
}
/*
3 3
0 1 200
1 2 100
2 0 100
4 4
0 1 20
1 2 100
2 3 100
3 1 100
7 9
0 1 20
1 2 100
2 3 100
3 1 100
2 4 30
3 4 50
4 5 9
5 6 2
4 6 4
*/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值