Cod sursa(job #2772480)

Utilizator popoviciAna16Popovici Ana popoviciAna16 Data 1 septembrie 2021 12:24:04
Problema Amenzi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

ifstream fin ("amenzi.in");
ofstream fout ("amenzi.out");

vector <pair <int, int>> v[151];
int a[151][3501];
int dp[3501][151]; //dp[i][j] = profitul maxim pe care-l pot obtine, daca la timpul i ma aflu in nodul j

int main()
{
    int n, m, k, p;
    int i, j;
    int x, y, z;

    fin >> n >> m >> k >> p;
    for (i = 1; i<=m; i++)
    {
        fin >> x >> y >> z;
        v[x].push_back({y, z});
        v[y].push_back({x, z});
    }

    for (i = 1; i<=k; i++)
    {
        fin >> x >> y >> z;
        a[x][y] += z;
    }

    for (i = 0; i<=3500; i++)
        for (j = 1; j<=n; j++)
            dp[i][j] = -1;

    dp[0][1] = 0;
    for (i = 1; i<=3500; i++)
        for (j = 1; j<=n; j++)
        {
            dp[i][j] = dp[i-1][j];
            for (x = 0; x<v[j].size(); x++)
            {
                y = v[j][x].first;
                z = v[j][x].second;
                if (i >= z)
                    dp[i][j] = max(dp[i][j], dp[i-z][y]);
            }
            if (dp[i][j] != -1)
                dp[i][j] = dp[i][j] + a[j][i];
        }

    for (i = 1; i<=p; i++)
    {
        fin >> x >> y;
        fout << dp[y][x] << '\n';
    }
    return 0;
}