Pagini recente » Cod sursa (job #3169239) | Cod sursa (job #1294050) | Cod sursa (job #902343) | Cod sursa (job #831295) | Cod sursa (job #2361445)
#include <fstream>
#include <queue>
#include <vector>
#define NM 50005
#define mp make_pair
#define oo 2100000000
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n,m,d[NM];
bool viz[NM];
vector<pair<int,int> > v[NM];
struct ok
{ bool operator()(int x,int y)
{ return d[x]>d[y]; }
};
priority_queue<int,vector<int>,ok> coada;
void Dijksta(int nod)
{ for(int i=1; i<=n; i++) d[i]=oo;
d[nod]=0;
viz[nod]=1;
coada.push(nod);
while(!coada.empty())
{ int x=coada.top();
coada.pop();
viz[x]=false;
for(unsigned int j=0; j<v[x].size(); j++)
{ int nd=v[x][j].first;
int cost=v[x][j].second;
if(d[x]+cost<d[nd])
{ d[nd]=d[x]+cost;
if(!viz[nd])
{ viz[nd]=1;
coada.push(nd);
}
}
}
}
}
int main()
{ f>>n>>m;
while(m--)
{ int x,y,val;
f>>x>>y>>val;
v[x].push_back(mp(y,val));
}
Dijksta(1);
for(int i=2; i<=n; i++)
if(d[i]==oo) g<<0<<' ';
else g<<d[i]<<' ';
}