Cod sursa(job #812836)

Utilizator toranagahVlad Badelita toranagah Data 14 noiembrie 2012 16:12:24
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
const int MAX_N = 30100;
const int MAX_M = 100100;
const int INF = 1 << 30;
struct edge {
	int n1, n2, c;
} edges[MAX_M];
vector<int> g[MAX_N];
int nodeCost[MAX_N];
queue<int> qu;
int N, M, X, Y;

void read();
void bfs(int node);

int main() {
	read();
	bfs(X);
	fout << nodeCost[Y];
}

void read() {
	fin >> N >> M >> X >> Y;
	for (int i = 1; i <= M; ++i) {
		fin >> edges[i].n1 >> edges[i].n2 >> edges[i].c;
		g[edges[i].n1].push_back(i);
		g[edges[i].n2].push_back(i);
	}
	for (int i = 1; i <= N; ++i) nodeCost[i] = INF;
	nodeCost[X] = 0;
}

void bfs(int node) {
	qu.push(node);
	while (!qu.empty()) {
		int v = qu.front(), neighbour, cost;
		qu.pop();
		for (unsigned int i = 0; i < g[v].size(); ++i) {
			neighbour = edges[g[v][i]].n1 + edges[g[v][i]].n2 - v;
			cost = edges[g[v][i]].c;
			if (neighbour > v) {
				cost = nodeCost[v] + cost; 
			} else {
				cost = nodeCost[v] - cost;
			}
			if (cost < nodeCost[neighbour]) {
				nodeCost[neighbour] = cost;
				qu.push(neighbour);
			}
		}
	}
}