Pagini recente » Cod sursa (job #1079200) | Cod sursa (job #1935525) | Cod sursa (job #2951260) | Cod sursa (job #2679064) | Cod sursa (job #2064461)
#include <fstream>
#include <iostream>
#include <vector>
#include <set>
using namespace std;
ifstream fi("dijkstra.in");
ofstream fo("dijkstra.out");
const int nmax=50005,INF=1e9;
int n,m,x,y,c,BEST[nmax];
vector <pair <int,int> > V[nmax];
set <pair <int,int> > S;
void dijkstra()
{
while(!S.empty())
{
auto nod=S.begin();
S.erase(nod);
int x = nod->second;
//int current_cost = nod->first;
for(auto edge:V[x])
{
int y = edge.first;
int cost = edge.second;
int new_cost = BEST[x] + cost;
if(BEST[y]>new_cost)
{
S.erase(make_pair(BEST[y],y));
BEST[y]=new_cost;
S.insert(make_pair(BEST[y],y));
}
}
}
}
int main()
{
fi>>n>>m;
for(int i=1;i<=m;i++)
{
fi>>x>>y>>c;
V[x].push_back(make_pair(y,c));
}
for(int i=2;i<=n;i++)
BEST[i]=INF;
S.insert({0,1});
dijkstra();
for(int i=2;i<=n;i++)
fo<<BEST[i]<<" ";
fi.close();
fo.close();
return 0;
}