Cod sursa(job #1282589)

Utilizator smaraldaSmaranda Dinu smaralda Data 4 decembrie 2014 15:26:19
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include<stdio.h>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;

#define ITERATOR vector <EDGE>::iterator
const int NMAX = 250005;

struct EDGE {
	int node, cost;
	EDGE (int node = 0, int cost = 0) {
		this->node = node;
		this->cost = cost;
	}
};

vector <EDGE> graph[NMAX];
int x, y, lim, d[NMAX];
bool vis[NMAX];
queue <int> q[1005];

char ln[100];
int p;

void getint(int &x) {
	x = 0;
	while(ln[p] >= '0' && ln[p] <= '9') {
		x = x * 10 + ln[p] - '0';
		++ p;
	}
	++ p;
}

int main() {
	freopen("pscnv.in", "r", stdin);
	freopen("pscnv.out", "w", stdout);
	int n, m, i, c, a, b, node;

	scanf("%d%d%d%d\n", &n, &m, &x, &y);
	for(i = 1; i <= m; ++ i) {
		gets(ln);
		p = 0;
		getint(a);
		getint(b);
		getint(c);
		graph[a].push_back(EDGE(b, c));
	}


	q[0].push(x);
	for(lim = 0; lim <= 1000; ++ lim)
		while(!q[lim].empty()) {
			node = q[lim].front();
			q[lim].pop();

			if(node == y) {
				printf("%d\n", lim);
				return 0;
			}

			if(vis[node])
				continue;

			for(ITERATOR it = graph[node].begin(); it != graph[node].end(); ++ it)
				if(d[it->node] > max(d[node], it->cost) || (d[it->node] == 0 && it->node != x)) {
					d[it->node] = max(d[node], it->cost);
					q[d[it->node]].push(it->node);
				}
		}
	return 0;
}