Cod sursa(job #885174)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 21 februarie 2013 18:16:27
Problema Pitici Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <cstdio>
#include <cstring>
#include <cassert>

#include <vector>
#include <queue>

#define v first
#define c second

using namespace std;

typedef pair<int, int> Edge;
typedef vector<Edge>::iterator it;

const int MAX_N = 1050;
const int MAX_K = 1050;
const int oo = 0x3f3f3f3f;

vector<Edge> G[MAX_N];
int N, K, Start, End, PathCost[MAX_N][MAX_K];
int Cost[MAX_N], Index[MAX_N];
bool Computed[MAX_N];

struct Compare {
    bool operator() (const int X, const int Y) const {
        return Cost[X] > Cost[Y];
    }
};

void Solve(int X) {
    Computed[X] = true;
    if (X == End) {
        PathCost[X][0] = 0;
        return;
    }
    if (G[X].empty())
        return;
    priority_queue<int, vector<int>, Compare> Heap;
    for (it Y = G[X].begin(); Y != G[X].end(); ++Y) {
        if (!Computed[Y->v])
            Solve(Y->v);
        Cost[Y->v] = Y->c + PathCost[Y->v][0], Index[Y->v] = 0;
        Heap.push(Y->v);
    }
    for (int i = 0; i < K; ++i) {
        int Y = Heap.top();
        PathCost[X][i] = Cost[Y];
        Heap.pop();
        Cost[Y] -= PathCost[Y][Index[Y]];
        ++Index[Y];
        Cost[Y] += PathCost[Y][Index[Y]];
        Heap.push(Y);
    }
}

void Read() {
    assert(freopen("pitici.in", "r", stdin));
    int M; assert(scanf("%d %d %d", &N, &M, &K) == 3);
    for (; M > 0; --M) {
        int X, Y, C; assert(scanf("%d %d %d", &X, &Y, &C) == 3);
        G[X].push_back(Edge(Y, C));
    }
    Start = 1, End = N;
}

void Print() {
    assert(freopen("pitici.out", "w", stdout));
    for (int i = 0; i < K; ++i)
        printf("%d ", PathCost[Start][i]);
    printf("\n");
}

int main() {
    Read();
    memset(PathCost, oo, sizeof(PathCost));
    Solve(Start);
    Print();
    return 0;
}