Cod sursa(job #471364)

Utilizator ChallengeMurtaza Alexandru Challenge Data 18 iulie 2010 13:57:22
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

const char InFile[]="sate.in";
const char OutFile[]="sate.out";
const int MaxN=30005;

struct stype{int to,cost;};

ifstream fin(InFile);
ofstream fout(OutFile);

vector<stype> v[MaxN];
stype s;
int n,m,x,y,a,b,cost,viz[MaxN];

void bfs(int nod)
{
	viz[nod]=1;
	queue<int> q;
	q.push(nod);
	while(!q.empty())
	{
		nod=q.front();
		q.pop();
		for(register int i=0;i<(int)v[nod].size();++i)
		{
			if(!viz[v[nod][i].to])
			{
				viz[v[nod][i].to]=viz[nod]+v[nod][i].cost;
				q.push(v[nod][i].to);
			}
		}
	}
}

int main()
{
	fin>>n>>m>>x>>y;
	for(register int i=0;i<m;++i)
	{
		fin>>a>>b>>cost;
		if(a>b){int aux=a;a=b;b=aux;}
		s.cost=cost;
		s.to=b;
		v[a].push_back(s);
		s.cost=-cost;
		s.to=a;
		v[b].push_back(s);
	}
	fin.close();

	bfs(x);

	fout<<viz[y]-1;
	fout.close();
	return 0;
}