HDU 3072--intelligence System "SCC indent new composition && find minimum cost to connect all SCC"

Source: Internet
Author: User

Intelligence SystemTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1859 Accepted Submission (s): 799


Problem Descriptionafter 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 (he code is once, and now 0), is sudden obtaining important infor Mation from one Intelligence personnel. That relates to the strategic direction and future development of the situation of ALPC. So it need-emergency notification to all Intelligence personnel, he decides-use the Intelligence system (KZC_TC inf ORM One, and the one inform other one or more, and so on. Finally The information is known to all).
We know a dangerous work. Each transmission of the information can is made through a fixed approach, from a fixed person to another fixed, and Cannot is exchanged, but between-persons may has more than one-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 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 of the other, so-the-between persons in the same branch would be ignored. The number of branch in intelligence agency are no more than one hundred.
As a result of the tensions of ALPC ' s funds, KZC_TC now have all relationships in his Intelligence system, and he W Ant to write a program to achieve the minimum cost to ensure so everyone knows this intelligence.
It ' s really annoying!

Inputthere is several test cases.
The first line was 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.

Outputthe minimum total cost for inform everyone.
Believe KZC_TC ' s working! There always is a-on-the-him-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
The main idea: to give a graph containing n (0<n<=50000) nodes, the connection between the two points in the figure will pay a price (through the sum of Benquan), but if the two points can reach each other, the cost is 0.

Q. What is the minimum cost of communicating to all other points from a given node 0?


Idea: If the two points can reach each other (direct introduction can be), the cost is 0, that is, the cost of a point connection in an SCC is 0. So first the SCC shrinks a new composition and forms a DAG graph (directed acyclic graph). Note: It is not necessary to connect points within an SCC, but it is a cost to connect two SCC, so we need to find the least cost of the entire SCC in each SCC so that we can spend the least amount of time connecting all the SCC. Also note that the 0-point SCC costs 0.

#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 50000 + 5000#define MAXM 100000 + 1 0000#define INF 0x3f3f3f3fusing 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];//records the cost of each indent.    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//has a heavy edge, which updates the minimum cost of this edge 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) {//with new minimum cost per indent 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;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3072--intelligence System "SCC indent new composition && find minimum cost to connect all SCC"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.