Pagini recente » Cod sursa (job #1369097) | Cod sursa (job #1520356) | Cod sursa (job #1986940) | Cod sursa (job #2389002) | Cod sursa (job #2846569)
#include <vector>
#include <fstream>
#include <set>
#include <queue>
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;
};
bool marked[N+1];
int dist[N+1];
std::vector<int> v[N+1];
muchie edge[M+1];
bool compare(int a, int b)
{
return v[a]<v[b];
}
std::priority_queue<pair, std::vector<pair>> q;
void Dijkstra()
{
dist[1]=0;
q.push({0, 1});
while(!q.empty())
{
int curr=q.top().second;
int dist_curr=q.top().first;
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])
{
dist[to]=dist_curr+edge[l].cost;
q.push({dist[to], to});
}
}
}
}
}
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<<" ";
}
}