Pagini recente » Cod sursa (job #2557673) | Cod sursa (job #2511679) | Cod sursa (job #2278629) | Cod sursa (job #1957975) | Cod sursa (job #1319058)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std;
ifstream f("pitici.in");
ofstream g("pitici.out");
vector <int> v[1050],c[1050];
int n,m,k,dp[1050][1050],numw[1050],can[1050],use[1050],dist[1050][1050];
struct vec
{ int cst,nod,nr; } el,newel;
struct comp
{
bool operator () (vec a,vec b)
{ return a.cst>b.cst; }
};
void DFS(int x)
{ int i;
if (use[x]) return;
use[x]=1;
for(i=0;i<v[x].size();i++)
{ DFS(v[x][i]);
if (can[v[x][i]]) can[x]=1;
}
}
void DFS2(int x)
{ int i;
priority_queue <vec,vector<vec>,comp> heap;
if (use[x]) return;
use[x]=1;
if (x==n) {numw[x]=1; dp[x][numw[x]]=0; return;}
for(i=0;i<v[x].size();i++)
if (can[v[x][i]])
{ DFS2(v[x][i]);
if (numw[v[x][i]])
{
el.cst=c[x][i]+dp[v[x][i]][1];
el.nod=v[x][i];
el.nr=1;
heap.push(el);
}
}
// cout<<x<<" "<<" nod\n";
for(i=1;i<=k && !heap.empty();i++)
{ el=heap.top(); heap.pop();
numw[x]++; dp[x][numw[x]]=el.cst;
if (el.nr<numw[el.nod])
{newel.nr=el.nr+1;
newel.cst=dp[el.nod][el.nr+1]+dist[x][el.nod];
newel.nod=el.nod;
heap.push(newel);
}
}
//for(i=1;i<=numw[x];i++)
// cout<<dp[x][i]<<" ";
// cout<<"\n";
while(!heap.empty())
heap.pop();
}
int main()
{ int i,x,y,cost;
f>>n>>m>>k;
for(i=1;i<=m;i++)
{ f>>x>>y>>cost;
v[x].push_back(y);
dist[x][y]=cost;
c[x].push_back(cost);
}
can[n]=1;
DFS(1);
memset(use,0,sizeof(use));
DFS2(1);
for(i=1;i<=k;i++)
g<<dp[1][i]<<" ";
return 0;
}