Pagini recente » Cod sursa (job #3269742) | Cod sursa (job #1004692) | Cod sursa (job #2693841) | Cod sursa (job #372757) | Cod sursa (job #2795325)
#include <fstream>
#include <climits>
#include <vector>
#include <queue>
#define pb push_back
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int INF=INT_MAX;
const int N=5e4+2;
struct vf{
int nod,cost;
bool operator< (const vf &o) const{
return cost>o.cost;
}
};
int v[N],cost;
int n,m,a,b,c,i;
vector<vf> g[N];
priority_queue<vf> q;
void dijkstra(int s)
{
q.push({s,0}), v[1]=0;
while(!q.empty())
{
vf now;
now.nod=q.top().nod;
now.cost=q.top().cost;
q.pop();
if(v[now.nod]!=now.cost)
continue;
for(auto i:g[now.nod])
{
cost=now.cost+i.cost;
if(v[i.nod]>cost)
{
v[i.nod]=cost;
q.push({i.nod,cost});
}
}
}
}
int main()
{
fin>>n>>m;
for(i=1; i<=m; i++)
{
fin>>a>>b>>c;
g[a].pb({b,c});
}
for(i=0; i<N; i++)
v[i]=INF;
dijkstra(1);
for(i=2; i<=n; i++)
if(v[i]>=INF)
fout<<"0 ";
else
fout<<v[i]<<' ';
return 0;
}