Pagini recente » Cod sursa (job #1247833) | Cod sursa (job #1428536) | Cod sursa (job #1103334) | Cod sursa (job #2106558) | Cod sursa (job #1413235)
#include <bits/stdc++.h>
using namespace std;
#ifdef INFOARENA
ifstream f("dijkstra.in");
#define cout g
#else
ifstream f("date.in");
#endif // INFOARENA
ofstream g("dijkstra.out");
#define nmax 50001
#define inf 0x3f3f3f3f
vector<pair<int,int>> G[nmax];
int dist[nmax];
int n,m,x,z,y,i,nod,cost;
struct eee{int nod,cost;
bool operator <(eee a) const{return cost>a.cost;};
};
priority_queue<eee> heap;
int main()
{
f>>n>>m;
memset(dist,inf,sizeof dist);
for(;m;--m)
{
f>>x>>y>>z;
G[x].push_back({y,z});
}
dist[1]=0;
heap.push({1,0});
while(!heap.empty())
{
nod=heap.top().nod;
cost=heap.top().cost;
heap.pop();
for(auto vec:G[nod])
if(cost+vec.second<dist[vec.first])
{
dist[vec.first]=cost+vec.second;
heap.push({vec.first,dist[vec.first]});
}
}
for(i=2;i<=n;++i) cout<<(dist[i]!=inf ? dist[i]:0)<<' ';
return 0;
}