Cod sursa(job #2777395)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 23 septembrie 2021 10:17:00
Problema Amenzi Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.43 kb
#include <bits/stdc++.h>

using namespace std;

ofstream fout("amenzi.out");


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

const int nmax = 155;
int n, m, k, p, infractiune[nmax][3505], dp[nmax][3505], minn[nmax];
vector <pair <int, int> > G[nmax];

int main(){
    InParser fin("amenzi.in");
    fin >> n >> m >> k >> p;
    for (int i = 1; i <= n; ++i){
        minn[i] = 1e9;
    }
    while (m--) {
        int a, b, c;
        fin >> a >> b >> c;
        G[a].push_back({b, c});
        G[b].push_back({a, c});
        minn[a] = min(minn[a], c);
        minn[b] = min(minn[b], c);
    }
    while (k--) {
        int a, b, c;
        fin >> a >> b >> c;
        infractiune[a][b] += c;
    }
    for (int j = 0; j <= 3500; ++j){
        for (int i = 1; i <= n; ++i){
            dp[i][j] = -1e9;
        }
    }
    dp[1][0] = infractiune[1][0];
    for (int timp = 1; timp <= 3500; ++timp){
        for (int i = 1; i <= n; ++i){
            dp[i][timp] = dp[i][timp - 1];
            if (timp - minn[i] >= 0){
                for (auto it : G[i]){
                int nod = it.first, cost = it.second;
                if (timp - cost >= 0)
                    dp[i][timp] = max(dp[i][timp], dp[nod][timp - cost]);
                }
            }
            if (dp[i][timp] >= 0) dp[i][timp] += infractiune[i][timp];
        }
    }
    for (int i = 1; i <= p; ++i){
        int a, b;
        fin >> a >> b;
        if (dp[a][b] >= 0)
            fout << dp[a][b] << "\n";
        else{
            fout << 0 << "\n";
        }
    }
    return 0;
}