Pagini recente » Cod sursa (job #1338496) | Cod sursa (job #1647347) | Cod sursa (job #74023) | Cod sursa (job #1868783) | Cod sursa (job #2326142)
#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
ifstream f ("dijkstra.in");//dijkstra
ofstream g ("dijkstra.out");
#define inf 0x3f3f3f3f
#define Gmax 500
struct point
{
int nod;
int cost;
};
bool operator<(point a, point b)
{
return a.cost > b.cost;
}
vector <pair <int,int> > G[Gmax];
priority_queue<point>pq;
int dist[Gmax];
int main()
{
int n,m;
f>>n>>m;
for(int i=1; i<=m; i++)
{
int x,y,z;
f>>x>>y>>z;
G[x].push_back({y,z});
}
memset(dist, 0x3f, sizeof(dist));
pq.push({1,0});
while(!pq.empty())
{
int nod = pq.top().nod;
int cost = pq.top().cost;
pq.pop();
if(dist[nod]!=inf)
continue;
dist[nod] = cost;
for(int i=0;i<G[nod].size();i++)
{
pq.push({G[nod][i].first, cost + G[nod][i].second});
}
}
for(int i=2; i<=n;i++)
{
if(dist[i] == inf) g<<0<<' ';
else g<<dist[i]<<' ';
}
return 0;
}