이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// `Be name khoda` //
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define all(x) x.begin(), x.end()
#define fi first
#define se second
const int maxn5 = 2e5 + 10;
const int inf = 1e9;
int dis[maxn5], prec[maxn5], no = 0, cmp[maxn5], h[maxn5];
vector <pair<int, int>> jda[maxn5], adj[maxn5];
bool mark[maxn5];
inline void dfs(int v){
mark[v] = true;
cmp[v] = no;
for(auto [u, t] : adj[v]){
h[u] = h[v] + t;
dfs(u);
}
return;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int k, n, m, q; cin >> k >> n >> m >> q;
for(int i = 0; i < m; i++){
int a, b, t; cin >> a >> b >> t;
jda[b].pb({a, t});
adj[a].pb({b, t});
}
fill(prec, prec + n + 5, inf);
prec[0] = 0;
for(int i = 0; i < n; i++) for(auto [u, t] : adj[i])
prec[u] = min(prec[u], prec[i] + t);
for(int i = 0; i < n; i++) if(prec[i] == inf)
prec[i] = -1;
for(int i = 0; i < n; i++) if(!mark[i] && k == 1){
dfs(i);
no++;
}
for(int i = 0; i < q; i++){
int a, b; cin >> a >> b;
if(a == 0){
cout << prec[b] << '\n';
continue;
}
if(k == 1){
if(cmp[a] == cmp[b])
cout << h[b] - h[a] << '\n';
else
cout << -1 << '\n';
continue;
}
fill(dis, dis + n + 4, inf);
dis[b] = 0;
for(int i = b; i > a; i--){
for(auto [u, t] : jda[i])
dis[u] = min(dis[u], dis[i] + t);
}
if(dis[a] == inf)
dis[a] = -1;
cout << dis[a] << '\n';
}
}
# | 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... |