#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;
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';
}
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));
}
if(k == 1) {
int pre[n+1];
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++) {
int u,v; cin>>u>>v;
if(u > v) cout<<-1<<'\n'; else cout<<pre[v] - pre[u-1]<<'\n';
}
return 0;
}
for(int i=1;i<=q;i++) {
int u,v;
cin>>u>>v;
solve(u,v);
}
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 |
81 ms |
100048 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3062 ms |
51912 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
62 ms |
95564 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
62 ms |
95564 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
81 ms |
100048 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |