zoukankan      html  css  js  c++  java
  • hdu 3072 Intelligence System(Tarjan 求连通块间最小值)

    Intelligence System

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 2   Accepted Submission(s) : 1

    Font: Times New Roman | Verdana | Georgia

    Font Size: ← →

    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所在的连通分量所需的最小花费连接起来

    #include <bits/stdc++.h>
    using namespace std;
    const int inf=0x7fffffff;
    int n,T,m,index,team_num;
    int low[50005],dfn[50005],team[50005],x[100005],y[100005],z[100005],co[50005];
    bool instack[50005];
    stack<int> S;
    vector<int> mp[50005];
    void Tarjan(int u)
    {
        low[u]=dfn[u]=++index;
        S.push(u);
        instack[u]=1;
        for(int i=0;i<mp[u].size();i++)
        {
            int v=mp[u][i];
            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])
        {
            team_num++;
            while(1)
            {
                int v=S.top(); S.pop();
                instack[v]=0;
                team[v]=team_num;
                if (u==v) break;
            }
        }
    }
    void dfs()
    {
        //memset(in,0,sizeof(in));
        //memset(out,0,sizeof(out));
        memset(instack,0,sizeof(instack));
        memset(team,0,sizeof(team));
        memset(low,0,sizeof(low));
        memset(dfn,0,sizeof(dfn));
        team_num=0;
        index=0;
        for(int i=1;i<=n;i++)
            if (!dfn[i]) Tarjan(i);
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            for(int i=1;i<=n;i++) mp[i].clear();
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d%d",&x[i],&y[i],&z[i]);
                x[i]++; y[i]++; // 把标号改成从0~n
                mp[x[i]].push_back(y[i]);
            }
            dfs();
      
            for(int i=1;i<=team_num;i++) co[i]=inf;
            for(int i=1;i<=m;i++)
            {
                if (team[1]==team[y[i]] || team[x[i]]==team[y[i]])continue;
                co[team[y[i]]]=min(co[team[y[i]]],z[i]);
            }
            int sum=0;
            for(int i=1;i<=team_num;i++)
                if(co[i]!=inf) sum+=co[i];
            printf("%d
    ",sum);
        }
        return 0;
    }
     
  • 相关阅读:
    微信公众号开发(一)——环境搭建
    Spring中@Async-异步处理
    SSL/TLS安全评估-查看访问的网址支持哪些协议等
    SSL异常javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
    Java代码自动部署
    SQL优化技巧
    idea2019.2 svn 忽略文件问题
    Remote System Explorer Operation在eclipse后台一直跑 解决办法
    JPA project Change Event Handler问题解决
    MySQL按首字母查询
  • 原文地址:https://www.cnblogs.com/stepping/p/7674574.html
Copyright © 2011-2022 走看看