HDU 3072 Intelligence System 強連通分量分解 + 最小樹形圖__acm

來源:互聯網
上載者:User

題目:http://acm.hdu.edu.cn/showproblem.php?pid=3072


題意:有n個人,有m條關係u v c,代表u可以通知v花費c,如果他們可以互相聯絡(可以是通過別人間接相互聯絡),那麼花費為0,問從給定點通知所有人,最小花費是多少。


思路:能夠互相聯絡的人花費為0,意味著在同一個強連通分量內的點花費為0,那麼自然而然想到縮點,求縮點後的最小花費,即是求有向非循環圖的最小樹形圖,統計每個點的最小花費入度,加起來就是答案


總結:寫錯了一個地方,調了好久,心痛。。。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>using namespace std;typedef long long ll;const int N = 50100;const int INF = 0x3f3f3f3f;struct edge{    int to, cost, next;} G[N*10], e;int dfn[N], low[N], scc[N], st[N];int head[N];int index, cnt, num, top;bool vis[N];int n, m;void init(){    memset(head, -1, sizeof head);    memset(dfn, -1, sizeof dfn);    memset(vis, 0, sizeof vis);    index = cnt = num = top;}void add_edge(int v, int u, int c){    G[cnt].to = u;    G[cnt].cost = c;    G[cnt].next = head[v];    head[v] = cnt++;}void tarjan(int v){    dfn[v] = low[v] = index++;    vis[v] = true;    st[top++] = v;    int u;    for(int i = head[v]; i != -1; i = G[i].next)    {        u = G[i].to;        if(dfn[u] == -1)        {            tarjan(u);            low[v] = min(low[v], low[u]);        }        else if(vis[u])            low[v] = min(low[v], dfn[u]);    }    if(dfn[v] == low[v])    {        num++;        do        {            u = st[--top];            vis[u] = false;            scc[u] = num;        }        while(u != v);    }}void slove(){    for(int i = 0; i < n; i++)        if(dfn[i] == -1)            tarjan(i);    int indeg[N];    memset(indeg, 0x3f, sizeof indeg);    for(int i = 0; i < n; i++)        for(int j = head[i]; j != -1; j = G[j].next)            if(scc[i] != scc[G[j].to])                indeg[scc[G[j].to]] = min(indeg[scc[G[j].to]], G[j].cost);    int res = 0;    for(int i = 1; i <= num; i++)        if(i != scc[0]) /*因為是從定點0出發去通知所有人,所以0所在的強連通分量不統計*/            res += indeg[i];    printf("%d\n", res);}int main(){    int a, b, c;    while(~ scanf("%d%d", &n, &m))    {        init();        for(int i = 0; i < m; i++)        {            scanf("%d%d%d", &a, &b, &c);            add_edge(a, b, c);        }        slove();    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.