Pagini recente » Cod sursa (job #301006) | Cod sursa (job #483244) | Cod sursa (job #1077178) | Cod sursa (job #1682143) | Cod sursa (job #488613)
Cod sursa(job #488613)
#include<stdio.h>
#include<vector>
#include<queue>
using namespace std;
#define inf 1000000007
struct pereche {
int x, y;
pereche(int xx, int yy) { x=xx; y=yy; }
};
priority_queue<pereche> heap;
int d[1025][1025],n,m,k,poz[1025];
vector <pereche> v[1025];
bool operator< (const pereche& a, const pereche& b)
{
return a.x>b.x;
}
void dfs(int nod)
{
int i,lim=v[nod].size();
for(i=0;i<lim;i++)
dfs(v[nod][i].x);
for(i=0;i<k;i++)
d[nod][i]=inf;
if(nod==n)
{
d[nod][0]=0;
return ;
}
while(!heap.empty())
heap.pop();
for(i=0;i<lim;i++)
{
heap.push(pereche(v[nod][i].y+d[v[nod][i].x][0],i));
poz[i]=0;
}
int ind,vec,cst;
for(i=0;i<k && !heap.empty();i++)
{
d[nod][i]=heap.top().x;
if (d[nod][i]==inf)
break;
ind=heap.top().y;
vec=v[nod][ind].x;
cst=v[nod][ind].y;
heap.pop();
heap.push(pereche(cst+d[vec][++poz[ind]],ind));
}
}
int main ()
{
int i,a,b,c;
freopen("pitici.in","r",stdin);
freopen("pitici.out","w",stdout);
scanf("%d%d%d",&n,&m,&k);
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
v[a].push_back(pereche(b,c));
}
dfs(1);
for(i=0;i<k-1;i++)
printf("%d ",d[1][i]);
printf("%d\n",d[1][k-1]);
return 0;
}