#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename num_t>
using ordered_set = tree<num_t, null_type, less<num_t>, rb_tree_tag, tree_order_statistics_node_update>;
#ifdef pikachu
#include "welcome_to_python_is_slower_than_c++_club.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define debug(...) 69
#endif
template<typename T1, typename T2>istream& operator>>(istream& in, pair<T1, T2>& a) { return in >> a.first >> a.second; }
template<typename T1, typename T2>ostream& operator<<(ostream& out, pair<T1, T2> a) { return out << a.first << " " << a.second;}
template<typename T> void print(T t) { cout << t <<' '; }
template<typename T, typename... Args> void print(T t, Args... args) { print(t);print(args...); }
string operator*=(string& s, int cnt) { string t = s;for (size_t i = 1; i < cnt; i++)s += t;return s; }
string operator*(string s, int cnt) { return s *= cnt; }
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define len(x) (int)((x).size())
#define bk back()
#define elif else if
#define add insert
#define append push_back
#define pop pop_back
#define str string
#define in :
#define fr first
#define sc second
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int,int>
#define mii map<pii,int>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>b;i--)
#define el '\n'
#define printl(arg) cout << arg << endl
// #define print(arg) cout << arg
#define inputa(arg) for (auto& e : arg) cin >> e
#define printa(arg) for (auto& e : arg) print(e);
#define printr(arg) { printl(arg);return; }
#define printd(arg) printf("%0.3lf\n", arg)
const int mod=1e9+7;
const int INF=1e18;
const int MAX_N= 5e4+10;
const double PI=3.14159265358979323846;
int n,m,k,x,y,z,t,q,counter;
vector<vector<int>> a[MAX_N][5];
int dat[17][MAX_N][5][5], mask[MAX_N];// 18 = ceil(log2(n))
int cc[5][5];
int fun1(int x1,int y1,int lvl,int id){//associative
rep(i,0,5){
int j=i;
for(auto& vl in a[y1-1][j]){
rep(k,0,5){
dat[lvl][id][i][k]=min(dat[lvl][id][i][k],vl[1]+dat[lvl][y1][vl[0]][k]);
}
}
}
}
int fun2(int x1,int y1,int lvl,int id){//associative
rep(i,0,5){
rep(j,0,5){
if(dat[lvl][x1][i][j]!=mod){
for(auto& vl in a[y1-1][j]){
rep(k,0,5){
dat[lvl][id][i][k]=min(dat[lvl][id][i][k],dat[lvl][x1][i][j]+vl[1]+dat[lvl][y1][vl[0]][k]);
}
}
}
}
}
}
int fun3(int x1,int y1,int lvl){//associative
rep(i,0,5){
rep(j,0,5){
if(dat[lvl][x1][i][j]!=mod){
for(auto& vl in a[y1-1][j]){
rep(k,0,5){
cc[i][k]=min(cc[i][k],dat[lvl][x1][i][j]+vl[1]+dat[lvl][y1][vl[0]][k]);
}
}
}
}
}
}
void divi(int l, int r, int lev) { // generate dat and mask
if (l == r) return;
int m = (l+r)/2;
rep(i,0,5) rep(j,0,5) dat[lev][m][i][j]=((i==j)?0:mod);
rrep(i,m-1,l-1) fun1(i,i+1,lev,i);
// dat[lev][m+1] = a[m+1];
rep(i,0,5) rep(j,0,5) dat[lev][m+1][i][j]=((i==j)?0:mod);
rep(i,m+2,r+1) fun2(i-1,i,lev,i);
rep(i,m+1,r+1) mask[i] ^= 1<<lev;
divi(l,m,lev+1); divi(m+1,r,lev+1);
}
int query(int x,int y){
int t = __builtin_ctz(mask[x]^mask[y]);
return fun3(x,y,t);//operations
}
void code(){
cin>>q>>n>>m>>counter;
rep(lvl,0,17) rep(k,0,MAX_N) rep(i,0,5) rep(j,0,5)
dat[lvl][k][i][j]=mod;
while(m--){
cin>>x>>y>>z;
a[x/q][x%q].append({y%q,z});
}
divi(0,(n+q-1)/q-1,0);
while(counter--){
cin>>x>>y;
debug(x,y);
rep(i,0,5) rep(j,0,5) cc[i][j]=mod;
if(x/q==y/q) cout<<-1<<el;
else{
query(x/q,y/q);
debug(cc,5,5);
if(cc[x%q][y%q]==mod) cout<<-1<<el;
else cout<<cc[x%q][y%q]<<el;
}
}
rep(i,0,15) debug(dat[0][i],5,5);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("cowjog.in", "r", stdin);
// freopen("cowjog.out", "w", stdout);
int t = 1;
// cin>>t;
while(t--) code();
return 0;
}
Compilation message
toll.cpp: In function 'std::string operator*=(std::string&, int)':
toll.cpp:24:75: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
24 | string operator*=(string& s, int cnt) { string t = s;for (size_t i = 1; i < cnt; i++)s += t;return s; }
| ~~^~~~~
toll.cpp: In function 'long long int fun1(long long int, long long int, long long int, long long int)':
toll.cpp:82:1: warning: no return statement in function returning non-void [-Wreturn-type]
82 | }
| ^
toll.cpp: In function 'long long int fun2(long long int, long long int, long long int, long long int)':
toll.cpp:96:1: warning: no return statement in function returning non-void [-Wreturn-type]
96 | }
| ^
toll.cpp: In function 'long long int fun3(long long int, long long int, long long int)':
toll.cpp:110:1: warning: no return statement in function returning non-void [-Wreturn-type]
110 | }
| ^
toll.cpp: In function 'void code()':
toll.cpp:16:24: warning: statement has no effect [-Wunused-value]
16 | #define debug(...) 69
| ^~
toll.cpp:147:17: note: in expansion of macro 'debug'
147 | debug(x,y);
| ^~~~~
toll.cpp:16:24: warning: statement has no effect [-Wunused-value]
16 | #define debug(...) 69
| ^~
toll.cpp:152:21: note: in expansion of macro 'debug'
152 | debug(cc,5,5);
| ^~~~~
toll.cpp:16:24: warning: statement has no effect [-Wunused-value]
16 | #define debug(...) 69
| ^~
toll.cpp:157:25: note: in expansion of macro 'debug'
157 | rep(i,0,15) debug(dat[0][i],5,5);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
314 ms |
356920 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
340 ms |
363316 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
297 ms |
349620 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
297 ms |
349620 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
314 ms |
356920 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |