Pagini recente » Cod sursa (job #1455372) | Cod sursa (job #2896490) | Cod sursa (job #2466720) | Cod sursa (job #3265098) | Cod sursa (job #36036)
Cod sursa(job #36036)
# include <stdio.h>
# include <string.h>
# define _fin "pitici.in"
# define _fout "pitici.out"
# define maxn 1024
# define maxm 200024
int *v[maxn], *c[maxn],
bx[maxm], by[maxm], bc[maxm], cnt[maxn],
b[maxn][maxn],
n, m, k, i, j, node;
void readf()
{
freopen(_fin, "r", stdin);
for (scanf("%d%d%d", &n, &m, &k), i=1; i<=m; i++)
scanf("%d%d%d", bx+i, by+i, bc+i), ++cnt[bx[i]];
for (i=1; i<=n; i++)
v[i] = new int[ cnt[i]+1 ], v[i][0]=0,
c[i] = new int[ cnt[i]+1 ];
for (i=1; i<=m; i++)
v[bx[i]][++v[bx[i]][0]] = by[i],
c[bx[i]][ v[bx[i]][0] ] = bc[i];
}
void update(int nd, int d)
{
if ( !b[nd][0] ) b[nd][0]=1, b[nd][1]=d;
else {
int step, i, j;
for (step=1; step<=b[nd][0]; step<<=1); step>>=1;
for (i=1; step; step>>=1)
if ( i+step <= b[nd][0] )
if ( b[nd][i+step] <= d ) i += step;
if ( i < k )
if ( d < b[nd][i] ) {
for (j=b[nd][0]; j>=i; j--) b[nd][j+1] = b[nd][j];
b[nd][i]=d;
}
else {
for (j=b[nd][0]; j>i; j--) b[nd][j+1] = b[nd][j];
b[nd][i+1]=d;
}
if ( b[nd][0] < k ) ++b[nd][0];
}
}
void solve()
{
b[1][0]=1, b[1][1]=0;
for (node=1; node<n; node++)
for (i=1; i<=v[node][0]; i++)
for (j=1; j<=b[node][0]; j++)
update(v[node][i], b[node][j] + c[node][i]);
}
void writef()
{
freopen(_fout,"w", stdout);
for (i=1; i<k; i++) printf("%d ", b[n][i]); printf("%d\n", b[n][i]);
}
int main()
{
readf();
solve();
writef();
return 0;
}