Pagini recente » Clasament emag_2016-incepatori-3 | Cod sursa (job #1466909) | Cod sursa (job #2893419) | Cod sursa (job #820856) | Cod sursa (job #584803)
Cod sursa(job #584803)
#include<fstream>
#include<queue>
#include<vector>
using namespace std;
#define oo 2147483647
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
struct nod
{ int to,cost;
nod(int y,int c) { to = y , cost = c; }
};
typedef vector<nod>:: iterator it;
int N,M;
vector<nod>a[50001];
int d[50001];
bool in[50001];
struct cmp
{ public:
bool operator ()(int i,int j)
{ if(d[i] > d[j]) return 1;
return 0;
}
};
priority_queue<int,vector<int>,cmp>q;
int main()
{ int i,j,x,y,c;
f>>N>>M;
for(i = 1; i <= M; i++)
{ f>>x>>y>>c;
a[x].push_back(nod(y,c));
}
for(i = 2; i <= N; i++)
d[i] = oo;
d[1] = 0; q.push(1); in[1] = 1;
while(!q.empty())
{ x = q.top(); q.pop(); in[x] = 0;
for(it k = a[x].begin(); k != a[x].end(); k++)
if(d[k->to] > d[x] + k->cost)
{ d[k->to] = d[x] + k->cost;
if(!in[k->to]) q.push(k->to) , in[k->to] = 1;
}
}
for(i = 2; i <= N; i++)
if(d[i] != oo) g<<d[i]<<" ";
else g<<0<<" ";
f.close();
g.close();
return 0;
}