Pagini recente » Cod sursa (job #2980093) | Cod sursa (job #970674) | Cod sursa (job #392662) | Cod sursa (job #1440606) | Cod sursa (job #1167379)
#include <fstream>
#include <vector>
#include <queue>
#define MAXN 50005
#define INF 2000000000
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
struct Comp{
bool operator()(pair<int,int> a,pair<int,int> b){
return a.second>b.second;}};
int n,m,x,y,cost,dist[MAXN];
bool uz[MAXN];
vector< pair<int,int> > G[MAXN];
priority_queue<pair<int,int>,vector< pair<int,int> >,Comp> pq;
pair<int,int> aux;
int main()
{
int i;
f>>n>>m;;
for(i=1;i<=m;i++){
f>>x>>y>>cost;
G[x].push_back(make_pair(y,cost));
G[y].push_back(make_pair(x,cost));}
pq.push(make_pair(1,0));
while(!pq.empty()){
while(!pq.empty()&&uz[pq.top().first])
pq.pop();
if(pq.empty())
break;
aux=pq.top();
pq.pop();
uz[aux.first]=1;
dist[aux.first]=aux.second;
for(i=0;i<G[aux.first].size();i++)
pq.push(make_pair(G[aux.first][i].first,G[aux.first][i].second+aux.second));}
for(i=2;i<=n;i++)
g<<dist[i]<<' ';
f.close();
g.close();
return 0;
}