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条单行路,为了让他的王国 ...
随机推荐
- 三、jQuery--jQuery基础--jQuery基础课程--第10章 jQuery UI型插件
1.拖曳插件——draggable 拖曳插件draggable的功能是拖动被绑定的元素,当这个jQuery UI插件与元素绑定后,可以通过调用draggable()方法,实现各种拖曳元素的效果,调用格 ...
- vim 查找时忽略大小写
:set ic 忽略大小写#ignorecase :set noic 不忽略大小写#noignorecase
- 数据结构和算法 – 11.高级排序算法(下)
三.选择类排序 3.1.简单选择排序 http://www.cnblogs.com/tangge/p/5338734.html#XuanZe 3.2 堆排序 要知道堆排序,首先要了解一下二叉树的模型. ...
- [LeetCode] Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- list[C++]
//双向链表 #include <iostream> using namespace std; #include <list> int main(int argc, const ...
- 用ajax和js怎么做出滚动条滚到最下面分页
获取滚动条位置(scrollTop) 获取可视窗口高度(viewportHeight) 获取整个页面可滚动高度(scrollHeight) 当scrollTop+viewportHeight==scr ...
- POJ 2464 Brownie Points II(树状数组)
一开始还以为对于每根竖线,只要与过了任意一点的横线相交都可以呢,这样枚举两条线就要O(n^2),结果发现自己想多了... 其实是每个点画根竖线和横线就好,对于相同竖线统计(一直不包含线上点)右上左下总 ...
- 通过jquery.transit.min.js插件,实现图片的移动
首先给出插件:jquery.transit.min.js (function(t,e){if(typeof define==="function"&&define. ...
- jsp遍历、循环
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 1. <% Te ...
- 分数try catch
要求:编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”.“及格”.“中”.“良”.“优”的结论.要求程序必须具备足够的健壮性,不管用户输入什 么样的内容 ...