HDU 3072 //图的强连通性,缩点后求树形图 //TARJAN算法
来源:互联网 发布:流量数据分析 编辑:程序博客网 时间:2024/06/06 01:03
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Intelligence System
Total Submission(s): 233 Accepted Submission(s): 104
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.
#include<stdio.h>
#include<string.h>
#define MAX_SIZE 50010
#define INF 100000000
int stack[MAX_SIZE];
bool instack[MAX_SIZE];
int dist[MAX_SIZE];
int N,M,b_cnt,idx,top;
struct Edge
{
int adj,val;//adj
Edge* next;//point to next
};
struct Node
{
int DFN,LOW;//定义DFN(u)为节点u搜索的次序编号(时间戳),Low(u)为u或u的子树能够追溯到的最早的栈中节点的次序号。
int belongs;//belongs to B_cnt,属于哪一个强联通分支
Edge *first;//point to first,形成链状
}node[MAX_SIZE];
int min(int a,int b)
{
if(a<b) return a;
return b;
}
void Init() //initial node[],in[],out[]
{
for(int i=0; i<=N; i++)
{
node[i].first=NULL;
node[i].DFN=node[i].LOW=0;
node[i].belongs=0;
instack[i]=false;
dist[i]=INF;
}
}
void InsertEdge(int u,int v,int c) //insert edge u->v
{
Edge*p=new Edge;
p->adj=v;//指向的下一个节点
p->val=c;
p->next=node[u].first;
node[u].first=p;
}
void Tarjan(int u) //Tarjan
{
int v;
node[u].DFN=node[u].LOW=(++idx);
instack[u]=true;
stack[++top]=u;
for(Edge*p=node[u].first; p; p=p->next)
{
v=p->adj;
if(!node[v].DFN)
{
Tarjan(v);
if(node[v].LOW<node[u].LOW)
node[u].LOW=node[v].LOW;
}
else if(instack[v]&&node[v].DFN<node[u].LOW)
node[u].LOW=node[v].DFN;
}
if(node[u].DFN==node[u].LOW)
{
b_cnt++;
do
{
v=stack[top--];
instack[v]=false;
node[v].belongs=b_cnt;
}
while(u!=v);
}
}
void Tarjan_SCC() //StronglyConnectedComponent
{
int i,t1,t2;
b_cnt=idx=top=0;//b_cnt 新图的点数
for(i=1; i<=N; i++)
if(!node[i].DFN)
Tarjan(i);
}
int main()
{
int v,w;
while(scanf("%d%d",&N,&M)!=EOF)
{
Init();
for(int i=0; i<M; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
InsertEdge(a,b,c);
}
Tarjan_SCC();
for(int i=0;i<N;i++)
{
for(Edge *p=node[i].first;p;p=p->next)
{
v=p->adj;
w=p->val;
if(node[i].belongs!=node[v].belongs) dist[node[v].belongs]=min(dist[node[v].belongs],w);
}
}
int sum=0;
for(int i=1;i<=b_cnt;i++)
{
if(dist[i]==INF) dist[i]=0;
sum+=dist[i];
}
printf("%d/n",sum);
}
return 0;
}
- HDU 3072 //图的强连通性,缩点后求树形图 //TARJAN算法
- HDU 3072 //图的强连通性,缩点后求树形图 //kosaraju算法
- 图的连通性算法(Tarjan)
- 图的连通性问题之tarjan算法
- 图的强连通性:Korasaju算法实现
- POJ1236图的连通性Tarjan
- 图的连通性tarjan模板
- 强连通性Tarjan算法和LCA
- 图的连通性问题&tarjan求强连通分量、割点、桥
- 强联通图的Tarjan算法
- 图的连通性-tarjan模板(2)
- Tarjan求图的连通性总结
- HDU OJ 1269 迷宫城堡【有向图强连通分量的Tarjan算法 入门】
- hdu 1269 迷宫城堡 tarjan算法求有向图的强连通分量
- hdu 1269 tarjan算法 图的强连通之迷宫城堡
- 连通性问题 之 Tarjan算法求强连通分量
- 连通性Tarjan算法 双连通与强连通
- 有向图的强连通算法 -- tarjan算法
- mysql 编译参数优化
- jsp servlet mysql fckeditor等配合使用时出现中文乱码的解决问题!
- pushlet基本流程
- 我来了 H5站长。。做采集的
- spring2.0常见异常
- HDU 3072 //图的强连通性,缩点后求树形图 //TARJAN算法
- 用Java实现的一个本地文件浏览器
- Eclipse快捷键大全(转载)
- 最近在研究AMF数据结构,因为菜,所以先编写了一个设置代理服务器的东东。delphi的。
- C#(.Net) 连接 ORACLE数据库
- jQuery绑定事件的命名空间
- 一个简单的Web应用程序
- 入门 --ubuntu下面mysql数据库安装以及相关操作
- CDMA模块通讯正常