Intelligence System (强连通 + 缩点)

来源:互联网 发布:无损播放软件排名 编辑:程序博客网 时间:2024/05/19 13:56

Intelligence System

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 70 Accepted Submission(s): 29 
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 30 1 1001 2 500 2 1003 30 1 1001 2 502 1 1002 20 1 500 1 100
 
Sample Output
15010050
 
 
Source
2009 Multi-University Training Contest 17 - Host by NUDT


 

题意:实现某确定一点向其他所有的点直接或者间接地传递消息。如果某几个点之间消息可以互达,则不收取费用,求所有点接受到消息所花费的最小值。(除了0点其他的入度均为1.

tarjan缩点,求解,最后求最小值,可以按照最小生成树的思路。

代码参考的白书的讲解。

#include <iostream>#include <string>#include <string.h>#include <algorithm>#include <array>#include <cmath>#include <vector>#include <stdio.h>#include <stack>using namespace std;int n, m;const int INF = 0x3fffffff;const int maxn = 50010;const int mod = 1e9 + 7;vector<pair<int, int>> g[maxn];int pre[maxn], low[maxn], scc_cnt, scc_num[maxn];int dis[maxn];int dfs_clock;stack<int> st;void tarjan(int u) {    pre[u] = low[u] = ++dfs_clock;    st.push(u);    for (int i = 0; i < g[u].size(); ++i) {        int v = g[u][i].first;        if (!pre[v]) {            tarjan(v);            low[u] = min(low[u], low[v]);        } else if (!scc_num[v]) {            low[u] = min(low[u], pre[v]);        }    }    if (pre[u] == low[u]) {        scc_cnt++;        while (1) {            int x = st.top();            st.pop();            scc_num[x] = scc_cnt;            if (u == x)                break;        }    }}void solve() {    memset(scc_num, 0, sizeof(scc_num));    memset(pre, 0, sizeof(pre));    dfs_clock = scc_cnt = 0;    for (int i = 0; i < n; ++i) {        if (!pre[i])            tarjan(i);    }}int main() {        //freopen("1.txt", "r", stdin);    while (~scanf("%d%d", &n, &m)) {        for (int i = 0; i < n; ++i) {            g[i].clear();        }        for (int i = 0; i < m; ++i) {            int x, y, z;                //cin >> x >> y >> z;            scanf("%d%d%d", &x, &y, &z);            g[x].push_back({y, z});        }        solve();                for (int i = 1; i <= scc_cnt; ++i) {            dis[i] = INF;        }        for (int i = 0; i < n; ++i) {            for (int j = 0; j < g[i].size(); ++j) {                                int u = scc_num[i];                int v = scc_num[g[i][j].first];                int w = g[i][j].second;                    //cout << u << "   " << v << endl;                if (u != v) {                    dis[v] = min(dis[v], w);                }            }        }        int ans = 0;        for (int i = 1; i <= scc_cnt; ++i) {            if (dis[i] != INF)                ans += dis[i];        }            //cout << ans << endl;        printf("%d\n", ans);    }}



原创粉丝点击