1. 程式人生 > >HDU 3072 Intelligence System(tarjan染色縮點+貪心+最小樹形圖)

HDU 3072 Intelligence System(tarjan染色縮點+貪心+最小樹形圖)

Intelligence System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3414    Accepted Submission(s): 1494


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  

 

Recommend lcy   |   We have carefully selected several similar problems for you:   3069  3077  3070  3071  3073    題目意思:
n個點,m條邊的有向圖,x y w表示x到y的花費是w
但是一個強連通分量內的點互相到達的花費是0
問你要到達所有點的最小花費
題目保證圖是連通的,且存在一個點可以到所有點 分析:
存在強連通分量,強連通內的點互相到達花費為0,可以看成一個點
但是需要注意貪心一下
比如1到強連通分量x
應該選擇1到強連通分量x的所有點中權值最小的邊
縮點和貪心之後
圖變成了一個DAG圖,有向無環圖
然後就是求這個DAG圖的最小樹形圖的值 這裡的求發很巧妙,因為題目保證了圖的連通性
所有我們只需要維護所有點的最小入邊的權值就好
但是有個點是沒有入邊的
(從此點出發,可以到達所有點)   code:
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 0x7fffffff
#define me(a,x) memset(a,x,sizeof(a))
int mon1[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
int mon2[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31};
int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};

int getval()
{
    int ret(0);
    char c;
    while((c=getchar())==' '||c=='\n'||c=='\r');
    ret=c-'0';
    while((c=getchar())!=' '&&c!='\n'&&c!='\r')
        ret=ret*10+c-'0';
    return ret;
}

#define max_v 50005
struct node
{
    int v,w;
    node(int vv,int ww)
    {
        v=vv;
        w=ww;
    }
};
int dfn[max_v];
int low[max_v];
int vis[max_v];
int stk[max_v];
int color[max_v];
int a[max_v];
vector<node> G[max_v];
int n,m;
int sig,cnt,sp;
LL ans;
void init()
{
    me(dfn,0);
    me(low,0);
    me(vis,0);
    me(stk,0);
    me(color,0);
    for(int i=1;i<=n;i++)
    {
        G[i].clear();
        a[i]=INF;
    }
    sig=0;
    cnt=1;
    sp=-1;
    ans=0;
}

void tarjan(int u)
{
    vis[u]=1;
    dfn[u]=low[u]=cnt++;
    stk[++sp]=u;
    for(int j=0;j<G[u].size();j++)
    {
        int v=G[u][j].v;
        if(vis[v]==0)
            tarjan(v);
        if(vis[v]==1)
            low[u]=min(low[u],low[v]);
    }
    if(low[u]==dfn[u])//染色
    {
        sig++;
        do
        {
            vis[stk[sp]]=-1;
            color[stk[sp]]=sig;
        }while(stk[sp--]!=u);
    }
}

int f(int u,int x)//u點到顏色x的點中的最小的權值
{
    int minv=INF;
    for(int j=0;j<G[u].size();j++)
    {
        int v=G[u][j].v;
        if(color[v]==x)
        {
            minv=min(minv,G[u][j].w);
        }
    }
    return minv;
}
int ff(int x,int y)//判斷有沒有x到y的邊
{
    for(int j=0;j<G[x].size();j++)
    {
        if(G[x][j].v==y)
            return 1;
    }
    return 0;
}
int main()
{
    int x,y,z;
    while(~scanf("%d %d",&n,&m))
    {
        init();
        for(int i=1;i<=m;i++)
        {
            scanf("%d %d %d",&x,&y,&z);
            x++,y++;
            if(ff(x,y)==0)//沒有x到y的邊
            {
                G[x].push_back(node(y,z));
            }else
            {
                if(G[x][y].w>z)//有x到y的邊,但是存在權更小的邊,替換
                    G[x].push_back(node(y,z));
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
                tarjan(i);
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<G[i].size();j++)
            {
                int v=G[i][j].v;
                if(color[i]!=color[v])
                {
                    //維護每個點的最小入邊,最小樹形圖演算法
                    a[color[v]]=min(a[color[v]],f(i,color[v]));
                }
            }
        }
        //所有點的最小入邊之和就是最小樹形圖的權值和(有一個點沒有入邊)
        for(int i=1;i<=sig;i++)
        {
            if(a[i]<INF)
                ans+=a[i];
        }
        printf("%lld\n",ans);
    }
    return 0;
}
/*
題目意思:
n個點,m條邊的有向圖,x y w表示x到y的花費是w
但是一個強連通分量內的點互相到達的花費是0
問你要到達所有點的最小花費
題目保證圖是連通的,且存在一個點可以到所有點

分析:
存在強連通分量,強連通內的點互相到達花費為0,可以看成一個點
但是需要注意貪心一下
比如1到強連通分量x
應該選擇1到強連通分量x的所有點中權值最小的邊
縮點和貪心之後
圖變成了一個DAG圖,有向無環圖
然後就是求這個DAG圖的最小樹形圖的值

這裡的求發很巧妙,因為題目保證了圖的連通性
所有我們只需要維護所有點的最小入邊的權值就好
但是有個點是沒有入邊的
(從此點出發,可以到達所有點)

*/