Pagini recente » Cod sursa (job #1432436) | Cod sursa (job #2661879) | Cod sursa (job #1131142)
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
#include<cstring>
#define pp pair<int,int>
using namespace std;
class Prioritize
{
public:
int operator() (const pair<int, int>& p1, const pair<int, int>& p2 )
{
return p1.second < p2.second;
}
};
const int NMAX = 50001;
unsigned int N, M, d[NMAX];
vector< pp > G[NMAX];
void read(){
ifstream fin("dijkstra.in");
fin >> N >> M;
for(int i = 0, x, y, c; i < M; i++){
fin >> x >> y >> c;
G[x-1].push_back(make_pair(y-1, c));
}
fin.close();
}
void dijkstra(int node){
int top, ad, cost;
memset(d, 0x3f3f3f3f, sizeof(d));
priority_queue< pp, vector<pp>, Prioritize > Q;
d[node] = 0;
Q.push(make_pair(node,d[0]));
while(!Q.empty())
{
top = Q.top().first;
Q.pop();
int size = G[top].size();
for(int i = 0; i < size; i++)
{
ad = G[top][i].first;
cost = G[top][i].second;
if(d[ad] > d[top] + cost)
{
d[ad] = d[top] + cost;
Q.push(make_pair(ad,d[ad]));
}
}
}
}
void write(){
ofstream fout("dijkstra.out");
for(int i=1; i<N; i++){
fout << d[i] << " ";
}
fout << "\n";
fout.close();
}
int main()
{
read();
dijkstra(0);
return 0;
}