Cod sursa(job #1219629)

Utilizator pulseOvidiu Giorgi pulse Data 14 august 2014 18:10:09
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

const int MAXN = 30005;
int N, M, X, Y, C[MAXN][MAXN], D[MAXN];
bool Used[MAXN];
queue <int> Q;

struct Graf
{
	int S;
	int C;
};

vector <Graf> G[MAXN];

void BFS()
{
	Q.push(X);
	Used[X] = 1;
	while (!Q.empty())
	{
		int node = Q.front(); Q.pop();
		if (node == Y)
		{
			printf("%d\n", D[Y]);
			return;
		}
		for (vector <Graf> :: iterator it = G[node].begin(); it != G[node].end(); ++it)
		{
			Graf i = *it;
			if (!Used[i.S])
			{
				Q.push(i.S);
				Used[i.S] = 1;
				D[i.S] = D[node] + i.C;
			}
		}
	}
}

Graf Insert(int s, int c)
{
	Graf aux;
	aux.S = s;
	aux.C = c;
	return aux;
}

int main()
{
	freopen("sate.in", "r", stdin);
	freopen("sate.out", "w", stdout);
	scanf("%d %d %d %d\n", &N, &M, &X, &Y);
	for (; M; --M)
	{
		int a, b, c;
		scanf("%d %d %d\n", &a, &b, &c);
		G[a].push_back(Insert(b, c));
		G[b].push_back(Insert(a, -c));
	}
	BFS();
	return 0;
}