链接:https://vjudge.net/problem/HDU-3072

题意:

给你n个点,1个点到另一个点连接花费c,但是如果几个点可以相互可达,则这几个点连通花费为0.

求将整个图连通的最小花费。

思路:

tarjan,求出强连通子图。

对每个子图的进点的最小值更新,再累加即可,(不过不知道为什么)

代码:

#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
#include <set>
#include <iterator>
#include <cstring>
using namespace std; typedef long long LL;
const int MAXN = 5e4+10;
const int INF = 0x3f3f3f3f; struct Node
{
int from_, to_, value_;
Node(int from, int to, int value):from_(from), to_(to), value_(value){}
bool operator < (const Node &that) const
{
return this->value_ > that.value_;
}
}; vector<Node> G[MAXN];
stack<int> St;
int Dfn[MAXN], Low[MAXN];
int Vis[MAXN], Dis[MAXN];
int Fa[MAXN], Val[MAXN];
int Num[MAXN];
int n, m;
int times, cnt; void Init()
{
for (int i = 1;i <= n;i++)
G[i].clear(), Fa[i] = i;
memset(Dfn, 0, sizeof(Dfn));
memset(Low, 0, sizeof(Low));
memset(Vis, 0, sizeof(Vis));
memset(Dis, 0, sizeof(Dis));
memset(Num, 0, sizeof(Num));
memset(Val, 0, sizeof(Val));
times = cnt = 0;
} void Tarjan(int x)
{
Dfn[x] = Low[x] = ++times;
Vis[x] = 1;
St.push(x);
for (int i = 0;i < G[x].size();i++)
{
int node = G[x][i].to_;
if (Dfn[node] == 0)
{
Tarjan(node);
Low[x] = min(Low[x], Low[node]);
}
else if (Vis[node] == 1)
Low[x] = min(Low[x], Dfn[node]);
}
if (Low[x] == Dfn[x])
{
cnt++;
Num[cnt] = 0;
while (St.top() != x)
{
Num[cnt]++;
Fa[St.top()] = cnt;
Vis[St.top()] = 0;
St.pop();
}
Num[cnt]++;
Fa[St.top()] = cnt;
Vis[St.top()] = 0;
St.pop();
}
} int main()
{
int t, cn = 0;
while (~scanf("%d%d", &n, &m))
{
Init();
int l, r, v;
for (int i = 1;i <= m;i++)
{
scanf("%d%d%d", &l, &r, &v);
l++, r++;
G[l].emplace_back(l, r, v);
}
for (int i = 1;i <= n;++i)
if (!Dfn[i])
Tarjan(i);
for (int i = 1;i <= cnt;i++)
Val[i] = INF;
for (int i = 1;i <= n;i++)
{
for (int j = 0;j < G[i].size();j++)
{
int node = G[i][j].to_;
if (Fa[i] != Fa[node])
Val[Fa[node]] = min(Val[Fa[node]], G[i][j].value_);
}
}
int res = 0;
for (int i = 1;i <= cnt;i++)
if (Val[i] != INF)
res += Val[i];
cout << res << endl;
} return 0;
}

  

HDU-3072-IntelligenceSystem(tarjan,贪心)的更多相关文章

  1. HDU 4442 Physical Examination(贪心)

    HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...

  2. HDU 3072 Intelligence System(tarjan染色缩点+贪心+最小树形图)

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

  3. Intelligence System (hdu 3072 强联通缩点+贪心)

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

  4. hdu 3072 Intelligence System(Tarjan 求连通块间最小值)

    Intelligence System Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  5. HDU 3072 (强连通分量)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3072 题目大意:为一个有向连通图加边.使得整个图全连通,有重边出现. 解题思路: 先用Tarjan把 ...

  6. hdu 3072

    强连通分量——tarjin 算法 这道题和前面那道hdu 2767唯一不同就是,2767需要找出最小数量的边使图成为连通分量,而这个题需要一点点贪心的思想在里面,它需要求出代价最小的边使图成为连通分量 ...

  7. HDU 5835 Danganronpa (贪心)

    Danganronpa 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5835 Description Chisa Yukizome works as ...

  8. HDU 5821 Ball (贪心)

    Ball 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...

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

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

  10. hdu 4004 (二分加贪心) 青蛙过河

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4004 题目意思是青蛙要过河,现在给你河的宽度,河中石头的个数(青蛙要从石头上跳过河,这些石头都是在垂 ...

随机推荐

  1. HTML5响应式模版Mocha

    HTML5响应式模版Mocha,经典,html5,蓝色,扁平,HTML5响应式模版Mocha是一款宽屏大气的HTML5网站展示模板. http://www.huiyi8.com/moban/

  2. Android窗口系统第二篇---Window的添加过程

    以前写过客户端Window的创建过程,大概是这样子的.我们一开始从Thread中的handleLaunchActivity方法开始分析,首先加载Activity的字节码文件,利用反射的方式创建一个Ac ...

  3. app测试点-1

    一.安全测试 1.软件权限 1)扣费风险:包括短信.拨打电话.连接网络等. 2)隐私泄露风险:包括访问手机信息.访问联系人信息等. 3)对App的输入有效性校验.认证.授权.数据加密等方面进行检测 4 ...

  4. Mysql处理字符串函数(转)

    http://www.jb51.net/article/27458.htm 感觉上MySQL的字符串函数截取字符,比用程序截取(如PHP或JAVA)来得强大,所以在这里做一个记录,希望对大家有用. 函 ...

  5. stack_2.由两个栈组成队列

    思路: 用两个栈($stack_a, $stack_b),当push的时候,压入$stack_a, 让pop的时候,先把$stack_a元素依次全部倒入$stack_b中,再对$stack_b进行po ...

  6. bzoj 2850: 巧克力王国 K-D树

    题目大意 http://www.lydsy.com/JudgeOnline/problem.php?id=2850 题解 对于每个人,我们发现它能够接受的巧克力中 如果对参数分别讨论,那么一定是一个连 ...

  7. AI-Info-Micron-Insight:高速数据:第四次工业革命的助推引擎

    ylbtech-AI-Info-Micron-Insight:高速数据:第四次工业革命的助推引擎 1.返回顶部 1. 高速数据:第四次工业革命的助推引擎 第四次工业革命已然来临,因为数字技术几乎连接了 ...

  8. Java中的数组和方法

    3.1 数组的定义和使用 数组(Array)是用来存储一组相同数据类型数据的集合.数组中的每个数据称为一个元素(element),数组可以分为一维数组,二维数组和多维数组.我们 主要讲解一维数组和二维 ...

  9. jQuery 防止相同的事件快速重复触发

    重复触发就是防止用户重复点击提交数据了,我们一般都是点击之后没反应会再次点击了,这个不但要从用户体验上来做好,还在要js或php程序脚本上做好,让用户知道点击是己提交服务器正在处理,下面我就整理从脚本 ...

  10. 选择排序(java)

    选择排序的思想是:内外两层循环,第一层循环从第一个数开始到倒数第一个数, 第二层循环从上一层的数开始, 与上一层循环的数比较,如果小于则交换位置. 代码如下: public class SelectS ...