Cod sursa(job #1689652)

Utilizator SmarandaMaria Pandele Smaranda Data 14 aprilie 2016 14:15:15
Problema Pitici Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.04 kb
#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

const int N = 1022;

int dp [N][N], s [N], cost [N][N];
bool used [N];
vector <pair <int, int> > graph [N];

struct TEMPLATE {
    int cost, y, num;
};

class MyComp {
public :
    bool operator () (const TEMPLATE &A, const TEMPLATE &B) {
        return A.cost > B.cost;
    }
};

priority_queue <int, vector <TEMPLATE>, MyComp> pq;

void dfs (int x) {
    vector <pair <int, int> > :: iterator it;

    used [x] = true;
    for (it = graph [x].begin (); it != graph [x].end (); ++ it)
        if (!used [(*it).first])
            dfs ((*it).first);
    s [++ s [0]] = x;
}

int main () {
    int i, n, m, k, x, y, a, b, c, j;
    TEMPLATE temp;
    vector <pair <int, int> > :: iterator it;

    freopen ("pitici.in", "r", stdin);
    freopen ("pitici.out", "w", stdout);

    scanf ("%d%d%d", &n, &m, &k);
    for (i = 1; i <= m; i ++) {
        scanf ("%d%d%d", &a, &b, &c);
        cost [a][b] = c;
        graph [a].push_back (make_pair (b, c));
    }
    dfs (1);
    for (i = 1; i <= n; i ++)
        for (j = 1; j <= k; j ++)
            dp [i][j] = -1;
    dp [n][1] = 0;
    for (i = 1; i <= n; i ++) {
        x = s [i];
        for (it = graph [x].begin (); it != graph [x].end (); ++ it) {
            y = (*it).first;
            c = (*it).second;
            if (dp [y][1] != -1) {
                temp.cost = c + dp [y][1];
                temp.y = y;
                temp.num = 1;
                pq.push (temp);
            }
        }
        for (j = 1; j <= k && !pq.empty (); j ++) {
            temp = pq.top ();
            pq.pop ();
            dp [x][j] = temp.cost;
            temp.cost = cost [x][temp.y] + dp [temp.y][temp.num + 1];
            temp.num ++;
            if (dp [temp.y][temp.num] != -1)
                pq.push (temp);
        }
        while (!pq.empty ())
            pq.pop ();
    }
    for (i = 1; i <= k; i ++)
        printf ("%d ", dp [1][i]);
    printf ("\n");
    return 0;
}