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]星球大战 [删边求连通块个数]
展开 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的 ...
随机推荐
- 不明原因报错集中处理:Undefined
1, NSGenericException错误 Terminating app due to uncaught exception 'NSGenericException', reason: '*** ...
- SQLSERVER 数据从一张那个表复制到另一张表
insert into 表名1 ( 字段A ,字段B ,字段C) SELECT 字段A ,字段B ,字段C FROM 表名2 (where条件看情况而定)
- Git教程摘录
http://download.csdn.net/download/lianghesgdmv/9893973 教程doc下载 备用-- 链接:http://pan.baidu.com/s/1gfu2 ...
- Java初始化块的作用
1.使代码更简洁 eg: public class Test { private void init() { System.out.println("初始化状态"); } publ ...
- C# 如何调用启动窗体
Program.cs中代码如下: using System; using System.Collections.Generic; using System.Windows.Forms; namespa ...
- BZOJ 2467: [中山市选2010]生成树(矩阵树定理+取模高斯消元)
http://www.lydsy.com/JudgeOnline/problem.php?id=2467 题意: 思路:要用矩阵树定理不难,但是这里的话需要取模,所以是需要计算逆元的,但是用辗转相减会 ...
- UVa 3349 Snowflake Snow Snowflakes(Hash)
http://poj.org/problem?id=3349 题意: 给出n片雪花留个角的长度,要求判断是否有一样的雪花. 思路: Hash表的应用. 首先将每个雪花所有角的总长计算出来,如果两片雪花 ...
- UVa 1149 装箱
https://vjudge.net/problem/UVA-1149 题意:给定N个物品的重量和背包的容量,同时要求每个背包最多装两个物品.求至少需要的背包数. 思路:很简单的贪心.每次将最轻的和最 ...
- TypeScript基础学习
什么是TypeScript? TypeScript是一种由微软开发的自由的和开源的编程语言,它是JavaScript的一个超集,扩展了JavaScript的语法. TypeScript支持任意浏览器, ...
- [ios][swift]文本框UITextField用法
参考:http://www.hangge.com/blog/cache/detail_530.html