hdu 3072(强连通分量)

本文探讨了一种在情报系统中实现信息快速传播的方法,通过构建最优传输路径来降低总成本,确保所有人员都能及时获得重要情报。

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

Intelligence System

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


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
 

Recommend
lcy
 
分析:这题要求缩点之后的最小树形图,由于缩点后已经没有环了,所以直接去最小值就行
代码:
#include<cstdio>
#define min(a,b) a<b?a:b
using namespace std;
const int mm=111111;
const int mn=55555;
const int oo=1000000000;
int fro[mm],ver[mm],cost[mm],next[mm];
int head[mn],dfn[mn],low[mn],q[mn],id[mn];
int i,u,v,c,n,m,tms,qe,ans;
void dfs(int u)
{
    int i,v;
    dfn[u]=low[u]=++tms;
    q[qe++]=u;
    for(i=head[u];i>=0;i=next[i])
        if(!dfn[v=ver[i]])
            dfs(v),low[u]=min(low[u],low[v]);
        else if(id[v]<0)low[u]=min(low[u],dfn[v]);
    if(low[u]==dfn[u])
    {
        id[u]=u;
        while((v=q[--qe])!=u)id[v]=u;
    }
}
void tarjan()
{
    int i;
    for(qe=tms=i=0;i<n;++i)dfn[i]=0,id[i]=-1;
    for(i=0;i<n;++i)
        if(!dfn[i])dfs(i);
}
int main()
{
    while(scanf("%d%d",&n,&m)!=-1)
    {
        for(i=0;i<n;++i)head[i]=-1;
        for(i=0;i<m;++i)
        {
            scanf("%d%d%d",&u,&v,&c);
            fro[i]=u,ver[i]=v,cost[i]=c,next[i]=head[u],head[u]=i;
        }
        tarjan();
        for(i=0;i<n;++i)low[i]=oo;
        for(i=0;i<m;++i)
            if(id[fro[i]]!=(v=id[ver[i]]))low[v]=min(low[v],cost[i]);
        for(i=ans=0;i<n;++i)
            if(low[i]<oo)ans+=low[i];
        printf("%d\n",ans);
    }
    return 0;
}


### 使用Tarjan算法计算强连通分量数量 #### 算法原理 Tarjan算法通过深度优先搜索(DFS)遍历有向图中的节点,记录访问顺序和低链值(low-link value),从而识别出所有的强连通分量。当发现一个节点的访问序号等于其最低可达节点编号时,表明找到了一个新的强连通分量。 #### 时间复杂度分析 该方法的时间效率取决于存储结构的选择。对于采用邻接表表示的稀疏图而言,整体性能更优,能够在线性时间内完成操作,即O(n+m)[^4];而针对稠密图则可能退化至平方级别(O())。 #### Python代码实现 下面给出一段Python程序用于演示如何基于NetworkX库构建并处理带权无环图(DAG),进而求解其中存在的全部SCC及其总数: ```python import networkx as nx def tarjan_scc(graph): index_counter = [0] stack = [] lowlinks = {} index = {} result = [] def strongconnect(node): # Set the depth index for this node to be the next available incrementing counter. index[node] = index_counter[0] lowlinks[node] = index_counter[0] index_counter[0] += 1 stack.append(node) try: successors = graph.successors(node) except AttributeError: successors = graph.neighbors(node) for successor in successors: if successor not in lowlinks: strongconnect(successor) lowlinks[node] = min(lowlinks[node], lowlinks[successor]) elif successor in stack: lowlinks[node] = min(lowlinks[node], index[successor]) if lowlinks[node] == index[node]: scc = set() while True: current_node = stack.pop() scc.add(current_node) if current_node == node: break result.append(scc) for node in graph.nodes(): if node not in lowlinks: strongconnect(node) return result if __name__ == "__main__": G = nx.DiGraph() # Create a directed graph object using NetworkX library edges_list = [(1, 2),(2, 3),(3, 1)] # Define edge list according to sample input data from hdu1269 problem statement[^5] G.add_edges_from(edges_list) components = tarjan_scc(G) print(f"Number of Strongly Connected Components found: {len(components)}") ``` 此段脚本定义了一个名为`tarjan_scc()`的功能函数接收网络对象作为参数,并返回由集合组成的列表形式的结果集,每个子集中包含了构成单个SCC的所有顶点。最后部分展示了创建测试用DAG实例的过程以及调用上述功能获取最终答案的方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值