Pagini recente » Cod sursa (job #557901) | Cod sursa (job #850862) | Cod sursa (job #2547229) | Cod sursa (job #3277101) | Cod sursa (job #2081365)
#include <bits/stdc++.h>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
#define nmax 100003
const int INF = 0x3f3f3f3f;
int n,m,p,from,to,cost;
int dist[nmax];
bool viz[nmax];
vector < pair < int, int > > matrice[nmax];
queue < int > coada;
void Dijkstra(int nod)
{
coada.push(nod);
dist[nod]=0;
viz[nod]=1;
while(!coada.empty())
{
nod=coada.front();
coada.pop();
for(vector < pair < int, int > >::iterator it=matrice[nod].begin(); it!=matrice[nod].end(); it++)
if(dist[it->first]>dist[nod]+it->second)
{
dist[it->first]=dist[nod]+it->second;
coada.push(it->first);
viz[it->first]=1;
}
}
}
int main()
{
f>>n>>m>>p;
while(f>>from>>to>>cost)
{
matrice[from].push_back({to,cost});
matrice[to].push_back({from,cost});
}
memset(dist,INF,sizeof dist);
Dijkstra(p);
for(int i=1; i<=n; i++)
if(viz[i]) g<<dist[i]<<" ";
else g<<-1<<" ";
return 0;
}