Cod sursa(job #2390263)

Utilizator flaviu_2001Craciun Ioan-Flaviu flaviu_2001 Data 27 martie 2019 21:15:02
Problema Pitici Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.52 kb
#include <bits/stdc++.h>
#define ff first
#define ss second

using namespace std;
typedef pair<int, int> pi;
const int nmax = 1025, inf = 2147483647;

class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};

class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }


public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32) {
        if(vu32 == -1){
            write_ch('-');
            write_ch('1');
        } else if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};

struct thing{
    int nod, first, val;
    bool operator>(const thing &obj)const{
        return val > obj.val;
    }
};

int n, m, k, pd[nmax];
bool ok[nmax];
vector<pi> v[nmax], pdl[nmax];
priority_queue< thing, vector<thing>, greater<thing> > q;

void dfs(int x)
{
    ok[x] = 1;
    pd[x] = inf;
    if(x == n)
        pd[x] = 0;
    for (auto y : v[x]){
        if(!ok[y.ff])
            dfs(y.ff);
        if(pd[y.ff] != inf){
            pd[x] = min(pd[x], pd[y.ff]+y.ss);
            pdl[x].push_back({pd[y.ff]+y.ss, y.ff});
        }
    }
}

int main()
{
    InParser fin ("pitici.in");
    OutParser fout ("pitici.out");
    fin >> n >> m >> k;
    for (int i = 1; i <= m; ++i){
        int x, y, c;
        fin >> x >> y >> c;
        v[x].push_back({y, c});
    }
    dfs(1);
    for (int i = 1; i <= n; ++i)
        sort(pdl[i].begin(), pdl[i].end());
    thing baga;
    baga.nod = 1;
    baga.first = 0;
    baga.val = pdl[1][0].ff;
    q.push(baga);
    for (int t = 1; t <= k; ++t){
        thing now = q.top();
        q.pop();
        fout << now.val << " ";
        if(now.first+1 < pdl[now.nod].size()){
            baga.nod = now.nod;
            baga.first = now.first+1;
            baga.val = now.val + pdl[now.nod][now.first+1].ff-pdl[now.nod][now.first].ff;
            q.push(baga);
        }
        int x = pdl[now.nod][now.first].ss;
        while(x != n){
            if(pdl[x].size() > 1){
                baga.nod = x;
                baga.first = 1;
                baga.val = now.val + pdl[x][1].ff-pdl[x][0].ff;
                q.push(baga);
            }
            x = pdl[x][0].ss;
        }
    }
    return 0;
}