Pagini recente » Cod sursa (job #1501827) | Cod sursa (job #551121) | Cod sursa (job #2227950) | Cod sursa (job #2531430) | Cod sursa (job #1610837)
# include <bits/stdc++.h>
# define NR 1050
using namespace std; ifstream f("pitici.in"); ofstream g("pitici.out"); struct elem { int ant, l, cost; }E; vector <int> v[NR]; vector <elem> HEAP; bool cmp (const elem &x, const elem &y) { return x.cost > y.cost; } void baga (int cost, int ant, int l) { E.cost=cost; E.ant=ant; E.l=l; HEAP.push_back(E); push_heap(HEAP.begin(), HEAP.end(), cmp); } void scoate () { pop_heap(HEAP.begin(), HEAP.end(), cmp); HEAP.pop_back(); } int i,j,n,m,x,y,k,ant,cost,VV,K,l; int C[NR][NR], a[NR][NR], ap[NR], ord[NR], L[NR]; void DFS (int k) { ap[k]=1; for (auto &x: v[k]) if (! ap[x]) DFS (x); ord[++VV]=k; } int main () { f>>n>>m>>K; for (i=1; i<=m; ++i) { f>>x>>y>>cost; C[y][x]=cost; v[y].push_back(x); } DFS(n); for (i=1; i<=VV; ++i) { k=ord[i]; HEAP.clear(); for (auto &x: v[k]) baga (a[x][1] + C[k][x], x, 1); while (HEAP.size() && L[k]<K) { cost=HEAP[0].cost; ant=HEAP[0].ant; l=HEAP[0].l; scoate(); a[k][++L[k]]=cost; if (l < L[ant]) { ++l; baga (a[ant][l] + C[k][ant], ant, l); } } } for (i=1; i<=K; ++i) g<<a[n][i]<<" "; g<<"\n"; return 0; }