Nu aveti permisiuni pentru a descarca fisierul grader_test8.in
Cod sursa(job #2425753)
Utilizator | Data | 25 mai 2019 00:42:01 | |
---|---|---|---|
Problema | Algoritmul lui Dijkstra | Scor | 40 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.25 kb |
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct graf
{
int a,b,cost;
}muchie[250005];
int distanta[50005];
int N,M;
int main()
{
fin>>N>>M;
distanta[1]=0;
for(int i=2;i<=N;i++)
{
distanta[i]=2147483647;
}
priority_queue <pair<int,int>, vector<pair<int,int> >, greater< pair<int,int> > > dist;
for(int i=1;i<=M;i++)
{
fin>>muchie[i].a>>muchie[i].b>>muchie[i].cost;
if(muchie[i].a==1)
{
dist.push({muchie[i].cost,muchie[i].b});
distanta[muchie[i].b]=muchie[i].cost;
}
}
while(!dist.empty())
{
for(int i=1;i<=M;i++)
{
if(muchie[i].a == dist.top().second && distanta[muchie[i].b]>distanta[muchie[i].a]+muchie[i].cost)
{
distanta[muchie[i].b]=distanta[muchie[i].a]+muchie[i].cost;
dist.push({distanta[muchie[i].b],muchie[i].b});
}
}
dist.pop();
}
for(int i=2;i<=N;i++)
{
if(distanta[i]!=2147483647)
fout<<distanta[i]<<" ";
else
fout<<"0 ";
}
return 0;
}