链接: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. matlab之细胞数组

    学习matlab的一个博客:https://blog.csdn.net/smf0504/article/details/51814362 Matlab从5.0版开始引入了一种新的数据类型—细胞( ce ...

  2. 使用libcurl,根据url下载对应html页面

    1. [图片] Capture.JPG ​2. [代码]GetPageByURL //static member variable definestring GetPageByURL::m_curPa ...

  3. 截取带HTML标签的文本并保留文本样式

    一个截取HTML文本的工具,可以按照文字字数或文字字节长度进行截取,保留HTML样式并在最后自动补齐截取后的标签.按工作要求编写,时间紧迫,代码未优化,欢迎讨论和指正.​1. [文件] SubHtml ...

  4. 《java编程思想》读后笔记:一,标签

    标签 是后面跟有冒号的标识符,格式如下: label : java中通过break与continue关键词可以完成类似于跳转的操作,其实现机制便是标签. 虽然很少有人使用,但是其有自身的适用场景:多层 ...

  5. 常用SASS封装

    结合Compass库和工作总结,列出了项目中最为常用的SASS片段.内容收集于网络,我进行了简单整理并测试正常,可以根据实际项目情况进行取舍,值得学习或直接应用,感谢! //重置浏览器默认样式@imp ...

  6. zabbix告警邮件美化

    为了更好的用户体验,我们需要尽量美化我们的输出内容,尽量做到整齐划一,让人看了会有很舒服的感觉, 这个好像和苹果的产品一样,给人一种美感让人感觉非常享受. 一般我们的zabbix告警邮件就是纯文字,建 ...

  7. AtCoder Grand Contest #026 C - String Coloring

    Time Limit: 3 sec / Memory Limit: 1024 MB Score : 600600 points Problem Statement You are given a st ...

  8. poj2392磊石头——排序后背包

    题目: 首先按限制高度从小到大排序,不会影响可行解,而不排序可能卡掉正确的情况: 用%2滚动数组时一定注意每次复制上一种情况,因为这个WA了好几次. 代码如下: #include<iostrea ...

  9. AI-Info-Micron-Solutions-Menu:Solutions

    ylbtech-AI-Info-Micron-Solutions-Menu:Solutions 1.返回顶部 1. 按应用分类 汽车解决方案 美光科技不仅是你的存储提供商,更是你的长期合作伙伴.我们提 ...

  10. docker 学习(七) docker 容器挂载

    1:docker的默认存放位置: $ sudo su # cd /var/lib/docker # ls -F containers/ graph/ repositories volumes/     ...