Pagini recente » Cod sursa (job #2663970) | Cod sursa (job #2046981) | Cod sursa (job #2321439) | Cod sursa (job #1598185) | Cod sursa (job #3162294)
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define eb emplace_back
#define cin in
#define cout out
using namespace std;
class InParser {
vector<char> str;
int ptr;
ifstream fin;
char getChar() {
if (ptr == int(str.size())) {
fin.read(str.data(), str.size());
ptr = 0;
}
return str[ptr++];
}
template<class T>
T getInt() {
char chr = getChar();
while (!isdigit(chr) && chr != '-')
chr = getChar();
int sgn = +1;
if (chr == '-') {
sgn = -1;
chr = getChar();
}
T num = 0;
while (isdigit(chr)) {
num = num * 10 + chr - '0';
chr = getChar();
}
return sgn * num;
}
public:
InParser(const char* name) : str(1e5), ptr(str.size()), fin(name) { }
~InParser() { fin.close(); }
template<class T>
friend InParser& operator>>(InParser& in, T& num) {
num = in.getInt<T>();
return in;
}
};
class OutParser {
vector<char> str;
int ptr;
ofstream fout;
void putChar(char chr) {
if (ptr == int(str.size())) {
fout.write(str.data(), str.size());
ptr = 0;
}
str[ptr++] = chr;
}
template<class T>
void putInt(T num) {
if (num < 0) {
putChar('-');
num *= -1;
}
if (num > 9)
putInt(num / 10);
putChar(num % 10 + '0');
}
public:
OutParser(const char* name) : str(1e5), ptr(0), fout(name) { }
~OutParser() { fout.write(str.data(), ptr); fout.close(); }
template<class T>
friend OutParser& operator<<(OutParser& out, const T num) {
out.putInt(num);
return out;
}
friend OutParser& operator<<(OutParser& out, const char chr) {
out.putChar(chr);
return out;
}
friend OutParser& operator<<(OutParser& out, const char* str) {
for (int i = 0; str[i]; i++)
out.putChar(str[i]);
return out;
}
};
InParser in("dijkstra.in");
OutParser out("dijkstra.out");
const int MAX=50000;
int cost[MAX+5];
struct cmp{
bool operator ()(int x,int y)
{
return (cost[x] > cost[y]);
}
};
int n,m,x,y,z;
vector<pii> v[MAX+5];///din x -> y cost z
priority_queue<int,vector<int>,cmp> Q;
void dijkstra(int start)
{
Q.emplace(start);
cost[start]=0;
while(!Q.empty())
{
int nod=Q.top();
Q.pop();
for(auto x: v[nod])
{
if(cost[nod]+x.second<cost[x.first])
{
cost[x.first]=cost[nod]+x.second;
Q.push(x.first);
}
}
}
}
int main()
{
cin>>n>>m;
while(m--)
{
cin>>x>>y>>z;
v[x].eb(y,z);
}
for(int i=1;i<=n;i++)
cost[i]=2e9;
dijkstra(1);
for(int i=2;i<=n;i++,cout<<' ')
if(cost[i]==2e9)
cout<<0;
else
cout<<cost[i];
return 0;
}