#include<iostream>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<deque>
using namespace std;
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define fi first
#define se second
#define ll long long
#define mp make_pair
#define pb push_back
#define ii pair<ll, int>
#define task "test"
const int inf = 1e9 + 7;
const ll infll = (ll)1e18 + 7;
const int MOD = 1e9 + 7;
const int N = 2e6 + 3;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}
int k,n,m,q;
int u[N], v[N];
vector<ii> adj[N];
void solve(int x,int y) {
vector<ll> d(n+1, inf);
d[x] = 0;
priority_queue< ii, vector<ii>, greater<ii> > p;
p.push(mp(0,x));
while(!p.empty()) {
int u = p.top().se;
int val = p.top().fi; p.pop();
if(val != d[u]) continue;
for(ii X : adj[u]) {
int v = X.se;
int he = X.fi;
if(d[v] > val + he) {
d[v] = val + he;
p.push(mp(d[v], v));
}
}
}
if(d[y] == inf) cout<<-1<<'\n'; else cout<<d[y]<<'\n';
}
void sub1() {
vector<int> pre(n+1,0);
pre[0] = 0;
for(int i=1;i<=n;i++) {
pre[i] = pre[i-1] + adj[i-1][0].fi;
}
for(int i=1;i<=q;i++) {
if(u[i] > v[i]) {
cout<<-1<<'\n';
continue;
}
if(u[i] == 0) cout<<pre[v[i]]<<'\n'; else cout<<pre[v[i]] - pre[u[i]-1]<<'\n';
}
}
void sub2() {
vector<ll> d(n+1, inf);
d[0] = 0;
priority_queue< ii, vector<ii>, greater<ii> > p;
p.push(mp(0,0));
while(!p.empty()) {
int u = p.top().se;
int val = p.top().fi; p.pop();
if(val != d[u]) continue;
for(ii X : adj[u]) {
int v = X.se;
int he = X.fi;
if(d[v] > val + he) {
d[v] = val + he;
p.push(mp(d[v], v));
}
}
}
for(int i=1;i<=q;i++) {
if(d[v[i]] == inf) cout<<-1<<'\n'; else cout<<d[v[i]]<<'\n';
}
}
void bruteforce() {
for(int i=1;i<=q;i++) {
solve(u[i], v[i]);
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>k>>n>>m>>q;
for(int i=1;i<=m;i++) {
int u,v,w; cin>>u>>v>>w;
adj[u].pb(mp(w,v));
}
int count_sub2 = 0;
for(int i=1;i<=q;i++) {
cin>>u[i]>>v[i];
if(u[i] == 0) count_sub2++;
}
if(k == 1) sub1(); else
if(count_sub2 == q) sub2(); else
bruteforce();
return 0;
}
/* 5 14 5 5
0 5 9
5 12 10
0 7 7
7 12 8
4 7 10
0 12
0 5
0 7
7 12
0 13 */
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
76 ms |
99424 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
50304 KB |
Output is correct |
2 |
Runtime error |
61 ms |
95572 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
63 ms |
95612 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
63 ms |
95612 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
76 ms |
99424 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |