Pagini recente » Cod sursa (job #273753) | Cod sursa (job #3221925) | Cod sursa (job #2511529) | Cod sursa (job #106449) | Cod sursa (job #2375914)
#include <bits/stdc++.h>
using namespace std;
#define nmax 50005
#define inf 1000000005
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n,m,dist[nmax];
struct str
{
int x,cost;
};
struct str2
{
int x,cost;
bool operator <(const str2 &a)const
{
return cost>a.cost;
}
};
vector<str>Q[nmax];
priority_queue<str2>V;
void read()
{
f>>n>>m;
for (int i=1;i<=m;++i)
{
int l1,l2,l3;
f>>l1>>l2>>l3;
Q[l1].push_back({l2,l3});
}
}
void solve()
{
for (int i=1;i<=n;++i)
dist[i]=inf;
dist[1]=0;
V.push({1,0});
while (!V.empty())
{
int nod=V.top().x;
int cost=V.top().cost;
V.pop();
if (cost!=dist[nod])
continue;
for (auto w:Q[nod])
{
int nnod=w.x;
int ncost=w.cost;
if (cost+ncost<dist[nnod])
{
dist[nnod]=cost+ncost;
V.push({nnod,dist[nnod]});
}
}
}
for (int i=2;i<=n;++i)
if (dist[i]==inf)
g<<0<<' ';
else g<<dist[i]<<' ';
}
int main()
{
read();
solve();
return 0;
}