Mai intai trebuie sa te autentifici.
Cod sursa(job #2034109)
| Utilizator | Data | 7 octombrie 2017 14:16:56 | |
|---|---|---|---|
| Problema | Algoritmul lui Dijkstra | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 1.13 kb |
#include <bits/stdc++.h>
#define INF 2147483647
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int i,j,a,b,c,d[50001],viz[50001],mi,ind,n,m;
vector<pair<int,int>> v[50001];
priority_queue<pair<int,int>> s;
int main()
{
fin>>n>>m;
for (i=2;i<=n;i++)
d[i]=INF;
for (i=1;i<=m;i++)
{
fin>>a>>b>>c;
v[a].push_back(make_pair(b,c));
}
s.push(make_pair(0,1));
while (!s.empty())
{
pair<int,int> p = s.top();
s.pop();
ind=0;
if (viz[p.second]==0)
ind=p.second;
if (ind!=0)
{
viz[ind]=1;
for (auto t:v[ind])
{
if (d[t.first]>d[ind]+t.second)
{
d[t.first]=d[ind]+t.second;
s.push(make_pair(-d[t.first], t.first));
}
}
}
}
for (i=2;i<=n;i++)
{
if (d[i]==INF)
fout<<0<<" ";
else fout<<d[i]<<" ";
}
return 0;
}
