이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 1e5 + 5;
const int inf = 1e9 + 7;
const long long INF = (long long)1e18 + 7;
const int mod = 1e9 + 7;
struct DSU {
int n;
vector<int> par;
DSU(int _n = 0) {
n = _n;
par.assign(n + 2, -1);
}
int root(int v) {
return par[v] < 0 ? v : (par[v] = root(par[v]));
}
bool join(int x, int y) {
x = root(x);
y = root(y);
if (x == y) return false;
if (par[y] < par[x]) swap(x, y);
par[x] += par[y];
par[y] = x;
return true;
}
} dsu;
struct Edge {
int u, v, c;
Edge(int _u = 0, int _v = 0, int _c = 0) {
u = _u; v = _v; c = _c;
}
} e[N];
vector<ii> adj[N];
int n, K, m, q;
void read() {
cin >> K >> n >> m >> q;
dsu = DSU(n);
for(int i = 1; i <= m; i++) {
int u, v, c; cin >> u >> v >> c;
adj[u].push_back({v, c});
dsu.join(u, v);
e[i] = Edge(u, v, c);
}
}
long long dfs(int s, int t) {
vector<long long> d(n + 1, INF);
d[s] = 0;
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> heap;
heap.push({0, s});
while(!heap.empty()) {
auto tmp = heap.top(); heap.pop();
long long du = tmp.fi;
int u = tmp.se;
if (du != d[u]) continue;
if (u == t) break;
for(auto [v, c]: adj[u]) if (minimize(d[v], du + c)) {
heap.push({d[v], v});
}
}
return d[t] == INF ? -1 : d[t];
}
void sub1() {
vector<long long> d(n + 2, 0);
for(int i = 1; i <= m; i++) d[e[i].v] = e[i].c;
for(int i = 1; i <= n; i++) d[i] += d[i - 1];
while(q--) {
int u, v; cin >> u >> v;
if (u > v || dsu.root(u) != dsu.root(v)) {
cout << -1 << endl;
continue;
}
cout << d[v] - (u > 0 ? d[u - 1] : 0) << endl;
}
}
void solve() {
// if (K == 1) {
// sub1();
// return;
// }
while(q--) {
int u, v; cin >> u >> v;
if (dsu.root(u) != dsu.root(v)) {
cout << -1 << endl;
continue;
}
cout << dfs(u, v) << endl;
}
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int test = 1;
// cin >> test;
while(test--) {
read();
solve();
}
return 0;
}
// rmq - rmq2d
// hash
// fw - fw2d
// segtree
컴파일 시 표준 에러 (stderr) 메시지
toll.cpp: In function 'int main()':
toll.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |