Cod sursa(job #3328774)

Utilizator adelinapetreAdelina Petre adelinapetre Data 10 decembrie 2025 12:17:06
Problema Pitici Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#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] << " ";
}