Cod sursa(job #2449129)

Utilizator CharacterMeCharacter Me CharacterMe Data 18 august 2019 12:32:57
Problema Amenzi Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <bits/stdc++.h>
#define pii pair<int, int>
#define tiii tuple<int, int, int>
#define node1 get<0>
#define node2 get<1>
#define time get<2>
///N=150
///M=1500
///T=3500
using namespace std;
int n, m, k, p, i, j, t=-1;
int sol[151][3501], costs[151][3501];
tiii edges[1501];
pii pairs[8001];
///
void read();
void solve();
void write();
int main()
{
    read();
    solve();
    write();
    return 0;
}
void read(){
    freopen("amenzi.in", "r", stdin);
    scanf("%d%d%d%d", &n, &m, &k, &p);
    for(i=1; i<=m; ++i){
        int x, y, c;
        scanf("%d%d%d", &x, &y, &c);
        edges[i]=make_tuple(x, y, c);
    }
    for(i=1; i<=k; ++i){
        int x, y, c;
        scanf("%d%d%d", &x, &y, &c);
        costs[x][y]=c;
    }
    for(i=1; i<=p; ++i){
        int x, y;
        scanf("%d%d", &x, &y);
        t=max(t, y);
        pairs[i]={x, y};
    }
    fclose(stdin);
}
void solve(){
    sol[1][0]=costs[1][0];
    for(i=1; i<=t; ++i){
        for(j=1; j<=m; ++j){
            int past=i-time(edges[j]);
            if(past<0) continue;
            int n1=node1(edges[j]), n2=node2(edges[j]);
            if(sol[n2][past] || (n2==1 && past==0))sol[n1][i]=max(sol[n1][i], sol[n2][past]+costs[n1][i]);
            if(sol[n1][past] || (n1==1 && past==0))sol[n2][i]=max(sol[n2][i], sol[n1][past]+costs[n2][i]);
        }
        for(j=1; j<=n; ++j) sol[j][i]=max(sol[j][i], sol[j][i-1]);
    }
}
void write(){
    freopen("amenzi.out", "w", stdout);
    for(i=1; i<=p; ++i) printf("%d\n", sol[pairs[i].first][pairs[i].second]);
    fclose(stdout);
}