Cod sursa(job #176434)

Utilizator plastikDan George Filimon plastik Data 11 aprilie 2008 11:37:41
Problema Pitici Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.52 kb
// Pitici, Infoarena/Lot 2006
// http://infoarena.ro/problema/pitici

#define _CRT_SECURE_NO_DEPRECATE
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;

const int NMAX = 1024;

int n, m, k;
multiset<int> Costs[NMAX];

short int Neighb[NMAX][NMAX];
int Dist[NMAX][NMAX];
bool Used[NMAX];
short int Done[NMAX];

void depthFirst(int n) {
	int next, i;
	
	for (i = 1; i <= Neighb[n][0]; ++ i) {
		next = Neighb[n][i];
		if (Used[next] == false) {
			Used[next] = true;
			depthFirst(next);
		}
	}
	Done[++ Done[0]] = n;
}

void costUpdate(short int n) {
	multiset<int>::iterator msi, aux;
	int cost;
	short int next, i;

	for (i = 1; i <= Neighb[n][0]; ++ i) {
		next = Neighb[n][i];
		cost = Dist[n][next];

		for (msi = Costs[n].begin(); msi != Costs[n].end(); ++ msi) {
			Costs[next].insert(*msi + cost);
			if (Costs[next].size() > k) {
				aux = Costs[next].end();
				-- aux;
				Costs[next].erase(aux);
			}
		}
	}
}

int main() {

	freopen("pitici.in", "r", stdin);
	freopen("pitici.out", "w", stdout);

	scanf("%d %d %d", &n, &m, &k);
	int i, v1, v2, c;
	for (i = 0; i < m; ++ i) {
		scanf("%d %d %d", &v1, &v2, &c);
		Neighb[v1][++ Neighb[v1][0]] = v2;
		Dist[v1][v2] = c;
	}

	memset(Used, 0, sizeof(Used));
	Used[1] = true;
	depthFirst(1);

	Costs[1].insert(0);
	for (i = Done[0]; i > 0; -- i)
		costUpdate(Done[i]);

	multiset<int>::iterator msi;
	for (msi = Costs[n].begin(); msi != Costs[n].end(); ++ msi)
		printf("%d ", *msi);

	return 0;
}