Pagini recente » Cod sursa (job #2404012) | Cod sursa (job #16382) | Cod sursa (job #2858775) | Cod sursa (job #2333702) | Cod sursa (job #2999212)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("pitici.in");
ofstream fout("pitici.out");
const int nmax = 1025;
int n, m, k;
int cnt[nmax + 5];
int best[nmax + 5][nmax + 5];
int cost[nmax + 5][nmax + 5];
struct elem
{
int nod, idx, cost;
bool operator < (const elem &other) const
{
return cost > other.cost;
}
};
priority_queue<elem> coada;
vector<int> g[nmax + 5], invG[nmax + 5];
bool visited[nmax + 5];
vector<int> sortTop;
void dfs(int fiu)
{
visited[fiu] = true;
for(auto it : g[fiu])
{
if(visited[it] == true)
{
continue;
}
dfs(it);
}
sortTop.push_back(fiu);
}
int main()
{
fin >> n >> m >> k;
while(m--)
{
int x, y, z;
fin >> x >> y >> z;
g[x].push_back(y);
invG[y].push_back(x);
cost[x][y] = z;
}
dfs(1);
reverse(sortTop.begin(), sortTop.end());
best[1][1] = 0;
cnt[1] = 1;
for(int i = 1; i < n; i ++)
{
int nod = sortTop[i];
for(auto it : invG[nod])
{
coada.push({it, 1, best[it][1] + cost[it][nod]});
}
while(!coada.empty() && cnt[nod] <= k)
{
int prevNod = coada.top().nod;
int prevIdx = coada.top().idx;
int distMin = coada.top().cost;
coada.pop();
best[nod][++cnt[nod]] = distMin;
if(prevIdx + 1 <= cnt[prevNod])
{
coada.push({prevNod, prevIdx + 1, best[prevNod][prevIdx + 1] + cost[prevNod][nod]});
}
}
while(!coada.empty())
{
coada.pop();
}
}
for(int i = 1; i <= k; i ++)
{
fout << best[n][i] << ' ';
}
fout << '\n';
return 0;
}