Pagini recente » Cod sursa (job #2718612) | Cod sursa (job #1669838) | Cod sursa (job #2539729) | Cod sursa (job #2980912) | Cod sursa (job #2828242)
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;
struct pereche{
int y,cost;
};
const int N = 5e4 + 5;
vector<pereche>v[N];
bitset<N>inq;
queue<pereche>q;
int dp[N];
void init(int n){
for(int i=1;i<n;i++){
dp[i] = 2147483647;
}
}
void bfs(){
inq[1] = true;
pereche x;
x.y=1;
x.cost=0;
dp[1] = 0;
q.push(x);
while(!q.empty()){
x = q.front();
q.pop();
inq[x.y] = false;
for(auto y:v[x.y]){
if(dp[y.y]>dp[x.y] + y.cost){
dp[y.y]=dp[x.y] + y.cost;
if(!inq[y.y]){
inq[y.y] = true;
q.push(y);
}
}
}
}
}
int main() {
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
int n,m,x,y,cost;
in>>n>>m;
while(m--){
in>>x>>y>>cost;
pereche w;
w.y=y;
w.cost=cost;
v[x].push_back(w);
w.y=x;
v[y].push_back(w);
}
init(n+1);
bfs();
int nr = 2147483647;
for(int i=2;i<=n;i++){
if(dp[i] == nr){
out<<0<<" ";
continue;
}
out<<dp[i]<<" ";
}
return 0;
}