博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3072 Intelligence System (Tarjan 或 Kosaraju)
阅读量:5773 次
发布时间:2019-06-18

本文共 6070 字,大约阅读时间需要 20 分钟。

Intelligence System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 901    Accepted Submission(s): 401

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
 

 

题意:给你n个点,m条边,每条边有一个权值(传送message代价),已知强连通分支内部不需花费,求minimal cost

步骤:

1.缩点n->scc个点

2.将到这scc个点的最小代价计算出来

3.相加

 

#include
#include
#include
using namespace std;const int VM=50010;const int EM=100010;const int INF=0x3f3f3f3f;struct Edge{ int to,nxt; int cap;}edge[EM<<1];int n,m,cnt,head[VM],cost[VM];int dep,top,atype;int dfn[VM],low[VM],vis[VM],stack[VM],belong[VM];void addedge(int cu,int cv,int cw){ edge[cnt].to=cv; edge[cnt].cap=cw; edge[cnt].nxt=head[cu]; head[cu]=cnt++;}void Tarjan(int u){ dfn[u]=low[u]=++dep; stack[top++]=u; vis[u]=1; for(int i=head[u];i!=-1;i=edge[i].nxt){ int v=edge[i].to; if(!dfn[v]){ Tarjan(v); low[u]=min(low[u],low[v]); }else if(vis[v]){ low[u]=min(low[u],dfn[v]); } } int j; if(dfn[u]==low[u]){ atype++; do{ j=stack[--top]; belong[j]=atype; vis[j]=0; }while(u!=j); }}void init(){ cnt=0; memset(head,-1,sizeof(head)); dep=0, top=0, atype=0; memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(vis,0,sizeof(vis)); memset(belong,0,sizeof(belong));}int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d",&n,&m)){ init(); int u,v,w; while(m--){ scanf("%d%d%d",&u,&v,&w); u++;v++; addedge(u,v,w); } for(int i=1;i<=n;i++) if(!dfn[i]) Tarjan(i); memset(cost, INF, sizeof(cost)); int ans=0; for(int u=1;u<=n;u++) for(int i=head[u];i!=-1;i=edge[i].nxt){ int v=edge[i].to; if(belong[u]!=belong[v]){ cost[belong[v]]=min(cost[belong[v]],edge[i].cap); } } for(int i=1;i<=atype;i++){ if(cost[i]!=INF) ans+=cost[i]; //printf("---------- i = %d %d\n",i,cost[i]); } /*换成下面这样也对, for(int i=1;i

 

 

 

#include
#include
#include
using namespace std;const int INF=0x3f3f3f3f;const int VM=50010;const int EM=100010;struct Edge{ int to,nxt; int cap;}edge1[EM],edge2[EM]; //edge1是正向图G edge2 是图G的逆GTint head1[VM],head2[VM],n,cnt;void addedge(int cu,int cv,int cw){ edge1[cnt].to=cv; edge1[cnt].cap=cw; edge1[cnt].nxt=head1[cu]; head1[cu]=cnt; edge2[cnt].to=cu; edge2[cnt].cap=cw; edge2[cnt].nxt=head2[cv]; head2[cv]=cnt++;}int k,scc;int order[VM],vis[VM],scc_id[VM],dis[VM]; //order是记录点出栈的时间,ssc_id 记强连通分量的集合 在同一强连通分量中 ssc_id[i] 的值相同int DFS(int u){ //搜索图G 记录各点出栈时间 for(int i=head1[u];i!=-1;i=edge1[i].nxt){ int v=edge1[i].to; if(!vis[v]){ vis[v]=1; DFS(v); } } order[++k]=u; //order [1..n] 记录的是出栈时间最早的 order[1]最早 return 0;}int reDFS(int u){ //搜索GT图 缩点 for(int i=head2[u];i!=-1;i=edge2[i].nxt){ int v=edge2[i].to; if(scc_id[v]==0){ scc_id[v]=scc; reDFS(v); } } return 0;}int Kosaraju(){ int i,u; k=scc=0; //scc是强连通图的个数 memset(order,0,sizeof(order)); memset(vis,0,sizeof(vis)); for(i=0;i
0;i--){ //从出栈时间最晚的开始搜索缩点 这样不会连通到别的树上 u=order[i]; if(scc_id[u]==0){ scc_id[u]=++scc; reDFS(u); } } return scc;}int main(){ //freopen("input.txt","r",stdin); int m; while(~scanf("%d%d",&n,&m)){ cnt=0; memset(head1,-1,sizeof(head1)); memset(head2,-1,sizeof(head2)); int u,v,w; for(int i=0;i

 

转载地址:http://mjaux.baihongyu.com/

你可能感兴趣的文章
Scrapy基本用法
查看>>
PAT A1030 动态规划
查看>>
自制一个 elasticsearch-spring-boot-starter
查看>>
软件开发学习的5大技巧,你知道吗?
查看>>
java入门第二季--封装--什么是java中的封装
查看>>
【人物志】美团前端通道主席洪磊:一位产品出身、爱焊电路板的工程师
查看>>
一份关于数据科学家应该具备的技能清单
查看>>
机器学习实战_一个完整的程序(一)
查看>>
Web框架的常用架构模式(JavaScript语言)
查看>>
如何用UPA优化性能?先读懂这份报告!
查看>>
这些Java面试题必须会-----鲁迅
查看>>
Linux 常用命令
查看>>
NodeJS 工程师必备的 8 个工具
查看>>
CSS盒模型
查看>>
ng2路由延时加载模块
查看>>
使用GitHub的十个最佳实践
查看>>
全面了解大数据“三驾马车”的开源实现
查看>>
脱离“体验”和“安全”谈盈利的游戏运营 都是耍流氓
查看>>
慎用!BLEU评价NLP文本输出质量存在严重问题
查看>>
基于干净语言和好奇心的敏捷指导
查看>>