Pagini recente » Cod sursa (job #442522) | Cod sursa (job #3144399) | Cod sursa (job #1351604) | Cod sursa (job #3279392) | Cod sursa (job #642510)
Cod sursa(job #642510)
#include<stdio.h>
#include<queue>
#define maxn 1022
#define mp make_pair
#define pss pair<short int,short int>
#define pii pair<int,int>
#define pb push_back
using namespace std;
FILE*f=fopen("pitici.in","r");
FILE*g=fopen("pitici.out","w");
int n,m,k,i,x,y,c,nod,sol[maxn],used[maxn];
vector< pss >G[maxn],W[maxn];
int calc[maxn][maxn];
priority_queue< pii >H;
queue<int>Q; int Gr[maxn];
int main () {
fscanf(f,"%d %d %d",&n,&m,&k);
for ( i = 1 ; i <= m ; ++i ){
fscanf(f,"%d %d %d",&x,&y,&c);
G[x].pb(mp(y,c));
W[y].pb(mp(x,c));
++Gr[y];
}
calc[1][0] = 1; calc[1][1] = 0;
Q.push(1);
while ( !Q.empty() ){
nod = Q.front(); Q.pop();
for ( vector< pss >::iterator itt = G[nod].begin() ; itt != G[nod].end() ; ++itt ){
--Gr[itt->first];
if ( !Gr[itt->first] ){
Q.push(itt->first);
}
}
if ( nod == 1 ) continue ;
while ( !H.empty() ) H.pop();
i = 0;
for ( vector< pss >::iterator itt = W[nod].begin() ; itt != W[nod].end() ; ++itt ){
used[i] = 1;
H.push(mp(-calc[itt->first][1]-itt->second,i)); ++i;
}
while ( !H.empty() && calc[nod][0] < k ){
int val = H.top().first; int ind = H.top().second; short int nodvcn = W[nod][ind].first; short int cost = W[nod][ind].second;
calc[nod][++calc[nod][0]] = -val;
if ( used[ind] < calc[nodvcn][0] ){
++used[ind];
H.push(mp(-calc[nodvcn][used[ind]]-cost,ind));
}
H.pop();
}
}
for ( i = 1 ; i <= k ; ++i ){
fprintf(g,"%d ",calc[n][i]);
}
fprintf(g,"\n");
fclose(f);
fclose(g);
return 0;
}