Cod sursa(job #1282575)

Utilizator smaraldaSmaranda Dinu smaralda Data 4 decembrie 2014 15:13:40
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include<stdio.h>
#include<vector>
#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;
bool vis[NMAX];

bool dfs (int node) {
	vis[node] = 1;
	if(node == y)
		return 1;

	bool ans = 0;
	for(ITERATOR it = graph[node].begin(); it != graph[node].end(); ++ it)
		if(!vis[it->node] && it->cost <= lim)
			ans |= dfs(it->node);
	return ans;
}

int bs (int left, int right) {
	int mid, last;
	while(left <= right) {
		mid = (left + right) / 2;
		lim = mid;
		memset(vis, 0, sizeof(vis));
		if(dfs(x))
			right = mid - 1,
			last = mid;
		else
			left = mid + 1;
	}
	return last;
}

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;

	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));
	}

	printf("%d\n", bs(1, 1000));
	return 0;
}