Pagini recente » Cod sursa (job #1761612) | Clasament simulare04032019 | Cod sursa (job #1805869) | Rating Theodor-Florentin Furtuna (Theo_Furtuna) | Cod sursa (job #1760485)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
#define nmax 50100
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
typedef struct muchie
{
int a,b,cost;
};
class compare
{
public:
bool operator () (muchie a, muchie b)
{
return a.cost>b.cost;
}
};
priority_queue <muchie, vector <muchie>, compare > heap;
vector <muchie> v[nmax];
int cost[nmax];
int n,m,i;
muchie x;
int main()
{
fin>>n>>m;
for(i=1; i<=m; ++i)
{
fin>>x.a>>x.b>>x.cost;
v[x.a].push_back(x);
v[x.b].push_back(x);
}
cost[1]=0;
for(i=2; i<=n; ++i)
cost[i]=1000000000;
for(i=0; i<v[1].size(); ++i)
heap.push(v[1][i]);
while(heap.size())
{
muchie a = heap.top();
heap.pop();
if(cost[a.a]+a.cost<cost[a.b])
{
cost[a.b]=cost[a.a]+a.cost;
for(i=0; i<v[a.b].size(); ++i)
heap.push(v[a.b][i]);
}
}
for(i=2; i<=n; ++i)
if(cost[i]!=1000000000)
fout<<cost[i]<<' ';
else
fout<<"0 ";
}