Cod sursa(job #2718527)

Utilizator Andy_ANDYSlatinaru Andrei Alexandru Andy_ANDY Data 8 martie 2021 19:45:45
Problema Amenzi Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.17 kb
#include <bits/stdc++.h>
#define ll long long
//#define int ll
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 >> (short &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 f("amenzi.in");
ofstream g("amenzi.out");

short n,k,p,m;
int infractiune[155][3505];
vector < pair < short ,short > > G[155];
int dp[155][3505];

main()
{
    f>>n>>m>>k>>p;

    for(int i=1;i<=m;++i)
    {
        short a,b,c;
        f>>a>>b>>c;
        G[a].push_back({b,c});
        G[b].push_back({a,c});
    }

    for(int i=1;i<=k;++i)
    {
        short a,b,c;
        f>>a>>b>>c;
        infractiune[a][b]+=c;
    }

    for(int i=1;i<=n;++i)
        for(int j=0;j<=3500;++j) dp[i][j]=-1;
    dp[1][0]=infractiune[1][0];
    for(int timp = 1 ; timp<=3500; ++timp )
    {
        for(int nod = 1 ; nod<=n;++nod)
        {
            dp[nod][timp]=max(dp[nod][timp],dp[nod][timp-1]);
            for(auto x:G[nod])
            {
                int timpdrum = x.second;
                int vecin = x.first;
                if(timp>=timpdrum)
                    dp[nod][timp] = max(dp[nod][timp],dp[vecin][timp-timpdrum]);
            }
            if(dp[nod][timp]!=-1) dp[nod][timp]+=infractiune[nod][timp];
        }
    }

    for(int i=1;i<=p;++i)
    {
        short a,b;
        f>>a>>b;
        g<<dp[a][b]<<'\n';
    }

    return 0;
}