Cod sursa(job #9788)

Utilizator victorsbVictor Rusu victorsb Data 27 ianuarie 2007 16:57:09
Problema Amenzi Scor 30
Compilator cpp Status done
Runda Unirea 2007, clasele 11-12 Marime 1.51 kb
#include <cstdio>
#include <vector>
#define pb push_back
#define sz(a) int((a).size())

using namespace std;

#define Nmax 155
#define Mmax 1505
#define Tmax 3505
#define inf 1000000000

int n, m, k, p, i, j, a, b, c, timp;
int dm[Tmax][Nmax], am[Tmax][Nmax], cost[Nmax][Nmax];
vector<int> lv[Nmax];

void citire()
{
    scanf("%d %d %d %d\n", &n, &m, &k, &p);
    for (i=1; i<=m; ++i)
    {
        scanf("%d %d %d\n", &a, &b, &c);
        cost[a][b] = cost[b][a] = c;
        lv[a].pb(b);
        lv[b].pb(a);
    }
    for (i=1; i<=k; ++i)
    {
        scanf("%d %d %d\n", &a, &b, &c);
        am[b][a] = c;
    }
}

void solve()
{
    dm[0][1] = am[0][1];
    for (i=2; i<=n; ++i)
        dm[0][i] = - inf;
    for (timp=1; timp<=3500; ++timp)
    {
        for (i=1; i<=n; ++i)
        {
            dm[timp][i] = dm[timp-1][i];
            for (j=0; j<sz(lv[i]); ++j)
                if (timp - cost[i][lv[i][j]] >= 0)
                    if (dm[timp - cost[i][lv[i][j]]][lv[i][j]] > dm[timp][i])
                        dm[timp][i] = dm[timp - cost[i][lv[i][j]]][lv[i][j]];
            dm[timp][i] += am[timp][i];
        }
    }
}

void scrie()
{
    for (i=1; i<=p; ++i)
    {
        scanf("%d %d\n", &a, &b);
        if (dm[b][a] < 0)
            printf("-1\n");
        else
            printf("%d\n", dm[b][a]);
    }
}

int main()
{
    freopen("amenzi.in","r",stdin);
    freopen("amenzi.out","w",stdout);
    citire();
    solve();
    scrie();
    return 0;
}