Pagini recente » Monitorul de evaluare | Cod sursa (job #448868) | Cod sursa (job #495750) | Cod sursa (job #2742267) | Cod sursa (job #3328774)
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
ifstream cin("pitici.in");
ofstream cout("pitici.out");
const int Nmax = 1020;
vector<pair<int, int>> g[Nmax], gt[Nmax];
int vis[Nmax];
vector<int>dp[Nmax], top;
void dfs(int nod)
{
vis[nod] = 1;
for (auto it : g[nod])
if(vis[it.first] == 0)
dfs(it.first);
top.push_back(nod);
}
struct muchie
{
int nod, idx, dist, cost;
bool operator<(const muchie& a) const
{
return dist > a.dist;
}
};
int main()
{
int n, m, k, x, y, c;
cin >> n >> m >> k;
for (int i = 1; i <= m; i++)
{
cin >> x >> y >> c;
g[x].push_back({ y, c });
gt[y].push_back({ x, c });
}
dfs(1);
dp[1].push_back(0);
reverse(top.begin(), top.end());
for (int i = 0; i < n; i++)
{
x = top[i];
priority_queue<muchie> pq;
for (auto it : gt[x])
{
int y = it.first;
if (dp[y].size())
pq.push({ y, 0, dp[y][0] + it.second, it.second});
}
//cout << x << ": ";
while (!pq.empty() && dp[x].size() < k)
{
muchie now = pq.top();
pq.pop();
y = now.nod;
//cout << y << " ";
dp[x].push_back(now.dist);
if (now.idx + 1 < dp[y].size())
pq.push({ y, now.idx + 1, dp[y][now.idx + 1] + now.cost, now.cost });
}
//cout << '\n';
}
for (int i = 0; i < min(k, (int)dp[n].size()); i++)
cout << dp[n][i] << " ";
}