题目链接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. AngularJS模型 ng-model 指令

    ng-model 指令用于绑定应用程序数据到 HTML 控制器(input, select, textarea)的值. ng-model 指令可以将输入域的值与 AngularJS 创建的变量绑定.例 ...

  2. ISD1700系列多段语音录放系列

    ISD1700系列语音芯片的基础指示:

  3. jquery学习笔记----ajax使用

    一.load() 加载页面数据 load(url,[data],[callback]) url:加载的页面地址,[data]传送的数据,[callback]加载完成时回调函数. 设计一个load.ht ...

  4. 数据结构和算法 – 8.链表

    8.1.数组存在的问题 在处理列表的时候数组是常用的数据结构.数组可以对所存储的数据项提供快速地存取访问,而且它很易于进行循环遍历操作.当然,数组已经是语言的一部分了,用户不需要使用额外的内存,也不需 ...

  5. C#的LINQ to Object

    using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...

  6. 无废话Android之内容观察者ContentObserver、获取和保存系统的联系人信息、网络图片查看器、网络html查看器、使用异步框架Android-Async-Http(4)

    1.内容观察者ContentObserver 如果ContentProvider的访问者需要知道ContentProvider中的数据发生了变化,可以在ContentProvider 发生数据变化时调 ...

  7. Asyncio中的Task管理

    #!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio import datetime import time from random ...

  8. Install PIL on mac osX10.9

    follow this instruction: http://blog.csdn.net/liushuaikobe/article/details/8729652 and if you encoun ...

  9. POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树

    题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...

  10. Storyboards

    这里是吐槽时间,换掉了mac默认的输入法,出发点只有一个,就是切换中英文输入的时候相当不爽.也许是习惯了其他各大输入法的一键切换,而又没有找到自带输入法可设置的地方. Segue 以前我们使用navi ...