Pagini recente » Cod sursa (job #34275) | Cod sursa (job #1406081) | Cod sursa (job #2761367) | Cod sursa (job #52365) | Cod sursa (job #3251512)
#include <bits/stdc++.h>
using namespace std;
string file="dijkstra";
ifstream fin(file+".in");
ofstream fout(file+".out");
map<int, map<int, int> > g;
int b[50001];
void dijkstra(int i)
{
b[i]=1;
queue<int> q;
q.push(i);
while (!q.empty()){
i=q.front();
q.pop();
for (auto it:g[i]){
if (!b[it.first] || b[it.first]>b[i]+it.second){
b[it.first]=b[i]+it.second;
q.push(it.first);
}
}
}
}
int main()
{
int n, m;
fin>>n>>m;
while (m){
m--;
int x, y, z;
fin>>x>>y>>z;
g[x][y]=z;
}
dijkstra(1);
for (int i=2; i<=n; i++)fout<<b[i]-1<<" ";
return 0;
}