Cod sursa(job #1701598)

Utilizator alexandra_udristoiuUdristoiu Alexandra Maria alexandra_udristoiu Data 13 mai 2016 17:04:33
Problema Pitici Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.25 kb
#include<fstream>
#include<vector>
#define f first
#define s second
using namespace std;
int n, k, m, i, j, a, b, c, nr;
int h[1020], fr[1020], w[1020], u[1020], vec[1020], cost[1020], poz[1020];
vector< pair<int, int> > v[1020], v1[1020];
vector<int> d[1020];
ifstream fin("pitici.in");
ofstream fout("pitici.out");
void dfs(int nod){
    fr[nod] = 1;
    for(int i = 0; i < v[nod].size(); i++){
        if(fr[ v[nod][i].f ] == 0){
            dfs(v[nod][i].f);
        }
    }
    w[++nr] = nod;
}
void adaugare(int pos){
    int p, c;
    c = pos;
    p = c / 2;
    while(p > 0 && d[ h[c] ][ u[ h[c] ] ] + cost[ h[c] ] < d[ h[p] ][ u[ h[p] ] ] + cost[ h[p] ]){
        swap(h[p], h[c]);
        poz[ h[p] ] = p;
        poz[ h[c] ] = c;
        c = p;
        p = c / 2;
    }
}
void coborare(int pos, int nr){
    int p, c;
    p = pos;
    c = 2 * p;
    while(c <= nr){
        if(c + 1 <= nr && d[ h[c] ][ u[ h[c] ]  ]+ cost[ h[c] ] > d[ h[c + 1] ][ u[ h[c + 1] ] ]+ cost[ h[c + 1] ]){
            c++;
        }
        if(d[ h[c] ][ u[ h[c] ] ]+ cost[ h[c] ] < d[ h[p] ][ u[ h[p] ] ]+ cost[ h[p] ]){
            swap(h[p], h[c]);
            poz[ h[p] ] = p;
            poz[ h[c] ] = c;
            p = c;
            c = 2 * p;
        }
        else{
            break;
        }
    }
}
void ff(int nod){
    int i, nr = 0;
    for(i = 0; i < v1[nod].size(); i++){
        nr++;
        vec[nr] = v1[nod][i].f;
        cost[ vec[nr] ] = v1[nod][i].s;
        u[ vec[nr] ] = 0;
        poz[nr] =  nr;
        h[nr] = vec[nr];
        adaugare(nr);
    }
    for(i = 1; i <= k; i++){
        if(u[ h[1] ] == d[ h[1] ].size() - 1){
            break;
        }
        d[nod].push_back(d[ h[1] ][ u[ h[1] ] ] + cost[ h[1] ]);
        u[ h[1] ]++;
        coborare(1, nr);
    }
    d[nod].push_back(1000000000);
}
int main(){
    fin>> n >> m >> k;
    for(i = 1; i <= m; i++){
        fin>> a >> b >> c;
        v[a].push_back( make_pair(b, c) );
        v1[b].push_back( make_pair(a, c));
    }
    dfs(1);
    d[1].push_back(0);
    d[1].push_back(1000000000);
    for(i = n - 1; i >= 1; i--){
        ff(w[i]);
    }
    for(i = 0; i < k; i++){
        fout<< d[n][i] <<" ";
    }
    return 0;
}