Cod sursa(job #2472173)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 12 octombrie 2019 10:01:34
Problema Amenzi Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.73 kb
#include <cstdio>
#include <algorithm>

using namespace std;

const int BSZ = (1 << 17);
char buf[BSZ];
int ptr = BSZ;

char nextch()
{
    if (ptr == BSZ)
    {
        fread(buf, 1, BSZ, stdin);
        ptr = 0;
    }
    return buf[ptr++];
}

int read()
{
    int sgn = +1, val = 0;
    char ch = nextch();

    while (ch < '0' || '9' < ch)
    {
        if (ch == '-')
        {
            sgn *= -1;
        }
        ch = nextch();
    }

    while ('0' <= ch && ch <= '9')
    {
        val = 10 * val + ch - '0';
        ch = nextch();
    }

    return sgn * val;
}

const int N = 150;
const int INF = (int) 1e9;
int n, m, k, p, d[N][N];

struct event
{
    int t;
    int a;
    int c;
    int i;
};

bool operator < (event a, event b)
{
    return a.t < b.t;
}

const int K = 12000 + 7;
const int P = 8000 + 7;

event e[K + P];
int sol[P];
int dp[K + P], md[K + P];

int main()
{
    freopen ("amenzi.in", "r", stdin);
    freopen ("amenzi.out", "w", stdout);

    n = read();
    m = read();
    k = read();
    p = read();

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            d[i][j] = INF;
        }
        d[i][i] = 0;
    }

    for (int i = 0; i < m; i++)
    {
        int a = read();
        int b = read();
        int c = read();
        a--;
        b--;
        d[a][b] = d[b][a] = c;
    }

    for (int k = 0; k < n; k++)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                d[i][j] = d[j][i] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (d[i][j] != INF && d[i][j] > md[i])
            {
                md[i] = d[i][j];
            }
        }
    }

    int top = 0;
    for (int i = 0; i < k; i++)
    {
        e[top].a = read();
        e[top].a--;
        e[top].t = read();
        e[top].c = read();
        top++;
    }

    for (int i = 0; i < p; i++)
    {
        e[top].a = read();
        e[top].a--;
        e[top].t = read();
        e[top].i = i + 1;
        top++;
    }

    sort(e, e + top);

    for (int i = 1; i < top; i++)
    {
        dp[i] = -INF;
    }

    for (int i = 0; i < top; i++)
    {
        dp[i] += e[i].c;
        sol[e[i].i] = dp[i];
        for (int j = i + 1; j < top; j++)
        {
            if (e[i].t + d[e[i].a][e[j].a] <= e[j].t && dp[i] > dp[j])
            {
                dp[j] = dp[i];
            }
        }
    }

    for (int i = 0; i < p; i++)
    {
        printf("%d\n", sol[i + 1]);
    }

    return 0;
}