题目链接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[];
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!=-;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()
{
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,-,sizeof(head));
memset(pre,,sizeof(pre));
memset(lowlink,,sizeof(lowlink));
memset(sccno,,sizeof(sccno));
memset(cost,0x3f3f,sizeof(cost));
tot=dfs_clock=cnt=;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&u,&v,&c);
addedge(u,v,c);
}
for(int i=;i<n;i++)
if(!pre[i]) tarjan(i);
LL sum=;
for(int i=;i<n;i++)
for(int j=head[i];j!=-;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=;i<=cnt;i++)
if(i!=sccno[])
sum+=cost[i];
printf("%I64d\n",sum);
}
}

HDU 3072 (强连通分量)的更多相关文章

  1. hdu 4685(强连通分量+二分图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4685 题意:n个王子和m个公主,王子只能和他喜欢的公主结婚,公主可以和所有的王子结婚,输出所有王子可能 ...

  2. hdu 4685(强连通分量+二分图的完美匹配)

    传送门:Problem 4685 https://www.cnblogs.com/violet-acmer/p/9739990.html 参考资料: [1]:二分图的最大匹配.完美匹配和匈牙利算法 [ ...

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

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

  4. hdu 3072 强连通+缩点+最小树形图思想

    #include<stdio.h> #include<string.h> #define N 51000 #define inf 1000000000 struct node ...

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

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  6. hdu 4685 二分匹配+强连通分量

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4685 题解: 这一题是poj 1904的加强版,poj 1904王子和公主的人数是一样多的,并且给出 ...

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

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

  8. HDU 1269 迷宫城堡(判断有向图强连通分量的个数,tarjan算法)

    迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  9. HDU 3861 The King’s Problem(强连通分量+最小路径覆盖)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意: 在csdn王国里面, 国王有一个新的问题. 这里有N个城市M条单行路,为了让他的王国 ...

随机推荐

  1. velocity思维导图笔记

  2. Eclipse的各种快捷键

     参考他人总结好的 Eclipse快捷键大全(转载) Ctrl+1 快速修复(最经典的快捷键,就不用多说了)     ---->例如:add unimplement methodCtrl+D:  ...

  3. C#学习笔记----栈与堆的知识

    http://my.oschina.net/lichaoqiang/blog/291906 当我们对.NET Framework的一些基本面了解之后,实际上,还是很有必要了解一些更底层的知识.比如.N ...

  4. python threading编程中的LOCK和RLOCK(可重入锁)

    找到一本PYTHON并发编辑的书, 弄弄.. #!/usr/bin/env python # -*- coding: utf-8 -*- import threading import time sh ...

  5. 【131202】SQL

    SELECT TOP 2 * FROM Persons SELECT *FROM PersonsWHERE ROWNUM <= 5   SELECT * FROM Persons WHERE C ...

  6. 算法系列:geometry

    1.基本几何变换及变换矩阵 基本几何变换都是相对于坐标原点和坐标轴进行的几何变换,有平移.比例.旋转.反射和错切等. 1.1 平移变换 是指将p点沿直线路径从一个坐标位置移到另一个坐标位置的重定位过程 ...

  7. java BigInteger使用

    虽然说我不怎么会用java写一些东西,但是java里的biginteger我还是很喜欢的.这个类解决了,我们在其他很多语言中遇到的问题:大数.比如在C++中我们要计算一个大小超过long long的数 ...

  8. Codeforces Testing Round #12 C. Subsequences 树状数组

    C. Subsequences     For the given sequence with n different elements find the number of increasing s ...

  9. barabasilab-networkScience学习笔记1-网络科学简介

    第一次接触复杂性科学是在一本叫think complexity的书上,Allen博士很好的讲述了数据结构与复杂性科学,barabasi是一个知名的复杂性网络科学家,barabasilab则是他所主导的 ...

  10. java集群技术(转)

    来源:http://blog.csdn.net/cdh1213/article/details/21443239 序言 越来越多的关键应用运行在J2EE(Java2, Enterprise Editi ...