Intelligence System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1250    Accepted Submission(s): 560

Problem Description
After a day, ALPCs finally complete their ultimate intelligence system, the purpose of it is of course for ACM ... ... 

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
There are several test cases. 

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
The minimum total cost for inform everyone.

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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std; const int MAX=50000+10;
int n,m,size,top,index;
int u[MAX*2],v[MAX*2],w[MAX*2];
int head[MAX],dfn[MAX],low[MAX],dp[MAX];
int mark[MAX],stack[MAX];
bool ind[MAX];//ind标记缩点后的点是否有入度 struct Edge{
int v,w,next;
Edge(){}
Edge(int V,int W,int NEXT):v(V),w(W),next(NEXT){}
}edge[MAX*2]; void Init(int num){
for(int i=0;i<=num;++i)head[i]=-1,mark[i]=0,dp[i]=INF;
size=top=index=0;
} void InsertEdge(int u,int v,int w){
edge[size]=Edge(v,w,head[u]);
head[u]=size++;
} void tarjan(int u){
if(mark[u])return;
dfn[u]=low[u]=++index;
stack[++top]=u;
mark[u]=1;
for(int i=head[u];i != -1;i=edge[i].next){
int v=edge[i].v;
tarjan(v);
if(mark[v] == 1)low[u]=min(low[u],low[v]);
}
if(dfn[u] == low[u]){
while(stack[top] != u){
mark[stack[top]]=-1;
low[stack[top--]]=low[u];
}
mark[u]=-1;
--top;
}
} void dfs(int u){
if(mark[u])return;
mark[u]=1;
for(int i=head[u];i != -1;i=edge[i].next){
int v=edge[i].v,w=edge[i].w;
if(w<dp[v])dp[v]=w;
dfs(v);
}
} int main(){
while(~scanf("%d%d",&n,&m)){
Init(n);
for(int i=0;i<m;++i){
scanf("%d%d%d",&u[i],&v[i],&w[i]);
InsertEdge(u[i],v[i],w[i]);
}
for(int i=0;i<n;++i){//求连通分量进行缩点
if(mark[i])continue;
tarjan(i);
} for(int i=0;i<=n;++i)head[i]=-1,ind[i]=false,mark[i]=0;
size=0;
for(int i=0;i<m;++i){//缩点重建图
if(low[u[i]] == low[v[i]])continue;
ind[low[v[i]]]=true;
InsertEdge(low[u[i]],low[v[i]],w[i]);
}
for(int i=0;i<n;++i){
if(!ind[low[i]])dp[low[i]]=0,dfs(low[i]);
ind[low[i]]=true;
}
int sum=0;
for(int i=0;i<n;++i){
if(ind[low[i]])sum+=dp[low[i]];
ind[low[i]]=false;
}
cout<<sum<<endl;
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

hdu3072的更多相关文章

  1. 强连通 HDU3072

    n个点m条边 m条边 权值 简单点说就是求把所有强连通分量连在一起所需的最小花费 不用双向 图是联通的  cost[] 维护到这里的最小花费求和 #include<stdio.h> #in ...

  2. hdu3072 强连通+最小树形图

    题意:有一个人他要把一个消息通知到所有人,已知一些通知关系:A 能通知 B,需要花费 v,而又知道,如果某一个小团体,其中的成员相互都能直接或间接通知到,那么他们之间的消息传递是不需要花费的,现在问这 ...

  3. HDU3072 Intelligence System

    题目传送门 有个中文版的题面...和原题稍有不同 /* Description “这一切都是命运石之门的选择.” 试图研制时间机器的机关SERN截获了中二科学家伦太郎发往过去的一条短信,并由此得知了伦 ...

  4. hdu3072 Intelligence System (最小树形图?)

    题意:给一个有向图,问要从0号点能到达所有点所需要经过路径的最小权值和是多少,然而,若两点强联通,则这两点互相到达不需要花费.保证0号点能到达所有点 tarjan缩点以后直接取每个点入边中花费最小的即 ...

  5. [HDU3072]:Intelligence System(塔尖+贪心)

    题目传送门 题目描述 “这一切都是命运石之门的选择.”试图研制时间机器的机关SERN截获了中二科学家伦太郎发往过去的一条短 信,并由此得知了伦太郎制作出了电话微波炉(仮).为了掌握时间机器的技术,SE ...

  6. NOIP2016提高A组 B题 【HDU3072】【JZOJ4686】通讯

    题目描述 “这一切都是命运石之门的选择.” 试图研制时间机器的机关SERN截获了中二科学家伦太郎发往过去的一条短 信,并由此得知了伦太郎制作出了电话微波炉(仮). 为了掌握时间机器的技术,SERN总部 ...

  7. HDU-3072-IntelligenceSystem(tarjan,贪心)

    链接:https://vjudge.net/problem/HDU-3072 题意: 给你n个点,1个点到另一个点连接花费c,但是如果几个点可以相互可达,则这几个点连通花费为0. 求将整个图连通的最小 ...

随机推荐

  1. 基于visual Studio2013解决C语言竞赛题之0507筛选素数

     题目

  2. python中multiprocessing.pool函数介绍_正在拉磨_新浪博客

    python中multiprocessing.pool函数介绍_正在拉磨_新浪博客     python中multiprocessing.pool函数介绍    (2010-06-10 03:46:5 ...

  3. [Android] FileInputStream跟踪

    1. 源起 需要跟踪FileInputStream的Read的Nativie实现,开始走了弯路,Java工程下的FileInputStream实现与Android工程的实现不同.另外,http://b ...

  4. UVa 121 - Pipe Fitters

    称号:放置在一个圆中的矩形,它要求每个圆的每行或列是切线,问:多少能竖起来. 分析:计算几何.数论.首先计算矩形显示屏,然后计算互显示器(每一行与相邻行相同差1个月)求最大,你可以. 说明:╮(╯▽╰ ...

  5. Swift实战-QQ在线音乐(第一版)

    //一.*项目准备 1.QQ音乐App 界面素材:(我使用PP助手,将QQ音乐App备份,解压ipa文件 即可得到里面的图片素材) 2.豆瓣电台接口:"http://douban.fm/j/ ...

  6. qt执行cmd命令

    源地址:http://blog.csdn.net/hn307165411/article/details/6858614 运行 route.ipconfig 肯定没问题 Copy code QProc ...

  7. 在SSH框架中使用Spring的好处(转)

    以下是我总结下今天笔试中SSh中的总结: 在SSH框架中spring充当了管理容器的角色.我们都知道Hibernate用来做持久层,因为它将JDBC做了一个良好的封装,程序员在与数据库进行交互时可以不 ...

  8. mysql 表级锁

    表级锁:分为读锁和写锁: lock tables table_name read;//其他事务只能读,不能加写锁,要等待更新. SESSION 50 执行: mysql> update test ...

  9. hdu 3917 (最大权闭合图)

    题意:政府有一些路,m个公司来修,每个公司修路要交税给政府,修路政府要付给公司费用,求政府能获得的最大利润,如果选择一个公司负责一个项目,那么该公司负责的其它项目也必须由他负责,并且与其有相连关系的公 ...

  10. 在程序中,你敢怎样使用“goto”语句!

    用goto是一个个人爱好的问题.“我”的意见是,十个goto中有九个可以用相应的结构化结构来替换.在那些简单情形下,你可以完全替换掉goto,在复杂的情况下,十个中也有九个可以不用:你可以把部分代码写 ...