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

题意:简单点说就是求把所有强连通分量连在一起所需的最小花费

解析:先把所有强连通分量求出来,再求不同连通分量连接起来的最小花费,最后把除0所在的连通分量所需的最小花费连接起来,

代码

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iterator>
#include<stack>
using namespace std;
const int INF=1e9+;
const int eps=0.0000001;
typedef __int64 LL;
const int maxn=;
const int maxm=;
int N,M,ans,id;
int dfn[maxn],low[maxn],cost[maxn],resign[maxn];
vector<int> G[maxn];
stack<int> KK;
bool inq[maxn];
struct edge
{
int u,v,w;
edge(int u=,int v=,int w=):u(u),v(v),w(w){}
}E[maxm];
void init()
{
ans=id=;
while(!KK.empty()) KK.pop();
for(int i=;i<=N;i++)
{
dfn[i]=low[i]=;
resign[i]=;
cost[i]=INF;
G[i].clear();
inq[i]=false;
}
}
void Tarjan(int x)
{
dfn[x]=low[x]=++id;
inq[x]=true;
KK.push(x);
int t,Size=G[x].size();
for(int i=;i<Size;i++)
{
t=G[x][i];
if(!dfn[t])
{
Tarjan(t);
low[x]=min(low[x],low[t]);
}
else if(inq[t]) low[x]=min(low[x],dfn[t]);
} //前面都差不多
if(dfn[x]==low[x])
{
ans++;
do
{
t=KK.top(); KK.pop();
inq[t]=false;
resign[t]=ans; //这个地方,标记连通分量
}while(t!=x);
}
return;
}
int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
init();
int u,v,w;
for(int i=;i<=M;i++)
{
scanf("%d%d%d",&u,&v,&w);
E[i]=edge(u,v,w);
G[u].push_back(v); //有向图
}
for(int i=;i<N;i++) if(!dfn[i]) Tarjan(i);
for(int i=;i<=M;i++)
{
edge& e=E[i];
int u=e.u,v=e.v,w=e.w;
int x=resign[u],y=resign[v]; //连通分量繁荣编号
if(x!=y) cost[y]=min(cost[y],w); //更新值
}
int sum=;
for(int i=;i<=ans;i++)
{
if(i==resign[]||cost[i]==INF) continue;//跟0是统一连通分量的不管
sum+=cost[i];
}
printf("%d\n",sum);
}
return ;
}

Hdu3072-Intelligence System(强连通求最小值)的更多相关文章

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

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

  2. hdoj 3072 Intelligence System【求scc&&缩点】【求连通所有scc的最小花费】

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

  3. HDU3072 Intelligence System

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

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

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

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

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

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

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

  7. Intelligence System

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

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

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

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

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

随机推荐

  1. URAL 1658

    题目大意:求出T个最小的满足各个位的和为S1,平方和为S2的数.按顺序输出.数的位数大于100或者不存在这样一个数时,输出:No solution. KB     64bit IO Format:%I ...

  2. Android——ViewPager多页面滑动切换以及动画效果

    一.首先,我们来看一下效果图,这是新浪微博的Tab滑动效果.我们可以手势滑动,也可以点击上面的头标进行切换.与此同方式,白色横条会移动到相应的页卡头标下.这是一个动画效果,白条是缓慢滑动过去的.好了, ...

  3. hadoop2对比hadoop1

    hadoop2对比hadoop1 1.体系结构 HDFS+MapReduce,共同点都是分布式的,主从关系结构. HDFS=一个NameNode+多个DataNode, NameNode含有我们用户存 ...

  4. iOS 部分问题总结2 - 苹果审核篇

    iOS 部分问题总结(二) - 苹果审核篇 1. 记录下5.1新规后上传被拒的问题排查和解决过程. 几天前,最新一次的更新被拒了,提示Invaild Binary.好在苹果同时发来了说明邮件做了详细说 ...

  5. STM32外部中断具体解释

      一.基本概念 ARM Coetex-M3内核共支持256个中断,当中16个内部中断,240个外部中断和可编程的256级中断优先级的设置.STM32眼下支持的中断共84个(16个内部+68个外部), ...

  6. mogodb亿万级数据性能測试

    本机 i7四核 8G 废话少说 mogodb 最像sql的nosql 使用批量插入一次20万循环10次总共200万数据用时65秒(尝试一次50万只是报内存溢出了,原因未知) 插入2000万数据用时10 ...

  7. 多路复用I/O select()

    select(),poll(),epoll()的总结:http://www.cnblogs.com/Anker/p/3265058.html 在socket编程中,仅仅使用connect,accept ...

  8. Spark 初级算子

    #常用Transformation(即转换,延迟加载) #通过并行化scala集合创建RDD val rdd1 = sc.parallelize(Array(1,2,3,4,5,6,7,8)) #查看 ...

  9. Tomcat 原理篇

    TOMCAT 原理篇一.Tomcat 组成(Tomcat 由以下组件组成) 1.server a) Server是一个Catalina Servlet容器: b) Server 可以包含一个或多个se ...

  10. 超长英文(代码)自动换行的样式(CSS)

    如何想让一连串文字在显示可以自动换行,而不会把在代码中使用的容器撑开,则在文章的CSS样式处加上以下代码即可: table-layout: fixed; word-wrap:break-word;或者 ...