Pagini recente » Cod sursa (job #1129418) | Cod sursa (job #2768447) | Monitorul de evaluare | Cod sursa (job #671764) | Cod sursa (job #556951)
Cod sursa(job #556951)
#include<fstream>
#include<algorithm>
#include<queue>
#include<vector>
#define maxn 50010
using namespace std;
const int nmax=5000;
vector< pair<int,int> > e[maxn];
bool test[maxn];
queue<int> Q;
int N,M;
long d[maxn],dp[maxn];
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
void bellman(int S){
for(int i=1;i<=N;i++)
d[i]=nmax;
memset(test,false,nmax);
d[1]=0;
test[1]=true;
Q.push(1);
while(!Q.empty()){
int nod=Q.front();
Q.pop();
test[nod]=false;
for(vector< pair<int,int> >::iterator it=e[nod].begin();it!=e[nod].end();++it){
if(d[it->first]>d[nod]+it->second){
d[it->first]=d[nod]+it->second;
if(!test[it->first])
Q.push(it->first),test[it->first]=true;
}
if(d[nod]>d[it->first]+it->second){
d[nod]=d[it->first]+it->second;
if(!test[nod])
Q.push(nod),test[nod]=true;
}
}
}
}
void citire(){
int x,y,c;
f>>N>>M;
for(int i=1;i<=M;i++){
f>>x>>y>>c;
e[x].push_back(make_pair(y,c)),e[y].push_back(make_pair(x,c));
}
}
int main(){
citire();
bellman(1);
for(int i=2;i<=N;i++)
g<<d[i]<<' ';
g<<'\n';
g.close();
return 0;
}