Pagini recente » Cod sursa (job #351964) | Cod sursa (job #2584778) | Cod sursa (job #319929) | Cod sursa (job #967978) | Cod sursa (job #3002147)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("pitici.in");
ofstream fout("pitici.out");
const int dim=1e3+100,inf=1e7;
vector<pair<int,int>>adj[dim];
vector<pair<int,int>>rev_adj[dim];
bool visited[dim];
vector<int>TopSort;
void dfs(int x){
visited[x]=true;
for(auto[y,c]:adj[x]){
if(!visited[y]){
dfs(y);
}
}
TopSort.push_back(x);
}
int n,m,k;
int dp[dim][dim];
signed main(){
fin>>n>>m>>k;
for(int i=1;i<=m;i++){
int x,y,c;
fin>>x>>y>>c;
adj[x].push_back({y,c});
rev_adj[y].push_back({x,c});
}
dfs(1);
reverse(TopSort.begin(),TopSort.end());
for(int i=1;i<=n;i++){
for(int j=1;j<=k;j++){
dp[i][j]=inf;
}
}
dp[TopSort[0]][1]=0;
for(auto x:TopSort){
priority_queue<tuple<int,int,int>,vector<tuple<int,int,int>>,greater<tuple<int,int,int>>>pq;
for(auto [y,c]:rev_adj[x]){
pq.push({dp[y][1]+c,y,1});
}
int cnt=0;
while(!pq.empty()&&cnt<k){
auto[dist,y,nr]=pq.top();
pq.pop();
dp[x][++cnt]=dist;
if(dp[y][nr+1]!=inf){
pq.push({dp[y][nr+1]+dist-dp[y][nr],y,nr+1});
}
}
}
for(int i=1;i<=k;i++){
fout<<dp[n][i]<<' ';
}
}