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. mysql-学习笔记1

    1.while循环的一个方便的用法是循环处理一个SQL查询结果中的数据行. 2.mysqli_fetch_array(),这个内置的PHP函数从一个数据库查询的结果中获取一个数据行,可以搭配while ...

  2. ionic1 项目微信支付

    使用的插件参照地址:https://github.com/xu-li/cordova-plugin-wechat:(这里包含微信登录,微信分享和微信支付) 插件安装 cordova plugin ad ...

  3. SQL SERVER 将表字段值0和1互转的几种方法

    需求: 如果表字段的值为 0 则将其修改为1 ,如果表字段的值为 1 则将其修改为 0. 方法一 end 方法二 ) 方法三 )

  4. TNS-12549问题分析及解决办法

    该服务器启动监听时候报错因为最后一句是Linux Error:No space left on device 因为是LINUX Error,所以可以到/var/log/messages里查看具体报错信 ...

  5. 分享一个 jsPDF的简单使用以及中文编码问题的解决

    后台一个下载文件中内容国际化问题的坑甩到了前端 前端自己匹配,自己处理国际化,生成文件下载 jsPDF-AutoTable    挺靠谱 中文乱码, 还是好人多啊 解决方式如下文章 jsPDF的简单使 ...

  6. 绝对良心提供百度网盘的jdk1.8源码下载包含sun包的

    但是openjdk网站有提供的: http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/6bfaecb8ff77/src/share/classes/ 或者直 ...

  7. Python笔记(十三):urllib模块

    (一)      URL地址 URL地址组件 URL组件 说明 scheme 网络协议或下载方案 net_loc 服务器所在地(也许含有用户信息) path 使用(/)分割的文件或CGI应用的路径 p ...

  8. 机器学习实战(Machine Learning in Action)学习笔记————04.朴素贝叶斯分类(bayes)

    机器学习实战(Machine Learning in Action)学习笔记————04.朴素贝叶斯分类(bayes) 关键字:朴素贝叶斯.python.源码解析作者:米仓山下时间:2018-10-2 ...

  9. 学习git踩坑之路

    第一次使用git: 1.使用CentOS Linux release 7.3.1611 (Core),内核版本3.10.0-514.el7.x86_64 2.安装git: root权限运行:yum   ...

  10. C语言的历史及个人拙见

    C语言是一个无限广阔的世界,你刚开始睁眼看它的时候以为视线的尽头就是边界,但当你慢慢走去,才发现天外有天.这或许就是江湖传说只有C才有真正高手的原因,或许我们也可以反过来说:C没有高手——因为没有人能 ...