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

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

Source

2009 Multi-University Training Contest 17 - Host by NUDT
 

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

解析:先把所有强连通分量求出来,再求不同连通分量连接起来的最小花费,最后把除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 ;
}
 

最新文章

  1. CodeForces - 662A Gambling Nim
  2. java中注解的使用与实例 (二)
  3. Android随笔之——Android ADB详解
  4. Java中从键盘中任意输入字符串,将其转换成数字后,并求和
  5. js URL中文传参乱码
  6. Linux学习笔记(7)Linux常用命令之压缩解压命令
  7. ie6,ie7兼容性总结(转)
  8. 内部类访问局部变量的时候,为什么变量必须加上final修饰
  9. easyui的页面等待提示层,即mask
  10. Vim,一个开放源代码的文本编辑器(转)
  11. C语言的字符测试函数
  12. MapReduce Kmeans聚类算法
  13. Hdu 5444 Elven Postman dfs
  14. Linux Shell Scripting Tutorial (LSST) v2.0
  15. linux软件安装(rpm,源码编译)
  16. Echarts数据可视化series-radar雷达图,开发全解+完美注释
  17. ActiveMQ入门练习
  18. Lyndon Word学习笔记
  19. c# word操作篇,解决字符串长度超过255就不能替换的问题
  20. 面试回顾——session相关

热门文章

  1. python_实现发送邮件功能
  2. Maven profile 打包分环境加载不同的资源文件
  3. 51nod 1137 矩阵乘法
  4. 瞎折腾之Webhooks
  5. YOLOv3-darknet 内容解析
  6. 【Coursera】Technology :Fifth Week(1)
  7. POJ 2728 Desert King(最优比率生成树 01分数规划)
  8. 学习mybatis-3 step by step 篇四
  9. Linux——用户管理简单学习笔记(二)
  10. Python将某文件夹及其子文件夹下某种格式的文件移动到另一个指定的文件下