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

大意:在一个联通分量里面的边权值忽略不计,求缩点后的所有联通分量链接在一起的最小权值和。

思路:必然先缩点,最后得到的图为DAG,然后就是统计权值,对于那么到达当前的缩点可能有多个入度,所以选择最小的即可。最后避开起始点的dis[star] == inf.累加即可。

#include<map>
#include<queue>
#include<cmath>
#include<cstdio>
#include<stack>
#include<iostream>
#include<cstring>
#include<algorithm>
#define LL int
#define inf 0x3f3f3f3f
#define eps 1e-8
#include<vector>
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
#define LL __int64
using namespace std;

const int nv = 50100;
const int ne = 101000;
struct node{
    int to,w,next;
}q[ne<<1];

const int Ma = nv;
int head[ne<<1],dfn[Ma],num[Ma],du[Ma],stk[Ma],vis[Ma],low[Ma];
int cnt,top,tim,scc,dis[Ma];
LL ans;

void Add(int a,int b,int c){
    q[cnt].to = b;
    q[cnt].w = c;
    q[cnt].next = head[a];
    head[a] = cnt++;
}

void init(){
    ans = scc =  cnt = top = 0;
    tim =  1;
    memset(head,-1,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    memset(num,0,sizeof(num));
    memset(vis,0,sizeof(vis));
    memset(low,0,sizeof(low));
}

void Tarjan(int u){
    low[u] = dfn[u] = tim++;
    vis[u] = 1;
    stk[top++] = u;
    for(int i = head[u]; ~i ; i = q[i].next){
        int v = q[i].to;
        if(!dfn[v]){
            Tarjan(v);
            low[u] = min(low[u],low[v]);
        }
        else if(vis[v])
            low[u] = min(low[u],dfn[v]);
    }

    if(low[u] == dfn[u]){
        scc++;
        while(top > 0&&stk[top] != u){
            top --;
            vis[stk[top] ] = 0;
            num[stk[top] ] = scc;
        }
    }
}

int main(){
    int n,m,i,j,k,a,b,c,cla;
    while(~scanf("%d%d",&n,&m)){
        init();
        for(i = 0;i <m;++ i){
            scanf("%d%d%d",&a,&b,&c);
            Add(a,b,c);
        }

        for(i = 0;i < n;++ i)
            if(!dfn[i])
                Tarjan(i);

        memset(dis,inf,sizeof(dis));
        for(i = 0;i < n;++ i)
            for(j = head[i];~j;j=q[j].next){
                int v = q[j].to;
                if(num[i] != num[v]){
                    dis[ num[v] ] = min(dis[num[v] ],q[j].w);
                }
            }

        for(i = 1;i <= scc;++ i){
            if(dis[i]!=inf)
                ans+=dis[i];
        }
        printf("%I64d\n",ans);

    }
    return 0;
}
时间: 2024-11-06 03:51:27

HDU 3072 Intelligence System (强连通+(贪心||树形图))的相关文章

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

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

HDU 3072 Intelligence System (强连通分量)

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

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

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

[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——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

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

Intelligence System HDU - 3072(强连通分量)

Intelligence System HDU - 3072 题意:一个人要传递命令给所有人,如果两人之间互达,不需任何费用,求最少费用 有向图强连通. 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int inf=0x3f3f3f3f; 4 const int maxv=50010; 5 const int maxe=100010; 6 int n,m; 7 struct Edge{ 8 int u,v,w; 9 int

Intelligence System (hdu 3072 强联通缩点+贪心)

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