HDU 3072 Intelligence System(强连通分量)

题意:给你一个有向网络,网络中的每条有向边都有一个代价,表示从u向v发信息的代价.现在你要从0号点将信息发到所有的其他点去,问你最小代价是多少.其中如果u与v点可以互达(即属于同一个强连通分量),那么他们之间的通信不需要花代价. 输入保证从0号点能到达所有其他点.

思路:求出图中所有强连通分量,缩点,维护每个缩点的最小值相加

Trick:此题虽然有重边,可是没影响


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
#include <stack>
using namespace std;
#define maxn 50000+100
#define LL long long
int cas=1,T;
struct Edge
{
	int v,cost;
	Edge(int v,int c):v(v),cost(c){}
};
vector<Edge>G[maxn];
int pre[maxn];
int lowlink[maxn];
int sccno[maxn];
int dfs_clock,scc_cnt;
int n,m;
const int INF = 1<<29;
stack<int>S;
void dfs(int u)
{
	pre[u]=lowlink[u]=++dfs_clock;
	S.push(u);
	for (int i = 0;i<G[u].size();i++)
	{
		int v = G[u][i].v;
		if (!pre[v])
		{
			dfs(v);
			lowlink[u] = min(lowlink[u],lowlink[v]);
		}
		else if (!sccno[v])
		{
			lowlink[u] = min (lowlink[u],pre[v]);
		}
	}
	if (lowlink[u] == pre[u])
	{
		scc_cnt++;
		for (;;)
		{
			int x = S.top();S.pop();
			sccno[x] = scc_cnt;
			if (x==u)
				break;
		}
	}
}

void find_scc(int n)
{
	dfs_clock=scc_cnt=0;
	memset(sccno,0,sizeof(sccno));
	memset(pre,0,sizeof(pre));
	for (int i = 0;i<n;i++)
		if (!pre[i])
			dfs(i);
}
int in[maxn];
int out[maxn];
int ans[maxn];
int main()
{
	//freopen("in","r",stdin);
//	scanf("%d",&T);
	while (scanf("%d%d",&n,&m)==2)
	{
		for (int i = 0;i<=n;i++)
			G[i].clear();
		memset(in,0,sizeof(in));
		memset(ans,0,sizeof(ans));
		memset(out,0,sizeof(out));
		for (int i = 1;i<=m;i++)
		{
			int u,v,cost;
			scanf("%d%d%d",&u,&v,&cost);
			G[u].push_back(Edge(v,cost));
		}
		find_scc(n);
		for (int i = 1;i<=scc_cnt;i++)
		{
			ans[i]=INF;
		}
		for (int u = 0;u<n;u++)
			for (int j=0;j<G[u].size();j++)
			{
				int v = G[u][j].v;
				if (sccno[u] != sccno[v])
				{
					ans[sccno[v]]=min(ans[sccno[v]],G[u][j].cost);
				}
			}

		int sum=0;
        for (int i = 1;i<=scc_cnt;i++)
		if (ans[i]!=INF)
		sum+=ans[i];
     	printf("%d\n",sum);
	}
	return 0;
}

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
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值