Pagini recente » vlad | Cod sursa (job #2255667) | Cod sursa (job #2764640) | Cod sursa (job #636735) | Cod sursa (job #2101079)
#include <bits/stdc++.h>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int nx=50002;
struct arc
{
int d;
int c;
};
vector < arc > v[nx];
int dist[nx];
int n,m,i,j,c;
void dijkstra()
{
dist[1]=0;
for(int i=2; i<=n; i++)
dist[i]=INT_MAX;
queue < int > q;
q.push(1);
while(!q.empty())
{
int i=q.front();
q.pop();
for(vector < arc > :: iterator it=v[i].begin(); it!=v[i].end(); it++)
if(dist[it->d]>dist[i]+it->c)
{
dist[it->d]=dist[i]+it->c;
q.push(it->d);
}
}
for(int i=2; i<=n; i++)
out<<(dist[i]==INT_MAX? 0:dist[i])<<' ';
}
int main()
{
in>>n>>m;
for(;m;m--)
{
in>>i>>j>>c;
v[i].push_back({j,c});
}
dijkstra();
return 0;
}