Pagini recente » Cod sursa (job #1112655) | Cod sursa (job #2534244) | Cod sursa (job #1109899) | Cod sursa (job #2914632) | Cod sursa (job #2269752)
#include <bits/stdc++.h>
#define dm 50005
#define inf 0x3f3f3f3f
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, x, y, a;
struct str{
int node, cost;
bool operator < (const str &other) const {
return cost > other.cost;
}
};
vector <str> v[dm];
priority_queue <str> pq;
int drum[dm];
void dijkstra(int start)
{
pq.push({start, 0});
drum[start] = 0;
while(!pq.empty())
{
str x = pq.top();
pq.pop();
for(auto it:v[x.node])
{
int nbr = it.node;
int cost = it.cost;
if(drum[x.node] + it.cost < drum[nbr])
{
drum[nbr] = drum[x.node] + it.cost;
pq.push({nbr, drum[nbr]});
}
}
}
}
int main()
{
fin >> n >> m;
memset(drum, inf, sizeof(drum));
for(int i = 1; i <= m; i++)
{
fin >> x >> y >> a;
v[x].push_back({y, a});
}
dijkstra(1);
for(int i = 2; i <= n; i++)
if(drum[i]!=inf)
fout << drum[i] << " ";
else fout << 0;
return 0;
}