HDU 3072 Intelligence System(tarjan染色缩点+贪心+最小树形图)
Intelligence System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3414 Accepted Submission(s): 1494
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!
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.
Believe kzc_tc’s working! There always is a way for him to communicate with all other intelligence personnel.
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
100
50
n个点,m条边的有向图,x y w表示x到y的花费是w
但是一个强连通分量内的点互相到达的花费是0
问你要到达所有点的最小花费
题目保证图是连通的,且存在一个点可以到所有点
存在强连通分量,强连通内的点互相到达花费为0,可以看成一个点
但是需要注意贪心一下
比如1到强连通分量x
应该选择1到强连通分量x的所有点中权值最小的边
缩点和贪心之后
图变成了一个DAG图,有向无环图
然后就是求这个DAG图的最小树形图的值
所有我们只需要维护所有点的最小入边的权值就好
但是有个点是没有入边的
(从此点出发,可以到达所有点)
#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染色缩点+贪心+最小树形图)的更多相关文章
- hdu 3072 Intelligence System(Tarjan 求连通块间最小值)
Intelligence System Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- HDU 3072 Intelligence System (强连通分量)
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU——3072 Intelligence System
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU - 3072 Intelligence System
题意: 给出一个N个节点的有向图.图中任意两点进行通信的代价为路径上的边权和.如果两个点能互相到达那么代价为0.问从点0开始向其余所有点通信的最小代价和.保证能向所有点通信. 题解: 求出所有的强连通 ...
- HDU——T 3072 Intelligence System
http://acm.hdu.edu.cn/showproblem.php?pid=3072 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- hdoj 3072 Intelligence System【求scc&&缩点】【求连通所有scc的最小花费】
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 5934 Bomb(tarjan/SCC缩点)题解
思路:建一个有向图,指向能引爆对象,把强连通分量缩成一点,只要点燃图中入度为0的点即可.因为入度为0没人能引爆,不为0可以由别人引爆. 思路很简单,但是早上写的一直错,改了半天了,推倒重来才过了... ...
- hdu 3072 有向图缩点成最小树形图计算最小权
题意,从0点出发,遍历所有点,遍历边时候要付出代价,在一个SCC中的边不要付费.求最小费用. 有向图缩点(无需建立新图,,n<=50000,建则超时),遍历边,若不在一个SCC中,用一个数组更新 ...
- Intelligence System (hdu 3072 强联通缩点+贪心)
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
随机推荐
- FZU1759(SummerTrainingDay04-B 欧拉降幂公式)
Problem 1759 Super A^B mod C Accept: 1056 Submit: 3444Time Limit: 1000 mSec Memory Limit : 327 ...
- 微信小程序传参数的几种方法
1,navigator 跳转时 wxml页面(参数多时可用“&”) <navigator url='../index/index?id=1&name=aaa'></n ...
- [HNOI2011]括号修复
设\(nd[4]\) 0--多出来的右括号 1--多出来的左括号 2--取反后多出来的右括号 3--取反后多出来的左括号 这样一来 Swap: swap(0,3),swap(1,2),swap(sn[ ...
- 【读书笔记】iOS-自定义视图的创建
静态创建自定义视图就是以拖动的方法来创建. 动态创建自定义视图可以理解为使用代码来创建自定义视图. 参考资料:<iOS7开发快速入门>
- 研发环境 chrome谷歌浏览器和firefox火狐浏览器解决跨域问题
1 火狐浏览器 (1).先在地址栏输入about:config,然后单击“我了解此风险”. (2).找到security.fileuri.strict_origin_policy,然后在值下面的tru ...
- Nginx控制并发连接数
ngx_http_limit_conn_module这个模块用于限制每个定义的key值的连接数,特别是单IP的连接数. 不是所有的连接数都会被计数.一个符合计数要求的连接是整个请求头已经被读取的连接. ...
- Mongodb的入门(4)mongodb3.6的索引
Mongodb的索引: 在介绍索引之前,再强调一下nosql数据库和sql数据库的区别: sql数据库:结构化数据,定好了表格后,每一行的内容都是结构化的 mongo:文档数据,表下的数据都可以有自己 ...
- centos安装pip3
安装pip3 1:安装依赖 yum install openssl-devel -y yum install zlib-devel -y 2:安装setuptools wget --no-check- ...
- MySQL分析函数实现
| MySQL分析函数实现还好MySQL8.0已经实现了与Oracle相同的分析函数. 1. 实现rownumSET @rn:=0;SELECT @rn:=@rn+1 AS rownum ,e.* F ...
- Finding the source of signals on Linux with strace, auditd, or systemtap
inux and UNIX® like operating systems commonly use signals to communicate between processes. The use ...