Intelligence System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3414    Accepted Submission(s): 1494

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3069 3077 3070 3071 3073 
 
题目意思:
n个点,m条边的有向图,x y w表示x到y的花费是w
但是一个强连通分量内的点互相到达的花费是0
问你要到达所有点的最小花费
题目保证图是连通的,且存在一个点可以到所有点
分析:
存在强连通分量,强连通内的点互相到达花费为0,可以看成一个点
但是需要注意贪心一下
比如1到强连通分量x
应该选择1到强连通分量x的所有点中权值最小的边
缩点和贪心之后
图变成了一个DAG图,有向无环图
然后就是求这个DAG图的最小树形图的值
这里的求发很巧妙,因为题目保证了图的连通性
所有我们只需要维护所有点的最小入边的权值就好
但是有个点是没有入边的
(从此点出发,可以到达所有点)
 
code:
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 0x7fffffff
#define me(a,x) memset(a,x,sizeof(a))
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
} #define max_v 50005
struct node
{
int v,w;
node(int vv,int ww)
{
v=vv;
w=ww;
}
};
int dfn[max_v];
int low[max_v];
int vis[max_v];
int stk[max_v];
int color[max_v];
int a[max_v];
vector<node> G[max_v];
int n,m;
int sig,cnt,sp;
LL ans;
void init()
{
me(dfn,);
me(low,);
me(vis,);
me(stk,);
me(color,);
for(int i=;i<=n;i++)
{
G[i].clear();
a[i]=INF;
}
sig=;
cnt=;
sp=-;
ans=;
} void tarjan(int u)
{
vis[u]=;
dfn[u]=low[u]=cnt++;
stk[++sp]=u;
for(int j=;j<G[u].size();j++)
{
int v=G[u][j].v;
if(vis[v]==)
tarjan(v);
if(vis[v]==)
low[u]=min(low[u],low[v]);
}
if(low[u]==dfn[u])//染色
{
sig++;
do
{
vis[stk[sp]]=-;
color[stk[sp]]=sig;
}while(stk[sp--]!=u);
}
} int f(int u,int x)//u点到颜色x的点中的最小的权值
{
int minv=INF;
for(int j=;j<G[u].size();j++)
{
int v=G[u][j].v;
if(color[v]==x)
{
minv=min(minv,G[u][j].w);
}
}
return minv;
}
int ff(int x,int y)//判断有没有x到y的边
{
for(int j=;j<G[x].size();j++)
{
if(G[x][j].v==y)
return ;
}
return ;
}
int main()
{
int x,y,z;
while(~scanf("%d %d",&n,&m))
{
init();
for(int i=;i<=m;i++)
{
scanf("%d %d %d",&x,&y,&z);
x++,y++;
if(ff(x,y)==)//没有x到y的边
{
G[x].push_back(node(y,z));
}else
{
if(G[x][y].w>z)//有x到y的边,但是存在权更小的边,替换
G[x].push_back(node(y,z));
}
}
for(int i=;i<=n;i++)
{
if(vis[i]==)
tarjan(i);
}
for(int i=;i<=n;i++)
{
for(int j=;j<G[i].size();j++)
{
int v=G[i][j].v;
if(color[i]!=color[v])
{
//维护每个点的最小入边,最小树形图算法
a[color[v]]=min(a[color[v]],f(i,color[v]));
}
}
}
//所有点的最小入边之和就是最小树形图的权值和(有一个点没有入边)
for(int i=;i<=sig;i++)
{
if(a[i]<INF)
ans+=a[i];
}
printf("%lld\n",ans);
}
return ;
}
/*
题目意思:
n个点,m条边的有向图,x y w表示x到y的花费是w
但是一个强连通分量内的点互相到达的花费是0
问你要到达所有点的最小花费
题目保证图是连通的,且存在一个点可以到所有点 分析:
存在强连通分量,强连通内的点互相到达花费为0,可以看成一个点
但是需要注意贪心一下
比如1到强连通分量x
应该选择1到强连通分量x的所有点中权值最小的边
缩点和贪心之后
图变成了一个DAG图,有向无环图
然后就是求这个DAG图的最小树形图的值 这里的求发很巧妙,因为题目保证了图的连通性
所有我们只需要维护所有点的最小入边的权值就好
但是有个点是没有入边的
(从此点出发,可以到达所有点) */

