HDU 3072--Intelligence System【SCC缩点新构图 && 求连通所有SCC的最小费用】

Intelligence System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1859    Accepted Submission(s): 799

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<=50000) 个节点的有向图,图中的两点之间的连接要付出代价的(经过的边权之和),但是如果这两个点之间相互可达,代价为 0。

问从给定的节点0向其他所有的点通信,所花费的最小代价是多少?

思路:如果这两个点之间相互可达(直接简介均可),代价为 0,即在一个SCC中的点连接的代价为0。所以首先SCC缩点新构图, 形成一个DAG图(有向无环图)。注意:一个SCC内的点相互连接是不需要花费的,但是连接两个SCC是要花费的,所以我们要在每个SCC中找到花费最小的点最为整个SCC的花费,这样我们连接所有的SCC时花费才最小。还要注意,0点所在的SCC花费为0。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 50000 + 5000
#define maxm 100000 + 10000
#define INF 0x3f3f3f3f
using namespace std;
int n, m;

struct node {
    int u, v, w, next;
};

node edge[maxm];

int head[maxn], cnt;
int low[maxn], dfn[maxn];
int dfs_clock;
int Stack[maxn], top;
bool Instack[maxn];
int Belong[maxn];
int scc_clock;
int num[maxn];//记录每个缩点的花费。

void init(){
    cnt = 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].v == v)
            break;
    }
    if(i == -1){
        edge[cnt] = {u, v, w, head[u]};
        head[u] = cnt++;
    }
    else//有重边,更新这条边最小的花费
        edge[i].w = min(edge[i].w, w);
}

void getmap(){
    int a, b, c;
    while(m--){
        scanf("%d%d%d", &a, &b, &c);
        a++, b++;
        addedge(a, b, c);
    }
}

void Tarjan(int u){
    int v;
    low[u] = dfn[u] = ++dfs_clock;
    Stack[top++] = u;
    Instack[u] = true;
    for(int i = head[u]; i != -1; i = edge[i].next){
        v = edge[i].v;
        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_clock++;
        do{
            v = Stack[--top];
            Instack[v] = false;
            Belong[v] = scc_clock;
        }
        while(v != u);
    }
}

void find(){
    memset(low, 0, sizeof(low));
    memset(dfn, 0, sizeof(dfn));
    memset(Belong, 0, sizeof(Belong));
    memset(Stack, 0, sizeof(Stack));
    memset(Instack, false, sizeof(false));
    dfs_clock = scc_clock = top = 0;
    for(int i = 1; i <= n ; ++i){
        if(!dfn[i])
            Tarjan(i);
    }
}

void suodian(){
    for(int i = 1; i <= scc_clock; ++i)
        num[i] = INF;
    for(int i = 0; i < cnt; ++i){
        int u = Belong[edge[i].u];
        int v = Belong[edge[i].v];
        if(u != v){
            //跟新每个缩点的最小花费
            num[v] = min(num[v], edge[i].w);
        }
    }
}

void solve(){
    int ans = 0;
    //printf("%d\n", scc_clock);
    for(int i = 1; i <= scc_clock; ++i){
        //printf("%d\n", num[i]);
        if(Belong[1] != i)
            ans += num[i];
    }
    printf("%d\n", ans);
}

int main (){
    while(scanf("%d%d", &n, &m) != EOF){
        init();
        getmap();
        find();
        suodian();
        solve();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-06 03:51:26

HDU 3072--Intelligence System【SCC缩点新构图 && 求连通所有SCC的最小费用】的相关文章

hdoj 3072 Intelligence System【求scc&amp;&amp;缩点】【求连通所有scc的最小花费】

Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1904    Accepted Submission(s): 824 Problem Description After a day, ALPCs finally complete their ultimate intelligence system

HDU 3072--Intelligence System【SCC缩点新构图 &amp;amp;&amp;amp; 求连通全部SCC的最小费用】

Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1859    Accepted Submission(s): 799 Problem Description After a day, ALPCs finally complete their ultimate intelligence syste

HDU 3072 Intelligence System(强连通+最小树形图)

HDU 3072 Intelligence System 题目链接 题意:给定有向图,边有权值,求保留一些边,从一点出发,能传递到其他所有点的最小代价,保证有解 思路:先缩点,然后从入度为0的点作为起点(因为题目保证有解,所以必然有一个且只有一个入度为0的点),然后做一下最小树形图即可 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #includ

[tarjan+最小树形图] hdu 3072 Intelligence System

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3072 Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1479    Accepted Submission(s): 653 Problem Description After a day, ALP

hdu 3072 Intelligence System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2909    Accepted Submission(s): 1259 Problem Description After a day, ALPCs finally complete their ultimate intelligence system, the purpose of it

HDU 3072 Intelligence System (强连通分量)

题目地址:HDU 3072 这题一开始理解错题目意思了..(不得不吐槽一下题目意思确实不好理解..)用的强连通+最小生成树做的...然后错了好多遍...sad..题目意思是从一个给定点向其他所有点通信的最少花费,那么入度为0的点肯定是那个给定点.然后对于其它点,枚举就好了.找一个到他花费最少的点的花费. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue&g

HDU 3072 Intelligence System (强连通+(贪心||树形图))

大意:在一个联通分量里面的边权值忽略不计,求缩点后的所有联通分量链接在一起的最小权值和. 思路:必然先缩点,最后得到的图为DAG,然后就是统计权值,对于那么到达当前的缩点可能有多个入度,所以选择最小的即可.最后避开起始点的dis[star] == inf.累加即可. #include<map> #include<queue> #include<cmath> #include<cstdio> #include<stack> #include<

HDU - 3072 Intelligence System(强连通分量+类最小生成树)

题目大意:有一个人想要将消息告诉给所有人(在同一个强连通分量里面的人可以相互转告,费用为0),问所有人都知道消息的最小花费是多少 解题思路:求出所有的强连通分量,然后将其缩点,桥就是连接其中的边 因为是张连通图,所以只要求出每个强连通分量被通知的最小价值,然后累加即可 刚开始以为可以用最小生成树,但发现错了,假设求出了三个强连通分量了,分别标号为1,2,3 再给出桥 1-2,权值1,1-3权值5,3-2权值4 按最小生成树的话,得到的结果是5,然而并不是这样的,因为是有向边,所以3这个点是不会被

HDU——T 3072 Intelligence System

http://acm.hdu.edu.cn/showproblem.php?pid=3072 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2909    Accepted Submission(s): 1259 Problem Description After a day, ALPCs finally complete their