hdu 3072 Intelligence System(Tarjan 求连通块间最小值)
Intelligence System
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 2 Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Now, kzc_tc, the head of the Intelligence Department (his code is once 48, but now 0), is sudden obtaining important information from one Intelligence personnel. That relates to the strategic direction and future development of the situation of ALPC. So it need for emergency notification to all Intelligence personnel, he decides to use the intelligence system (kzc_tc inform one, and the one inform other one or more, and so on. Finally the information is known to all).
We know this is a dangerous work. Each transmission of the information can only be made through a fixed approach, from a fixed person to another fixed, and cannot be exchanged, but between two persons may have more than one way for transferring. Each act of the transmission cost Ci (1 <= Ci <= 100000), the total cost of the transmission if inform some ones in our ALPC intelligence agency is their costs sum.
Something good, if two people can inform each other, directly or indirectly through someone else, then they belong to the same branch (kzc_tc is in one branch, too!). This case, it’s very easy to inform each other, so that the cost between persons in the same branch will be ignored. The number of branch in intelligence agency is no more than one hundred.
As a result of the current tensions of ALPC’s funds, kzc_tc now has all relationships in his Intelligence system, and he want to write a program to achieve the minimum cost to ensure that everyone knows this intelligence.
It's really annoying!
Input
In each case, the first line is an Integer N (0< N <= 50000), the number of the intelligence personnel including kzc_tc. Their code is numbered from 0 to N-1. And then M (0<= M <= 100000), the number of the transmission approach.
The next M lines, each line contains three integers, X, Y and C means person X transfer information to person Y cost C.
Output
Believe kzc_tc’s working! There always is a way for him to communicate with all other intelligence personnel.
Sample Input
3 3
0 1 100
1 2 50
0 2 100
3 3
0 1 100
1 2 50
2 1 100
2 2
0 1 50
0 1 100
Sample Output
150
100
50
Source
题意:简单点说就是求把所有强连通分量连在一起所需的最小花费
解析:先把所有强连通分量求出来,再求不同连通分量连接起来的最小花费,最后把除0所在的连通分量所需的最小花费连接起来
#include <bits/stdc++.h>
using namespace std;
const int inf=0x7fffffff;
int n,T,m,index,team_num;
int low[],dfn[],team[],x[],y[],z[],co[];
bool instack[];
stack<int> S;
vector<int> mp[];
void Tarjan(int u)
{
low[u]=dfn[u]=++index;
S.push(u);
instack[u]=;
for(int i=;i<mp[u].size();i++)
{
int v=mp[u][i];
if (!dfn[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if (instack[v]) low[u]=min(low[u],dfn[v]);
}
if (dfn[u]==low[u])
{
team_num++;
while()
{
int v=S.top(); S.pop();
instack[v]=;
team[v]=team_num;
if (u==v) break;
}
}
}
void dfs()
{
//memset(in,0,sizeof(in));
//memset(out,0,sizeof(out));
memset(instack,,sizeof(instack));
memset(team,,sizeof(team));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
team_num=;
index=;
for(int i=;i<=n;i++)
if (!dfn[i]) Tarjan(i);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<=n;i++) mp[i].clear();
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&x[i],&y[i],&z[i]);
x[i]++; y[i]++; // 把标号改成从0~n
mp[x[i]].push_back(y[i]);
}
dfs(); for(int i=;i<=team_num;i++) co[i]=inf;
for(int i=;i<=m;i++)
{
if (team[]==team[y[i]] || team[x[i]]==team[y[i]])continue;
co[team[y[i]]]=min(co[team[y[i]]],z[i]);
}
int sum=;
for(int i=;i<=team_num;i++)
if(co[i]!=inf) sum+=co[i];
printf("%d\n",sum);
}
return ;
}
hdu 3072 Intelligence System(Tarjan 求连通块间最小值)的更多相关文章
- HDU 3072 Intelligence System(tarjan染色缩点+贪心+最小树形图)
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdoj 3072 Intelligence System【求scc&&缩点】【求连通所有scc的最小花费】
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU——3072 Intelligence System
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 3072 Intelligence System (强连通分量)
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Coconuts HDU - 5925 (二维离散化求连通块的个数以及大小)
题目链接: D - Coconuts HDU - 5925 题目大意:首先是T组测试样例,然后给你n*m的矩阵,原先矩阵里面都是白色的点,然后再输入k个黑色的点.这k个黑色的点可能会使得原先白色的点 ...
- HDU - 3072 Intelligence System
题意: 给出一个N个节点的有向图.图中任意两点进行通信的代价为路径上的边权和.如果两个点能互相到达那么代价为0.问从点0开始向其余所有点通信的最小代价和.保证能向所有点通信. 题解: 求出所有的强连通 ...
- DFS入门之二---DFS求连通块
用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...
- HDU1241 Oil Deposits —— DFS求连通块
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...
- P1197 [JSOI2008]星球大战 [删边求连通块个数]
展开 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的 ...
随机推荐
- SQLSERVER 内存占用高的处理方式
https://www.cnblogs.com/srsrd/p/6962982.html 方法一: 方法二: 使用以下语句查找出什么语句占内存最高,针对占内存高的语句进行优化SELECT SS.SUM ...
- 《EMCAScript6入门》读书笔记——24.编程风格
- Unity3D学习笔记(十四):Animation旧动画
animator(新动画系统):骨骼动画,骨骼驱动,格式化编辑,动画机图形化 animation(旧动画系统):物理系统,帧动画 一.如何建立动画文件 Animation Clip 手动添加动 ...
- The way to Go(4): Go runtime及解释器
Reference: Github: Go Github: The way to Go Go runtime Go runtime: 尽管 Go 编译器产生的是本地可执行代码,这些代码仍旧运行在 Go ...
- Elasticsearch 原理
Elasticsearch简介 Elasticsearch是一个基于Apache lucene的实时分布式搜索.具有以下优点: 1.实时处理大规模数据.2.全文检索,能够做到结构化检索和聚合分析.3. ...
- SqlParameter 参数化模糊查询
sql += " and a.f_fullName like N'%'+@fullName+'%'";
- RTC(x86)
RTC 原创,转载请写明出处. 一直以来想写一篇关于RTC的总结,可是人太懒,在读完John Z. Sonmez大伽的<软技能代码之外的生存技能>后,终于下定决心,完成这项早已计划中的任务 ...
- go 字符串拼接
s := "hello," m := " world" a := s + m fmt.Printf("%s\n", a)
- resource not found :rgbd_launch
放到src下,再次编译catkin_make git https://github.com/ros-drivers/rgbd_launch.git
- 《剑指offer》第十六题(数值的整数次方)
// 面试题:数值的整数次方 // 题目:实现函数double Power(double base, int exponent),求base的exponent // 次方.不得使用库函数,同时不需要考 ...