Cod sursa(job #2447153)

Utilizator Dobricean_IoanDobricean Ionut Dobricean_Ioan Data 12 august 2019 12:01:13
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <fstream>
#include <algorithm>

using namespace std;

//ifstream fin ("sate.in");
ofstream fout ("sate.out");

const int MAXN = 3000001;
int dp[MAXN],n,m,x,y;

struct muc {

    int x,y,d;

};
muc a[MAXN];
#include <stdio.h>
#include <ctype.h>

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 != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}

	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};
InParser fin("sate.in");
int main() {

    fin >> n >> m >>  x >> y;
    for ( int i = 1; i <= m; ++i)
        fin >> a[i].x >> a[i].y >> a[i].d;

    dp[x] = 1;
    while ( !dp[y]) {
        for ( int i = 1; i <= m; ++i)
            if (!dp[a[i].y] and dp[a[i].x])
                dp[a[i].y] = dp[a[i].x]+ a[i].d;
            else
            if (!dp[a[i].x] and dp[a[i].y]) {
                dp[a[i].x] = dp[a[i].y] - a[i].d;
            }
    }
    fout << dp[y]-1;
}