Pagini recente » Cod sursa (job #545452) | Cod sursa (job #810394) | Cod sursa (job #2924694) | Cod sursa (job #430227) | Cod sursa (job #1139254)
#include<fstream>
#include<queue>
#include<vector>
#define M 2000000000
using namespace std;
ifstream f("date.in");
ofstream g("date.out");
struct nod
{
int inf,c;
};
int n,m,nodcur,cost[50001],i,x1,y1,z1,j;
vector<nod>L[50001];
struct cmp
{
bool operator() (nod a,nod b)
{
if(cost[a.inf]>cost[b.inf])return true;
else return false;
}
};
priority_queue<nod,vector<nod>,cmp>heap;
void citire()
{
f>>n>>m;
for(i=1;i<=m;i++)
{
f>>x1>>y1>>z1;
nod a;
a.inf=y1;
a.c=z1;
L[x1].push_back(a);
}
}
void dijkstra()
{
for(i=1;i<=n;i++)
cost[i]=M;
cost[1]=0;
nod a;
a.inf=1;
a.c=0;
heap.push(a);
for(i=1;i<=n-1&&heap.size();i++)
{
nodcur=heap.top().inf;
heap.pop();
for(j=0;j<L[nodcur].size();j++)
if(cost[L[nodcur][j].inf]>cost[nodcur]+L[nodcur][j].c)
{
cost[L[nodcur][j].inf]=cost[nodcur]+L[nodcur][j].c;
heap.push(L[nodcur][j]);
}
}
for(i=2;i<=n;i++)
if(cost[i]==M)
g<<"0 ";
else
g<<cost[i]<<" ";
}
int main()
{
citire();
dijkstra();
return 0;
}