hdoj 3072 Intelligence System 【SCC缩点 求连通所有SCC的费用】

本文介绍了一个基于SCC(强连通分量)算法的问题解决案例,通过对人员间信息传递关系的建模,采用Tarjan算法求解最小成本的传递路径。

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

Intelligence System

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


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
 

题意:有N个人编号从0到N-1,给出M组关系<u,v,w>表示u联系v需要费用w(但不代表v联系u需要费用w)。若一个集合中 任意两个人可以互相联系(不管是直接联系的还是通过其他人间接联系的),那么在这个集合里面联系的费用可以忽略。现在你是编号0,问你联系到所有人的最小费用。题目保证至少有一组方案使得你可以联系到所有人。
 
分析:集合中任意两个人可以互相联系,我们可以把这个集合看成一个SCC。这样题目就变成——从编号为0的人所在的SCC出发,连通其它SCC所需要的最小费用。



思路:先tarjan求出所有SCC,在缩点的过程中更新 连通SCC所需要的最小费用,最后累加即可。



可以适当进行一些优化,建图时的重边处理 + 更新最小费用。这样边数减少了嘛。。

亲测 —— 去重 499ms ,不去 561ms


AC代码:
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 50000+10
#define MAXM 100000+100
#define INF 100000000
using namespace std;
struct Edge
{
    int from, to, val, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int low[MAXN], dfn[MAXN];
int dfs_clock;
int sccno[MAXN], scc_cnt;
stack<int> S;
bool Instack[MAXN];
int N, M;
void init()
{
    edgenum = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w)//注意重边的处理
{
    int i;
    for(i = head[u]; i != -1; i = edge[i].next)
    {
        if(edge[i].to == v)
            break;
    }
    if(i == -1)
    {
        Edge E = {u, v, w, head[u]};
        edge[edgenum] = E;
        head[u] = edgenum++;
    }
    else//去重 更新较小的花费
        edge[i].val = min(edge[i].val, w);
}
void getMap()
{
    int a, b, c;
    for(int i = 1; i <= M; i++)
    {
        scanf("%d%d%d", &a, &b, &c);
        addEdge(a, b, c);
    }
}
void tarjan(int u, int fa)
{
    int v;
    low[u] = dfn[u] = ++dfs_clock;
    S.push(u);
    Instack[u] = true;
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        v = edge[i].to;
        if(!dfn[v])
        {
            tarjan(v, u);
            low[u] = min(low[u], low[v]);
        }
        else if(Instack[v])
            low[u] = min(low[u], dfn[v]);
    }
    if(low[u] == dfn[u])
    {
        scc_cnt++;
        for(;;)
        {
            v = S.top(); S.pop();
            Instack[v] = false;
            sccno[v] = scc_cnt;
            if(v == u) break;
        }
    }
}
void find_cut(int l, int r)
{
    memset(low, 0, sizeof(low));
    memset(dfn, 0, sizeof(dfn));
    memset(sccno, 0, sizeof(sccno));
    memset(Instack, false, sizeof(Instack));
    dfs_clock = scc_cnt = 0;
    for(int i = l; i <= r; i++)
        if(!dfn[i]) tarjan(i, -1);
}
vector<int> G[MAXN];//缩点后新图
int dp[MAXN];//连通编号为i的SCC的花销
void suodian()
{
    for(int i = 1; i <= scc_cnt; i++)
        G[i].clear(), dp[i] = INF;
    for(int i = 0; i < edgenum; i++)
    {
        int u = sccno[edge[i].from];
        int v = sccno[edge[i].to];
        if(u != v)
        {
            G[u].push_back(v);
            dp[v] = min(dp[v], edge[i].val);
        }
    }
}
void solve()
{
    find_cut(0, N-1);
    suodian();//缩点
    int ans = 0;
    for(int i = 1; i <= scc_cnt; i++)
    {
        if(sccno[0] != i)//节点0所在的SCC 自然不需要计算花费
            ans += dp[i];
    }
    printf("%d\n", ans);
}
int main()
{
    while(scanf("%d%d", &N, &M) != EOF)
    {
        init();
        getMap();
        solve();
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值