Cod sursa(job #2313918)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 7 ianuarie 2019 17:15:56
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <bits/stdc++.h>
using namespace std;

	
class InParser 
{
	
private:
	
	FILE *fin;
	
	char *buff;
	int sp;
	
	char read_ch() 
	{
		sp++;
	
		if(sp == 4096) 
		{
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
	
		return buff[sp];
	}
	
public:
	
	InParser(const char* nume) 
	{
		fin = fopen(nume, "r");
		
		buff = new char[4096]();
		sp = 4095;
	}
	
	InParser& operator >> (int &n) 
	{
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		
		n = c - '0';
	
		while (isdigit(c = read_ch())) 
			n = 10 * n + c - '0';
	
		return *this;
	}
};

ofstream out("sate.out");

const int DIM = 1e5 + 27;

struct Drum
{
	int x, y, d;
};

Drum v[DIM];

int pos[DIM];

int n, m, x, y, i;

int main()
{
	InParser fin("sate.in");
	
	fin >> n >> m >> x >> y;
	
	for(i = 1; i <= m; i++)
	{
		fin >> v[i].x >> v[i].y >> v[i].d;
	}
	
	pos[x] = 1;
	
	while(pos[y] == 0)
	{
		for(i = 1; i <= m; i++)
			if(pos[v[i].x] != 0 && pos[v[i].y] == 0)
				pos[v[i].y] = pos[v[i].x] + v[i].d;
			else
				if(pos[v[i].y] != 0 && pos[v[i].x] == 0)
					pos[v[i].x] = pos[v[i].y] - v[i].d;
	}
	
	out << pos[y] - 1;
	
}