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
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!
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.
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.
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
题目意思:
n个点,m条边的有向图,x y w表示x到y的花费是w
但是一个强连通分量内的点互相到达的花费是0
问你要到达所有点的最小花费
题目保证图是连通的,且存在一个点可以到所有点
n个点,m条边的有向图,x y w表示x到y的花费是w
但是一个强连通分量内的点互相到达的花费是0
问你要到达所有点的最小花费
题目保证图是连通的,且存在一个点可以到所有点
分析:
存在强连通分量,强连通内的点互相到达花费为0,可以看成一个点
但是需要注意贪心一下
比如1到强连通分量x
应该选择1到强连通分量x的所有点中权值最小的边
缩点和贪心之后
图变成了一个DAG图,有向无环图
然后就是求这个DAG图的最小树形图的值
存在强连通分量,强连通内的点互相到达花费为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[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31}; int mon2[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31}; int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; int getval() { int ret(0); char c; while((c=getchar())==' '||c=='\n'||c=='\r'); ret=c-'0'; while((c=getchar())!=' '&&c!='\n'&&c!='\r') ret=ret*10+c-'0'; 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,0); me(low,0); me(vis,0); me(stk,0); me(color,0); for(int i=1;i<=n;i++) { G[i].clear(); a[i]=INF; } sig=0; cnt=1; sp=-1; ans=0; } void tarjan(int u) { vis[u]=1; dfn[u]=low[u]=cnt++; stk[++sp]=u; for(int j=0;j<G[u].size();j++) { int v=G[u][j].v; if(vis[v]==0) tarjan(v); if(vis[v]==1) low[u]=min(low[u],low[v]); } if(low[u]==dfn[u])//染色 { sig++; do { vis[stk[sp]]=-1; color[stk[sp]]=sig; }while(stk[sp--]!=u); } } int f(int u,int x)//u点到颜色x的点中的最小的权值 { int minv=INF; for(int j=0;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=0;j<G[x].size();j++) { if(G[x][j].v==y) return 1; } return 0; } int main() { int x,y,z; while(~scanf("%d %d",&n,&m)) { init(); for(int i=1;i<=m;i++) { scanf("%d %d %d",&x,&y,&z); x++,y++; if(ff(x,y)==0)//没有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=1;i<=n;i++) { if(vis[i]==0) tarjan(i); } for(int i=1;i<=n;i++) { for(int j=0;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=1;i<=sig;i++) { if(a[i]<INF) ans+=a[i]; } printf("%lld\n",ans); } return 0; } /* 题目意思: n个点,m条边的有向图,x y w表示x到y的花费是w 但是一个强连通分量内的点互相到达的花费是0 问你要到达所有点的最小花费 题目保证图是连通的,且存在一个点可以到所有点 分析: 存在强连通分量,强连通内的点互相到达花费为0,可以看成一个点 但是需要注意贪心一下 比如1到强连通分量x 应该选择1到强连通分量x的所有点中权值最小的边 缩点和贪心之后 图变成了一个DAG图,有向无环图 然后就是求这个DAG图的最小树形图的值 这里的求发很巧妙,因为题目保证了图的连通性 所有我们只需要维护所有点的最小入边的权值就好 但是有个点是没有入边的 (从此点出发,可以到达所有点) */
心之所向,素履以往
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南