HDU 3072 //图的强连通性,缩点后求树形图 //kosaraju算法
2010-09-07 18:45
393 查看
Intelligence System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 232 Accepted Submission(s): 103
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
2009 Multi-University Training Contest 17 - Host by NUDT
Recommend
lcy
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
const int N=50010;
const int inf=1 << 28;
int n, m;
struct Edge
{
int v, w;
}e;
vector<Edge> beg
;
vector<Edge> rebeg
;
void init()
{
for(int i=0;i<n;i++)
{
beg[i].clear();
rebeg[i].clear();
}
}
void add(int a, int b, int w)
{
e.v=b;
e.w=w;
beg[a].push_back(e);
e.v = a;
rebeg[b].push_back(e);
}
int order
;
int id
,dis
;
int cnt, scc;
void dfs(int u)
{
for(int i=0;i<beg[u].size();i++)
{
int v = beg[u][i].v;
if (!id[v])
{
id[v]=1;
dfs(v);
}
}
order[++cnt]=u;
}
void redfs(int u)
{
for(int i=0;i<rebeg[u].size();i++)
{
int v=rebeg[u][i].v;
if(!id[v])
{
id[v]=scc;
redfs(v);
}
}
}
void kosaraju(int n)
{
cnt=0;
memset(order,0,sizeof (order));
memset(id,0,sizeof (id));
for(int i=0;i<n;i++)
{
if(!id[i])
{
id[i]=1;
dfs(i);
}
}
memset(id,0,sizeof (id));
scc=0;
for(int i=cnt;i>0;i--)
{
int u=order[i];
if(!id[u])
{
id[u]=++scc;
redfs(u);
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=1;i<=m;i++)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);//a->b,权值为w
add(a,b,w);
}
kosaraju(n);
for (int i=1;i<=scc;i++) dis[i]=inf;
for(int u=0;u<n;u++)
{
for(int j=0;j<beg[u].size();j++)
{
int v=beg[u][j].v;
int w=beg[u][j].w;
if(id[u]!=id[v]) dis[id[v]]=min(dis[id[v]],w);
}
}
int ans=0;
for(int i=1;i<=scc;i++)
{
if(dis[i]==inf) dis[i]=0;//只有KTC一个人知道消息
ans+=dis[i];
}
printf("%d/n",ans);
}
return 0;
}
相关文章推荐
- HDU 3072 //图的强连通性,缩点后求树形图 //TARJAN算法
- hdu 3072 Intelligence System (强连通,缩点)
- HDU 3072 Intelligence System (强连通+(贪心||树形图))
- HDU 2242 连通分量缩点+树形dp
- hdu 3836 Equivalent Sets hdu 2767 Proving Equivalences 强连通缩点入门
- HDU 3072 Intelligence System (tarjan-scc缩点 + 最小树形图)
- HDU 3861 The King’s Problem(强连通缩点 + 最小路径覆盖)
- HDU 3072 2009多校联合17 强连通
- Intelligence System (hdu 3072 强联通缩点+贪心)
- 【HDU】4971 A simple brute force problem. 强连通缩点+最大权闭合子图
- hdu 1827 强连通缩点
- hdu 3062 Party【2-SAT入门---强连通Tarjan+染色缩点】
- HDU 1827 Summer Holiday 强连通缩点
- HDU 3639 强连通加缩点
- hdu-4635(有向图缩点+判断强连通)
- HDU 3072 Intelligence System (强连通缩点+简单最小树形图,4级)
- hdu 3639(强连通+缩点+建反向图)+hdu 3072(最小树形图)
- HDU 3072 连通分量+最小树形图
- HDU 3072 Intelligence System (强连通缩点+简单最小树形图,4级)
- The King’s Problem (hdu 3861 强连通缩点+最小路径覆盖)