Pagini recente » Cod sursa (job #1683907) | Cod sursa (job #2537670) | Cod sursa (job #2928948) | Cod sursa (job #1180823) | Cod sursa (job #2722729)
#include <fstream>
#include <vector>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
typedef pair < int, int > PII;
const int NMAX = 5e4 + 1;
const int INF = 1e9;
int N, M;
vector < PII > G[NMAX];
int d[NMAX];
bool Sel[NMAX];
static inline void Read ()
{
f.tie(nullptr);
f >> N >> M;
for(int i = 1; i <= M; ++i)
{
int X = 0, Y = 0, C = 0;
f >> X >> Y >> C;
G[X].push_back({C, Y});
}
return;
}
static inline void Initialize ()
{
for(int i = 1; i <= N; ++i)
d[i] = INF, Sel[i] = 0;
return;
}
static inline void Solve ()
{
Initialize();
d[1] = 0, Sel[1] = 1;
for(auto it : G[1])
if(it.first < d[it.second])
d[it.second] = it.first;
for(int i = 2; i <= N; ++i)
{
int Cost = INF, Node = 0;
for(int j = 1; j <= N; ++j)
if(!Sel[j] && d[j] < Cost)
Cost = d[j], Node = j;
if(Node == 0)
return;
Sel[Node] = 1;
for(auto it : G[Node])
if(Cost + it.first < d[it.second])
d[it.second] = Cost + it.first;
}
return;
}
static inline void Write ()
{
for(int i = 2; i <= N; ++i)
{
g << ((d[i] == INF) ? 0 : d[i]);
if(i != N)
g << ' ';
}
g << '\n';
return;
}
int main()
{
Read();
Solve();
Write();
return 0;
}