Pagini recente » Cod sursa (job #1357389) | Cod sursa (job #2725214) | Cod sursa (job #2165634) | Cod sursa (job #1032239) | Cod sursa (job #3143055)
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
const int dim=5e4+5,inf=1e9;
int n,m,d[dim];
vector<pair<int,int> > G[dim];
set<pair<int,int> > S;
void dijkstra(int p)
{
d[p]=0;
S.insert({0,p});
while(!S.empty())
{
int x=S.begin()->second;
S.erase(S.begin());
for(int i=0; i<G[x].size(); i++)
{
pair<int,int> u=G[x][i];
if(d[u.first]>d[x]+u.second)
{
S.erase({d[u.first],u.first});
d[u.first]=d[x]+u.second;
S.insert({d[u.first],u.first});
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
f>>n>>m;
for(int i=2; i<=n; i++)
{
d[i]=inf;
}
for(int i=1; i<=m; i++)
{
int x,y,c;
f>>x>>y>>c;
G[x].push_back({y,c});
}
dijkstra(1);
for(int i=2; i<=n; i++)
{
if(d[i]==inf)
{
g<<0<<' ';
}
else
{
g<<d[i]<<' ';
}
}
return 0;
}