Pagini recente » Cod sursa (job #875056) | Cod sursa (job #3282037) | Cod sursa (job #2723912) | Cod sursa (job #1691468) | Cod sursa (job #2353957)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int maxn = 5e4+2;
const int inf= 2e9;
int n, m, cost[maxn];
bool viz[maxn];
struct str{
int x;
bool operator <(const str &other)const
{
return cost[x]>cost[other.x];
}
};
vector<pair<int, int> >G[maxn];
priority_queue<str>H;
void dijkstra()
{
cost[1]=0;
for(int i=2; i<=n; i++)
{
cost[i]=inf;
}
H.push({1});
viz[1]=true;
while(!H.empty())
{
str t=H.top();
H.pop();
viz[t.x]=false;
for(auto it:G[t.x])
{
if(cost[it.first]>it.second+cost[t.x])
{
cost[it.first]=it.second+cost[t.x];
if(!viz[it.first])
{
viz[it.first]=true;
H.push({it.first});
}
}
}
}
}
int main()
{
int x,y,z;
fin>>n>>m;
for(int i=1; i<=m; i++)
{
fin>>x>>y>>z;
G[x].push_back(make_pair(y,z));
}
dijkstra();
for(int i=2; i<=n; i++)
{
if(cost[i]==inf)
{
fout<<"0 ";
}
else
{
fout<<cost[i]<<' ';
}
}
return 0;
}