Pagini recente » Cod sursa (job #1465336) | Cod sursa (job #2759168) | Cod sursa (job #1271677) | Cod sursa (job #1487618) | Cod sursa (job #3304761)
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 50002
#define INF (1<<30)
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int N,M;
queue<int>q;
vector<int> viz(NMAX,0),cost(NMAX,INF);
vector<pair<int,int>>graph[NMAX];
void citire()
{
fin>>N>>M;
int A,B,C;
for(int i=1; i<=M; i++)
{
fin>>A>>B>>C;
graph[A].push_back({B,C});
}
}
int main()
{
citire();
cost[1]=0;
q.push(1);
viz[1]=1;
while(!q.empty())
{
int nod=q.front();
q.pop();
viz[nod]=0;
for(int j=0; j<graph[nod].size(); j++)
{
int next_nod=graph[nod][j].first;
if(cost[next_nod]>cost[nod]+graph[nod][j].second)
{
cost[next_nod]=cost[nod]+graph[nod][j].second;
if(!viz[next_nod])
{
viz[next_nod]=1;
q.push(next_nod);
}
}
}
}
for(int i=2; i<=N; i++)
{
if(cost[i]==INF)
{
cost[i]=0;
}
fout<< cost[i] << " ";
}
fout<< "\n";
return 0;
}