Cod sursa(job #640144)

Utilizator antoanelaAntoanela Siminiuc antoanela Data 24 noiembrie 2011 20:36:28
Problema Pitici Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#define nmax 1022
#define f first
#define s second

vector <pair <int, int> > g[nmax], v[nmax];
priority_queue <pair <int, int> > heap;
int n, m, k, grint[nmax], q[nmax], sol[nmax][nmax], p[nmax];

void sortare_topologica()
{
	int i, j, nod, v;
	for (i=1; i<=n; i++)
		if (!grint[i]) q[++q[0]]=i;
	for (i=1; i<=q[0]; i++)
	{
		nod=q[i];
		for (j=0; j<g[nod].size(); j++)
		{
			v=g[nod][j].f;
			grint[v]--;
			if (!grint[v]) q[++q[0]]=v;
		}
	}
}

int main()
{
	freopen("pitici.in","r",stdin);
	freopen("pitici.out","w",stdout);
	scanf("%d %d %d", &n, &m, &k);
	int i, x, y, c, j, nod, ind, vec;
	for (i=1; i<=m; i++)
	{
		scanf("%d %d %d", &x, &y, &c);
		g[x].push_back (make_pair (y, c));
		v[y].push_back (make_pair (x, c));
		grint[y]++;
	}
	sortare_topologica();
	sol[1][0]=1;
	for (i=2; i<=n; i++)
	{
		nod=q[i];
		for (j=1; j<=n; j++) p[j]=1;
		for (j=0; j<v[nod].size(); j++)
			heap.push (make_pair (-(sol[v[nod][j].f][1]+v[nod][j].s), j));
		for (j=1; j<=k && !heap.empty(); j++)
		{
			ind=heap.top().s;
			sol[nod][++sol[nod][0]]=-heap.top().f;
			vec=v[nod][ind].f;
			heap.pop();
			if (p[vec]<sol[vec][0]) 
			{
				heap.push((make_pair (-(sol[vec][p[vec]+1]+v[nod][ind].s), ind)));
				p[vec]++;
			}
			//printf("%d %d %d\n", nod, vec, sol[vec][0]);
		}
		while (!heap.empty()) heap.pop();
	}
	for (i=1; i<=k; i++) printf("%d ",sol[n][i]);
}