Cod sursa(job #483361)

Utilizator andrei.dAndrei Diaconeasa andrei.d Data 8 septembrie 2010 11:11:23
Problema Reconst Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

#define file_in "reconst.in"
#define file_out "reconst.out"

#define nmax (1<<11)

int N,M;
int Q[nmax];
vector < pair< int, int > > G[nmax];
int viz[nmax];
int dist[nmax];


void adfile(void){
	
	int a,b,c;
	freopen(file_in,"r",stdin);
	freopen(file_out,"w",stdout);
	
	scanf("%d %d", &N, &M);
	while(M--){
		
		scanf("%d %d %d", &a, &b,&c);b++;
		G[a].push_back(make_pair(b,c));
		G[b].push_back(make_pair(a,-c));
	}
	
	fclose(stdin);
	
	return ;
	
}

void bfs(int nod){
	
	int p,u,x;
	p=u=0;
	
	viz[nod]=1;
	Q[0]=nod;
	
	while(p<=u){
		
		x=Q[p];
		p++;
		for (vector< pair <int,int> > ::iterator it=G[x].begin();it!=G[x].end();++it)
			 if (!viz[(it->first)])
			 {
				 viz[(it->first)]=1;
				 Q[++u]=(it->first);
				 dist[Q[u]]=dist[x]+(it->second);
			 }
	}
	
}


	

void solve(void){
	
	int i;
	for (i=1;i<=N+1;++i)
		 if (!viz[i])
			  bfs(i);
		 
	for (i=1;i<=N;++i)	 
		 printf("%d ", dist[i+1]-dist[i]);
	
}

int main(){
	
	adfile();
	solve();
	
	return 0;
	
}