Cod sursa(job #818889)

Utilizator alex_unixPetenchea Alexandru alex_unix Data 18 noiembrie 2012 11:15:05
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb

#include <cstdio>

const int MAX_SIZE(30001);

struct edge
{
	int vertex;
	int cost;
	struct edge *next;
} *graph [MAX_SIZE];

int queue [MAX_SIZE];
int cost [MAX_SIZE];

int n, m, start, finish;

inline void add (int x, int y, int cost)
{
	struct edge *new_edge(new struct edge);
	new_edge->vertex = y;
	new_edge->cost = cost;
	new_edge->next = graph[x];
	graph[x] = new_edge;
}

inline void read (void)
{
	std::freopen("sate.in","r",stdin);
	std::scanf("%d%d%d%d",&n,&m,&start,&finish);
	int x, y, cost;
	for (int counter(0) ; counter < m ; ++counter)
	{
		std::scanf("%d%d%d",&x,&y,&cost);
		add(x,y,cost);
		add(y,x,cost);
	}
	std::fclose(stdin);
}

inline void print (void)
{
	std::freopen("sate.out","w",stdout);
	std::printf("%d\n",cost[finish] - 1);
	std::fclose(stdout);
}

inline void BreadthFirstSearch (void)
{
	*queue = start;
	cost[start] = 1;
	int *push(queue + 1), *pop(queue), vertex, new_cost;
	struct edge *iterator;
	while (pop < push)
	{
		vertex = *pop;
		++pop;
		for (iterator = graph[vertex] ; iterator ; iterator = iterator->next)
		{
			if (cost[iterator->vertex])
				continue;
			new_cost = cost[vertex];
			if (iterator->vertex > vertex)
				new_cost += iterator->cost;
			else
				new_cost -= iterator->cost;
			cost[iterator->vertex] = new_cost;
			*push = iterator->vertex;
			++push;
		}
	}
}

int main (void)
{
	read();
	BreadthFirstSearch();
	print();
	return 0;
}