码迷,mamicode.com
首页 > 其他好文 > 详细

hdu3072

时间:2014-05-09 01:42:39      阅读:372      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

Intelligence System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1250    Accepted Submission(s): 560


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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=50000+10;
int n,m,size,top,index;
int u[MAX*2],v[MAX*2],w[MAX*2];
int head[MAX],dfn[MAX],low[MAX],dp[MAX];
int mark[MAX],stack[MAX];
bool ind[MAX];//ind标记缩点后的点是否有入度 

struct Edge{
	int v,w,next;
	Edge(){}
	Edge(int V,int W,int NEXT):v(V),w(W),next(NEXT){}
}edge[MAX*2];

void Init(int num){
	for(int i=0;i<=num;++i)head[i]=-1,mark[i]=0,dp[i]=INF;
	size=top=index=0;
}

void InsertEdge(int u,int v,int w){
	edge[size]=Edge(v,w,head[u]);
	head[u]=size++;
}

void tarjan(int u){
	if(mark[u])return;
	dfn[u]=low[u]=++index;
	stack[++top]=u;
	mark[u]=1;
	for(int i=head[u];i != -1;i=edge[i].next){
		int v=edge[i].v;
		tarjan(v);
		if(mark[v] == 1)low[u]=min(low[u],low[v]);
	}
	if(dfn[u] == low[u]){
		while(stack[top] != u){
			mark[stack[top]]=-1;
			low[stack[top--]]=low[u];
		}
		mark[u]=-1;
		--top;
	}
}

void dfs(int u){
	if(mark[u])return;
	mark[u]=1;
	for(int i=head[u];i != -1;i=edge[i].next){
		int v=edge[i].v,w=edge[i].w;
		if(w<dp[v])dp[v]=w;
		dfs(v);	
	}
}

int main(){
	while(~scanf("%d%d",&n,&m)){
		Init(n);
		for(int i=0;i<m;++i){
			scanf("%d%d%d",&u[i],&v[i],&w[i]);
			InsertEdge(u[i],v[i],w[i]);
		}
		for(int i=0;i<n;++i){//求连通分量进行缩点 
			if(mark[i])continue;
			tarjan(i);
		}
		
		for(int i=0;i<=n;++i)head[i]=-1,ind[i]=false,mark[i]=0;
		size=0;
		for(int i=0;i<m;++i){//缩点重建图 
			if(low[u[i]] == low[v[i]])continue;
			ind[low[v[i]]]=true;
			InsertEdge(low[u[i]],low[v[i]],w[i]);
		}
		for(int i=0;i<n;++i){
			if(!ind[low[i]])dp[low[i]]=0,dfs(low[i]);
			ind[low[i]]=true;
		}
		int sum=0;
		for(int i=0;i<n;++i){
			if(ind[low[i]])sum+=dp[low[i]];
			ind[low[i]]=false;
		}
		cout<<sum<<endl;
	}
	return 0;
}



hdu3072,布布扣,bubuko.com

hdu3072

标签:des   style   blog   class   code   java   

原文地址:http://blog.csdn.net/xingyeyongheng/article/details/25339103

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!