Pagini recente » Cod sursa (job #72914) | Cod sursa (job #2073923) | Cod sursa (job #1196650) | Cod sursa (job #3326343) | Cod sursa (job #3304760)
#include <fstream>
#include <vector>
#define NMAX 50002
#define INF (1<<30)
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int N,M;
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;
for(int i=1; i<=N; i++)
{
int nod,vmin;
nod=0;
vmin=INF;
for(int j=1; j<=N; j++)
{
if(!viz[j] && cost[j]<vmin)
{
vmin=cost[j];
nod=j;
}
}
if(nod)
{
viz[nod]=1;
for(int j=0; j<graph[nod].size(); j++)
{
int next_nod=graph[nod][j].first;
if(!viz[next_nod] && cost[next_nod]>cost[nod]+graph[nod][j].second)
{
cost[next_nod]=cost[nod]+graph[nod][j].second;
}
}
}
else
{
break;
}
}
for(int i=2; i<=N; i++)
{
if(cost[i]==INF)
{
cost[i]=0;
}
fout<< cost[i] << " ";
}
fout<< "\n";
return 0;
}