Cod sursa(job #1473058)

Utilizator IulianBoboUAIC Boboc Iulian IulianBobo Data 18 august 2015 14:14:57
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include<iostream>
#include<fstream>
using namespace std;

#define MAX_N 30001
#define INF 20000001

typedef struct graph{int nod;long cost;graph *next;} Graph;
Graph *element[MAX_N];

int n,x,y,vizitat[MAX_N],dist[MAX_N];
long m;

ifstream fin("sate.in");
ofstream fout("sate.out");

void add(int a,int b,long cost)
{
	Graph *newNode = new graph;
	newNode->nod = b;
	newNode->cost = cost;
	newNode->next = element[a];
	element[a] = newNode;
}

void readData()
{
	int from,to;
	long cost;
	
	fin>>n>>m>>x>>y;
	
	for(int i = 0;i < m; ++i)
	{
		fin>>from>>to>>cost;
		add(from,to,cost);
		add(to,from,-cost);
	}
}

void computeSolution()
{
	for(int i = 1;i <= MAX_N;++i)
	{
		dist[i] = INF;
	}
	dist[x] = 0;
	
	int u,p,Q[MAX_N],currentNode;
	u = p = 1;
	Q[u] = x;
	
	while(u<=p)
	{
		currentNode = Q[u];
		for(Graph *it = element[currentNode];it;it = it->next)
		{
			if(dist[it->nod] > dist[currentNode] + it->cost && !vizitat[it->nod])
			{
				dist[it->nod] = dist[currentNode] + it->cost;
				Q[++p] = it->nod;
			}
		}
		vizitat[currentNode] = 1;
		++u;
	}
	fout<<dist[y];
}

int main()
{
	readData();
	computeSolution();
	return 0;
}