Cod sursa(job #1698579)

Utilizator akaprosAna Kapros akapros Data 4 mai 2016 20:07:55
Problema Pitici Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.81 kb
#include <bits/stdc++.h>
#define maxN 1021
#define maxM 200021
#define pii pair < int, int >
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define inf 2000000002
using namespace std;
int n, m, k, v[maxN], dp[maxN][maxN], nw[maxN][maxN];
vector < pii > V[maxN];
vector < int > P[maxN];
void read()
{
    freopen("pitici.in", "r", stdin);
    scanf("%d %d %d", &n, &m, &k);
    while (m --)
    {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        P[b].pb(0);
        V[b].pb(mp(a, c));
    }
}
void solve(int nod, int x)
{
    int Nw = 0, ans = inf;
    if (nod == 1)
    {
        if (x == 1)
    {
        dp[nod][x] = 0;
        nw[nod][x] = 1;
    }else
    {
        dp[nod][x] = inf;
        nw[nod][x] = 0;
    }
    return ;
    }
    int i;
    for (i = 0; i < P[nod].size(); ++ i)
    {
        int Nod = V[nod][i].f, w = P[nod][i], cost = V[nod][i].s;
        if (w == 0 || (dp[Nod][w] + cost == dp[nod][x - 1]))
        {
           ++ P[nod][i];
            if (dp[Nod][w + 1] == 0)
                solve(Nod, w + 1);
        }
    }
    for (i = 0; i < V[nod].size(); ++ i)
    {
        int Nod = V[nod][i].f, cost = V[nod][i].s, w = P[nod][i];

        if(dp[Nod][w] + cost < ans)
        {
            Nw = nw[Nod][w];
            ans = dp[Nod][w] + cost;
        }else
        if(dp[Nod][w] + cost == ans)
            Nw += nw[Nod][w];

    }
    dp[nod][x] = ans;
    nw[nod][x] = Nw;
}
void write()
{
    int i, step = 0;
    freopen("pitici.out", "w", stdout);
    while (k)
    {
        solve(n, ++ step);
        for (i = 1; i <= nw[n][step] && k; ++ i)
        {
            -- k;
            printf("%d ", dp[n][step]);
        }
    }

}
int main()
{
    read();
    write();
    return 0;
}