HDU 3072 (强连通分量)
题目链接: 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 (强连通分量)的更多相关文章
- hdu 4685(强连通分量+二分图)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4685 题意:n个王子和m个公主,王子只能和他喜欢的公主结婚,公主可以和所有的王子结婚,输出所有王子可能 ...
- hdu 4685(强连通分量+二分图的完美匹配)
传送门:Problem 4685 https://www.cnblogs.com/violet-acmer/p/9739990.html 参考资料: [1]:二分图的最大匹配.完美匹配和匈牙利算法 [ ...
- UVa 12167 & HDU 2767 强连通分量 Proving Equivalences
题意:给出一个有向图,问最少添加几条有向边使得原图强连通. 解法:求出SCC后缩点,统计一下出度为0的点和入度为0的点,二者取最大值就是答案. 还有个特殊情况就是本身就是强连通的话,答案就是0. #i ...
- hdu 3072 强连通+缩点+最小树形图思想
#include<stdio.h> #include<string.h> #define N 51000 #define inf 1000000000 struct node ...
- HDU 3072 Intelligence System (强连通分量)
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 4685 二分匹配+强连通分量
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4685 题解: 这一题是poj 1904的加强版,poj 1904王子和公主的人数是一样多的,并且给出 ...
- HDU 4685 Prince and Princess(二分图+强连通分量)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4685 题意:给出n个王子和m个公主.每个王子有一些自己喜欢的公主可以匹配.设最大匹配为M.那么对于每个 ...
- HDU 1269 迷宫城堡(判断有向图强连通分量的个数,tarjan算法)
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HDU 3861 The King’s Problem(强连通分量+最小路径覆盖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意: 在csdn王国里面, 国王有一个新的问题. 这里有N个城市M条单行路,为了让他的王国 ...
随机推荐
- echarts基本使用
基本操作: 1,准备好需要渲染chart图的div层 <div id="org-data-percent" class="org-data-percent" ...
- sql server 的cpu使用率过高的分析
有哪些SQL语句会导致CPU过高? 1.编译和重编译 编译是 Sql Server 为指令生成执行计划的过程.Sql Server 要分析指令要做的事情,分析它所要访问的表格结构,也就是生成执行计划的 ...
- Mongo DB Study: first face with mongo DB
Mongo DB Study: first face with mongo DB 1. study methods: 1. Translate: I am the mongo DB organiz ...
- java中常用的工具类(三)
继续分享java中常用的一些工具类.前两篇的文章中有人评论使用Apache 的lang包和IO包,或者Google的Guava库.后续的我会加上的!谢谢支持IT江湖 一.连接数据库的综合类 ...
- lvs+keepalived 负载均衡
LVS是一个开源的软件,可以实现LINUX平台下的简单负载均衡.LVS是Linux Virtual Server的缩写,意思是Linux虚拟服务器.目前有三种IP负 载均衡技术(VS/NAT.VS/T ...
- hdu 2232 矩阵 ***
一天四个不同的机器人a.b.c和d在一张跳舞毯上跳舞,这是一张特殊的跳舞毯,他由4个正方形毯子组成一个大的正方形毯子,一开始四个机器人分别站在4 块毯子上,舞蹈的每一步机器人可以往临近(两个毯子拥有同 ...
- 第二篇:SOUI源码的获取及编译
源代码的获取 SOUI的源码采用SVN管理. SVN:http://code.taobao.org/svn/soui2 这里主要包含两个目录:trunk 及 third-part. trunk目录保存 ...
- Codeforces Round #161 (Div. 2) D. Cycle in Graph(无向图中找指定长度的简单环)
题目链接:http://codeforces.com/problemset/problem/263/D 思路:一遍dfs即可,dp[u]表示当前遍历到节点u的长度,对于节点u的邻接点v,如果v没有被访 ...
- Arduino101学习笔记(十二)—— 101定时器中断
一.API 1.开定时器中断 //*********************************************************************************** ...
- LVS 三种工作模式原理、以及优缺点比较(转载)
原文地址:http://9ilinux.com/149.html 一.NAT模式(VS-NAT) 原理:就是把客户端发来的数据包的IP头的目的地址,在负载均衡器上换成其中一台RS的IP地址,并发至此R ...