题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3072

题目大意:为一个有向连通图加边。使得整个图全连通,有重边出现。

解题思路

先用Tarjan把强连通分量缩点。

由于整个图肯定是连通的,所以枚举每一条边,记录到非0这个点所在连通分量的最小cost。

一共需要累加cnt-1个连通分量的cost。

在Tarjan过程中的重边,可以用链式前向星结构解决。(vector邻接表会算错)

在枚举每条边的cost中,用cost[i]记录i这个连通分量的最小cost。

最后不要算上cost[scc[0]],因为0点所在的连通分量是免费的。

#include "cstdio"
#include "algorithm"
#include "stack"
#include "cstring"
using namespace std;
#define maxn 50005
#define LL long long
int head[maxn],tot,pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,cnt,cost[maxn];
stack<int> S;
struct Edge
{int to,next,c;
}e[100005];
void addedge(int u,int v,int c)
{e[tot].to=v;e[tot].next=head[u];e[tot].c=c;head[u]=tot++;
}
void tarjan(int u)
{pre[u]=lowlink[u]=++dfs_clock;S.push(u);for(int i=head[u];i!=-1;i=e[i].next){int v=e[i].to;if(!pre[v]){tarjan(v);lowlink[u]=min(lowlink[u],lowlink[v]);}else if (!sccno[v]) lowlink[u]=min(lowlink[u],lowlink[v]);}if(lowlink[u]==pre[u]){cnt++;while(1){int x=S.top();S.pop();sccno[x]=cnt;if(x==u) break;}}
}
int main()
{//freopen("in.txt","r",stdin);int n,m,u,v,c;while(scanf("%d%d",&n,&m)!=EOF){memset(head,-1,sizeof(head));memset(pre,0,sizeof(pre));memset(lowlink,0,sizeof(lowlink));memset(sccno,0,sizeof(sccno));memset(cost,0x3f3f,sizeof(cost));tot=dfs_clock=cnt=0;for(int i=0;i<m;i++){scanf("%d%d%d",&u,&v,&c);addedge(u,v,c);}for(int i=0;i<n;i++)if(!pre[i]) tarjan(i);LL sum=0;for(int i=0;i<n;i++)for(int j=head[i];j!=-1;j=e[j].next)if(sccno[i]!=sccno[e[j].to]) cost[sccno[e[j].to]]=min(cost[sccno[e[j].to]],e[j].c);for(int i=1;i<=cnt;i++)if(i!=sccno[0])sum+=cost[i];printf("%I64d\n",sum);}
}

HDU 3072 (强连通分量)相关推荐

  1. HDU 4635(强连通分量分解

    题目:给出一个有向图,要求添加最多的边数,使得图仍然不强连通. 思路:首先这个图在添加边之后肯定变成了两个强连通分量,现在就看怎么分.然后我们可以注意到,原图进行强连通分量分解之后必然存在一些分量的出 ...

  2. UVa 12167 HDU 2767 强连通分量 Proving Equivalences

    题意:给出一个有向图,问最少添加几条有向边使得原图强连通. 解法:求出SCC后缩点,统计一下出度为0的点和入度为0的点,二者取最大值就是答案. 还有个特殊情况就是本身就是强连通的话,答案就是0. 1 ...

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

    Intelligence System HDU - 3072 题意:一个人要传递命令给所有人,如果两人之间互达,不需任何费用,求最少费用 有向图强连通. 1 #include <bits/std ...

  4. hdu 5420 Victor and Proposition 线段树建图+强连通分量

    题意: http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=620&pid=1003 题目求有多少对互为充要 ...

  5. HDU 4685 Prince and Princess(二分图+强连通分量)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4685 题意:给出n个王子和m个公主.每个王子有一些自己喜欢的公主可以匹配.设最大匹配为M.那么对于每个 ...

  6. 有向图 加最少的边 成为强连通分量的证明 poj 1236 hdu 2767

    poj 1236: 题目大意:给出一个有向图, 任务一: 求最少的点,使得从这些点出发可以遍历整张图  任务二: 求最少加多少边 使整个图变成一个强连通分量. 首先任务一很好做, 只要缩点 之后 求 ...

  7. hdu 1269(Tarjan求强连通分量)

    这道题目就是求强连通分量... 这里采用的是Tarjan算法:http://m.blog.csdn.net/blog/qq574857122/16361033 AC代码: #include<io ...

  8. Strongly connected HDU - 4635(tarjan+强连通分量)

    题意: 给一个简单有向图,让你加最多的边,使他还是一个简单有向图. 题目: Give a simple directed graph with N nodes and M edges. Please ...

  9. HDU 5934:Boom——强连通分量+缩点

    [题目描述] There are N bombs needing exploding.Each bomb has three attributes: exploding radius ri, posi ...

  10. 【HDU - 4635】Strongly connected(缩点,新图性质,建图,Tarjan求强连通分量)

    题干: Give a simple directed graph with N nodes and M edges. Please tell me the maximum number of the ...

最新文章

  1. SSL应用系列之二:为Web站点实现SSL加密访问
  2. Git命令集之八——提交命令
  3. html5 内嵌网页_如何分析并优化网页的性能?新梦想软件测试
  4. centos7安装php5.2yum源操作_CentOS7使用阿里yum源进行升级和安装php70W
  5. java中getlast_Java ArrayDeque getLast()用法及代码示例
  6. 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList
  7. Mimics三维建模
  8. C++数据库编程 ODBC连接SQL Server数据库
  9. 住宅内部通信系统市场现状研究分析与发展前景预测报告
  10. 服务都微了,编排怎么整?
  11. findContours函数报错:“将一个无效参数传递给了将无效参数视为严重错误的函数”解决方案
  12. 华为路由器交换机命令汇总-持续更新
  13. 审计大数据综合分析采集管理系统软件平台
  14. chrome无法打开无痕模式的解决方案
  15. MIT Technology Review 2020年“十大突破性技术”解读 【中国科学基金】2020年第3期发布...
  16. Codeforces div2 D. Madoka and The Corruption Scheme
  17. 计算机世界第一人—艾兰·图灵
  18. 阿帕奇(apache)服务器的下载与安装
  19. P1914 小书童——凯撒密码
  20. 收藏!教育部发布67家出版社中小学春季教材电子版!网址都在这里,一键获取!...

热门文章

  1. bash给脚本加进度条_shell脚本进度条。
  2. 【GNN框架系列】DGL第一讲:使用Deep Graph Library实现GNN进行节点分类
  3. [LeetCode][easy]Create Target Array
  4. SMO算法是干什么的?有什么作用?
  5. TF-IDF来源及理论推导 熵推导出
  6. 运营效果分析:假设检验
  7. 【 Codeforces Round #552 (Div. 3) G】Minimum Possible LCM【埃氏筛】
  8. 快速突破面试算法之字符串表篇
  9. 计算机网络怎样连手机软件,玩够了手机投屏?了解一下手机如何直接连智能电视...
  10. mysql 多主一从备份_Linux下安装Mysql多实例作为数据备份服务器实现多主到一从多实例的备份 | 很文博客...