Cod sursa(job #3328735)

Utilizator Ilie_MityIlie Dumitru Ilie_Mity Data 9 decembrie 2025 22:54:56
Problema Pitici Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.39 kb
// Ilie "The-Winner" Dumitru
// Dumnezeu sa o ierte
#include<bits/stdc++.h>
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define err(...) fprintf(stderr, __VA_ARGS__)
using ll=long long;
using dbl=long double;
constexpr int NMAX=1'024;
constexpr ll MOD=9'901;

struct edge
{
	int u, w;
};

struct path
{
	int u, w, ew, i;

	friend bool operator<(path p, path q)
	{
		return p.w>q.w;
	}
};

int N, K;
std::vector<edge> GT[NMAX];
int topo[NMAX], deg[NMAX];
std::vector<int> dp[NMAX];

int main()
{
	FILE* f=fopen("pitici.in", "r"), *g=fopen("pitici.out", "w");
	int i, a, b, c, M;

	fscanf(f, "%d%d%d", &N, &M, &K);
	for(i=0;i<M;++i)
	{
		fscanf(f, "%d%d%d", &a, &b, &c);
		--a;
		--b;
		GT[b].push_back({a, c});
		++deg[a];
	}

	a=N;
	for(i=0;i<N;++i)
		if(!deg[i])
			topo[--a]=i;
	for(i=N-1;i>-1;--i)
		for(edge e : GT[topo[i]])
			if(!--deg[e.u])
				topo[--a]=e.u;

	dp[0].push_back(0);
	for(i=1;i<N;++i)
	{
		std::priority_queue<path> pq;
		int node=topo[i];

		for(edge e : GT[node])
			if(!dp[e.u].empty())
				pq.push({e.u, e.w+dp[e.u][0], e.w, 1});

		while(!pq.empty() && sz(dp[node])<K)
		{
			path p=pq.top();
			pq.pop();

			dp[node].push_back(p.w);
			if(p.i<sz(dp[p.u]))
				pq.push({p.u, p.ew+dp[p.u][p.i], p.ew, p.i+1});
		}

		GT[node].clear();
		GT[node].shrink_to_fit();
	}

	for(i=0;i<K;++i)
		fprintf(g, "%d ", dp[N-1][i]);
	fprintf(g, "\n");

	fclose(f);
	fclose(g);
	return 0;
}