HDU 3072 Intelligence System(Tarjan+最小树形图)
来源:互联网 发布:如果明朝没有灭亡知乎 编辑:程序博客网 时间:2024/09/21 06:17
Intelligence System
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 30 1 1001 2 500 2 1003 30 1 1001 2 502 1 1002 20 1 500 1 100
Sample Output
15010050
Source
2009 Multi-University Training Contest 17 - Host by NUDT
题意:给一个n个顶点m条边的有向图,求从0点出发,将所有顶点连接起来的最小权值。如果两个顶点可以两两到达,那么这两个顶点之间的权值为0。
分析:很经典的Tarjan缩点+最小树形图。
#include<bits/stdc++.h>using namespace std;struct Edge{ int u,v,w,next;};Edge edge[100005],edge_temp[100005];int cnt;int head[50005];int dfn[50005];int low[50005];int Stack[50005];int vis[50005];int scc[50005];int in[50005];int pre[50005];int scc_num;int top;int depth;void init(){ memset(head,-1,sizeof(head)); memset(dfn,-1,sizeof(dfn)); memset(low,-1,sizeof(low)); memset(vis,0,sizeof(vis)); memset(scc,-1,sizeof(scc)); memset(in,0,sizeof(in)); top=depth=scc_num=cnt=0;}void add(int u,int v,int w){ edge[cnt].u=u; edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++;}void tarjan(int cur){ low[cur]=dfn[cur]=depth++; Stack[top++]=cur; vis[cur]=1; for(int i=head[cur];i!=-1;i=edge[i].next){ int v=edge[i].v; if(dfn[v]==-1){ tarjan(v); low[cur]=min(low[cur],low[v]); } else if(vis[v]){ low[cur]=min(low[cur],dfn[v]); } } if(low[cur]==dfn[cur]){ int u; scc_num++; do{ u=Stack[--top]; scc[u]=scc_num; vis[u]=0; }while(cur!=u); }}int Dir_MST(int root,int n,int m){ int ans=0; while(true){ memset(in,0x3f3f3f3f,sizeof(in)); memset(pre,-1,sizeof(pre)); memset(vis,-1,sizeof(vis)); memset(scc,-1,sizeof(scc)); for(int i=1;i<=m;i++){ int u=edge_temp[i].u; int v=edge_temp[i].v; int w=edge_temp[i].w; if(u!=v&&in[v]>w){//printf("%d->%d=%d\n",u,v,w); in[v]=w; pre[v]=u; } } in[root]=0; cnt=1; for(int i=1;i<=n;i++){ ans+=in[i];//printf("ans=%d\n",ans); int v=i; while(vis[v]!=i&&scc[v]==-1&&v!=root){ vis[v]=i; v=pre[v]; } if(scc[v]==-1&&v!=root){//printf("寻找环\n"); int u=pre[v]; while(u!=v){ scc[u]=cnt; u=pre[u]; } scc[v]=cnt; } } if(cnt==1){ return ans; } for(int i=1;i<=n;i++){ if(scc[i]==-1){ scc[i]=++cnt; } } for(int i=1;i<=m;i++){ int u=edge_temp[i].u; int v=edge_temp[i].v; edge_temp[i].u=scc[u]; edge_temp[i].v=scc[v]; if(scc[u]!=scc[v]){ edge_temp[i].w-=in[v]; } } root=scc[root]; n=cnt; }}int main(){ int n,m; while(~scanf("%d%d",&n,&m)){ init(); for(int i=1;i<=m;i++){ int u,v,w; scanf("%d%d%d",&u,&v,&w); u++; v++; add(u,v,w); edge_temp[i].u=u; edge_temp[i].v=v; edge_temp[i].w=w; } for(int i=1;i<=n;i++){ if(dfn[i]==-1){ tarjan(i); } } for(int i=1;i<=m;i++){ int u=edge_temp[i].u; int v=edge_temp[i].v; edge_temp[i].u=scc[u]; edge_temp[i].v=scc[v]; } printf("%d\n",Dir_MST(scc[1],scc_num,m)); } return 0;}
阅读全文
0 0
- [tarjan+最小树形图] hdu 3072 Intelligence System
- HDU 3072 Intelligence System(最小树形图+Tarjan)
- HDU 3072 Intelligence System(Tarjan+最小树形图)
- HDU 3072 Intelligence System(tarjan+DAG上的最小生成树形图)
- HDU 3072 Intelligence System (tarjan-scc缩点 + 最小树形图)
- hdu3072 Intelligence System(tarjan缩点+最小树形图)
- HDU 3072 Intelligence System 强连通 最小树形图
- HDU 3072 Intelligence System(强连通+最小树形图)
- HDU 3072 Intelligence System (强连通缩点+简单最小树形图,4级)
- HDU 3072Intelligence System 强连通分量分解 + 最小树形图
- HDU 3072:Intelligence System(强连通+不定根最小树形图)
- HDU3072 Intelligence System【最小树形图】
- hdu 3072 Intelligence System (Tarjan+Kosaraju+缩点)
- HDU 3072 Intelligence System (强连通+(贪心||树形图))
- HDU 3072 Intelligence System
- HDU 3072 Intelligence System 传递的最小费用
- hdu3072 Intelligence System (Tarjan)
- hdu3072 Intelligence System(Tarjan缩点+最小生成树)
- Caused by: org.jbpm.api.JbpmException: resource classpath:jdpm/jbpm.cfg.xml does not exist
- javaweb的request获取真实IP地址
- 代码整洁之道--函数
- [公告] 2017年9月装机员系统更新
- 通过H5的新标签canvas做出一个时钟的全过程,希望对初学者有帮助
- HDU 3072 Intelligence System(Tarjan+最小树形图)
- Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
- GVIM win7添加到右键菜单方法
- Python 科学计算之实验环境安装及使用
- Spring学习笔记(一)环境搭建和测试开发环境
- 使用ssh 访问越狱iPhone的两种方式
- 对IoC(Inversion of Control)的理解
- Set的获取方式
- python数据库连接操作---cursor()