HDU 3072 Intelligence System(tarjan染色缩点+贪心+最小树形图)的更多相关文章

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

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

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

    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

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

  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. hdoj 3072 Intelligence System【求scc&&缩点】【求连通所有scc的最小花费】

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

  7. HDU 5934 Bomb(tarjan/SCC缩点)题解

    思路:建一个有向图,指向能引爆对象,把强连通分量缩成一点,只要点燃图中入度为0的点即可.因为入度为0没人能引爆,不为0可以由别人引爆. 思路很简单,但是早上写的一直错,改了半天了,推倒重来才过了... ...

  8. hdu 3072 有向图缩点成最小树形图计算最小权

    题意,从0点出发,遍历所有点,遍历边时候要付出代价,在一个SCC中的边不要付费.求最小费用. 有向图缩点(无需建立新图,,n<=50000,建则超时),遍历边,若不在一个SCC中,用一个数组更新 ...

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

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

随机推荐

  1. ubuntu16.4+nginx+uwsgi+Django 部署上线

    Nginx概述 Nginx是一款轻量级的HTTP服务器,采用事件驱动和异步非阻塞处理方式框架,这让其具有极好的IO性能,市场用于服务端的反向代理和负载均衡 Nginx优点 高并发连接:官方测试Ngin ...

  2. iOS设备上出现的click,live,on点击失去效果

    iOS设备上出现的点击事件失效,但是在Android上可以正常使用, 1.iOS设备对标签点击限制,不认为是可点击的标签,需要给要绑定点击事件的标签加上一个样式,cursor:pointer:这样就可 ...

  3. Spring Boot系列学习文章(二) -- 配置多数据源

    前言: 在上一章中,我们已经搭建好项目,现在来讲一下如何配置数据源. 由于在有的项目中,用的数据源可能会涉及多个,且是不同类型的,我们接下来就讲解多数据源的配置. 情景描述: 现有项目需要访问不同的数 ...

  4. 安装google,多试试

    对于谷歌Chrome32位版本,使用如下链接: wget https://dl.google.com/linux/direct/google-chrome-stable_current_i386.de ...

  5. 带你从零学ReactNative开发跨平台App开发(二)

    ReactNative跨平台开发系列教程: 带你从零学ReactNative开发跨平台App开发(一) 带你从零学ReactNative开发跨平台App开发(二) 带你从零学ReactNative开发 ...

  6. 【转】qt-vs-addin:Qt4和Qt5之VS插件如何共存与使用

    原则上,两者是不可以同时存在的,但是如果都安装了,该如何分别使用他们呢? Qt4 Visual Studio Add-in:官网可以下载安装程序,qt-vs-addin-1.1.11-opensour ...

  7. 如何在 Windows 10 中搭建 Node.js 环境?

    [编者按]本文作者为 Szabolcs Kurdi,主要通过生动的实例介绍如何在 Windows 10 中搭建 Node.js 环境.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 在本文中 ...

  8. PMS与orcalebs结合之字段

    call fnd_global.APPS_INITIALIZE(1318,50583,401) select fnd_profile.VALUE('ORG_ID') FROM DUAL select ...

  9. hibernate数据库操作基础

    1.根据主键查询 2.getSession().createSQLQuery(sql)和session.createQuery(sql) 3.Seeion的其他方法  4.Hibernate Crit ...

  10. sql server 存储过程的学习

    存储过程学习笔记 存储过程就是一条或者多条sql语句的集合,为了实现特定任务,而将一些需要多次调用的固定操作语句编写成程序段,这些程序段存储在服务器上,有数据库服务器通过程序来调用.T_SQL:存储过 ...