Intelligence System

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

Total Submission(s): 1988    Accepted Submission(s): 859

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<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
#define MAX 500100
#define INF 0x3f3f3f
struct node
{
int u,v,val;
int next;
}edge[MAX];
int low[MAX],dfn[MAX];
int head[MAX],scc_cnt,dfs_clock,cnt;
int in[MAX],out[MAX],dp[MAX];
int sccno[MAX];
int m,n;
bool Instack[MAX];
vector<int>G[MAX];
vector<int>scc[MAX];
stack<int>s;
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
void add(int u,int v,int val)
{
int i;
for( i=head[u];i!=-1;i=edge[i].next)
{
if(edge[i].v==v)
break;
}
if(i==-1)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].val=val;
edge[cnt].next=head[u];
head[u]=cnt++;
}
else
edge[i].val=min(edge[i].val,val);
}
void getmap()
{
int a,b,c;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
}
void tarjan(int u,int fa)
{
int v;
low[u]=dfn[u]=++dfs_clock;
Instack[u]=true;
s.push(u);
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
}
else if(Instack[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
scc_cnt++;
for(;;)
{
v=s.top();s.pop();
Instack[v]=false;
sccno[v]=scc_cnt;
if(v==u) break;
}
}
}
void find(int l,int r)
{
memset(sccno,0,sizeof(sccno));
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(Instack,false,sizeof(Instack));
scc_cnt=dfs_clock=0;
for(int i=l;i<=r;i++)
if(!dfn[i])
tarjan(i,-1);
}
void suodian()
{
for(int i=1;i<=scc_cnt;i++)
G[i].clear(),dp[i]=INF;
for(int i=0;i<cnt;i++)
{
int u=sccno[edge[i].u];
int v=sccno[edge[i].v];
if(u!=v)
{
G[u].push_back(v);
dp[v]=min(dp[v],edge[i].val);
}
}
}
void solve()
{
int sum=0;
for(int i=1;i<=scc_cnt;i++)
{
if(sccno[0]!=i)
sum+=dp[i];
}
printf("%d\n",sum);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
getmap();
find(0,n-1);
suodian();
solve();
}
return 0;
}

hdoj--3072--Intelligence System(scc+缩点+数据去重)的更多相关文章

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

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

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

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

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

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

  4. HDU——3072 Intelligence System

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

  5. HDU——T 3072 Intelligence System

    http://acm.hdu.edu.cn/showproblem.php?pid=3072 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

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

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

  7. HDU - 3072 Intelligence System

    题意: 给出一个N个节点的有向图.图中任意两点进行通信的代价为路径上的边权和.如果两个点能互相到达那么代价为0.问从点0开始向其余所有点通信的最小代价和.保证能向所有点通信. 题解: 求出所有的强连通 ...

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

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

  9. HDU 3072--Intelligence System【SCC缩点新构图 &amp;&amp; 求连通全部SCC的最小费用】

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

随机推荐

  1. input表单(02)

    01.表单的代码实现 <!DOCTYPE html> <html> <head> <title>世纪佳缘,你在我也在</title> < ...

  2. MainActivity 多个Fragment 内存被回收

    0. 前言 应用首页采用Activity +Tab 模式,多个Fragment 替换显示隐藏 FragmentTransaction transaction = getSupportFragmentM ...

  3. 安装Windows服务,一直提示系统正在关机的错误。

    错误截图如下: 问题概况: 在本机安装没问题,程序没问题. 安装到公司的测试环境就报错了!以管理员身份运行也不行. 解决方案: 1.最后发现是360安全防护中心拦截了.具体解决过程如下: 2.进入36 ...

  4. VMWare linux 打印太多,看不到之前的记录的解决方法总结

    1.在命令后面加 | more. 可以每次按空格键或是回车键后翻.2.命令后面加| less ,可以前后翻.3.用重定向到文件 > 文件名,之后慢慢看 ----待补充 ------

  5. C# 彻底关闭程序,包括循环

    System.Environment.Exit(System.Environment.ExitCode); this.Dispose(); this.Close();

  6. Caffe+Kubuntu16.04_X64+CUDA 8.0配置

    前言: 经过尝试过几次Caffe,theano,MxNet之后,很长时间没有进行caffe的更新,此次在Ubuntu16.04下安装Caffe,折腾了一天时间,终于安装成功. 参考链接:Caffe+U ...

  7. C# 聚合数据借口发短信的使用

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. day8 面向对象编程基础

    活在当下的程序员应该都听过“面向对象编程”一词,也经常有人问能不能用一句话解释下什么是“面向对象编程”,我们先来看看比较正式的说法. 把一组数据结构和处理它们的方法组成对象(object),把相同行为 ...

  9. [bzoj3291] Alice与能源计划 (二分图最大匹配)

    传送门 Description 在梦境中,Alice来到了火星.不知为何,转眼间Alice被任命为火星能源部长,并立刻面临着一个严峻的考验.为 了方便,我们可以将火星抽象成平面,并建立平面直角坐标系. ...

  10. HashMap源码分析笔记(一)

    一.结构 HashMap的结构由数组和链表组成,可以说是一个链表类型的数组: 快速定位方式:key值得hash变换作为数组索引快速找到对应数组块,之后通过hash值对比从链表中查找到匹配项. hash ...