Pagini recente » Cod sursa (job #92602) | Cod sursa (job #1921717) | Cod sursa (job #1810343) | Cod sursa (job #1339122) | Cod sursa (job #2846603)
#include <vector>
#include <iostream>
#include <fstream>
#include <set>
#include <queue>
#define cout std::cout
typedef std::pair<int, int> pair;
std::ifstream in("dijkstra.in");
std::ofstream out("dijkstra.out");
const int N=50000;
const int M=250000;
const int INF=2000000000;
struct muchie
{
int from;
int to;
int cost;
};
int dist[N+1];
std::vector<int> v[N+1];
muchie edge[M+1];
struct boring
{
int first;
int second;
};
bool operator<(const boring a, const boring b)
{
return a.first>b.first;
}
std::priority_queue<boring, std::vector<boring>> q;
void Dijkstra()
{
dist[1]=0;
q.push({0, 1});
int cnt =0;
while(!q.empty())
{
int dist_curr=q.top().first;
int curr=q.top().second;
q.pop();
if(dist_curr==dist[curr])
{
for(int l : v[curr])
{
int to = edge[l].to;
if(dist_curr+edge[l].cost<dist[to])
{
cnt++;
dist[to]=dist_curr+edge[l].cost;
q.push({dist[to], to});
}
}
}
}
cout << cnt;
}
int n, m;
int main()
{
in>>n>>m;
for(int i=1; i<=m; i++)
{
in>>edge[i].from>>edge[i].to>>edge[i].cost;
v[edge[i].from].push_back(i);
}
for(int i=1; i<=n; i++)
{
dist[i]=INF;
}
Dijkstra();
for(int i=2; i<=n; i++)
{
if(dist[i]-INF) out<<dist[i]<<" ";
else out<<0<<" ";
}
}