#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define x first
#define y second
#define getbit(u,i) ((u>>i)&1)
#define all(x) x.begin(),x.end()
#define N 200001
using namespace std;
typedef pair<int,int> ii;
typedef pair<double,double> dd;
int k,m,n,q,h[N],p[50010][16][6],s[50010][16][6],d[N],cur[6],nxt[6];
vector<ii> g[N],f[N];
vector<int> del;
const int inf=4557430888798830399;
void init()
{
for(int i=0;i<n;i++) h[i]=i/k+1;
for(int dep=(n+k-1)/k;dep>=0;dep--)
{
for(int u=dep;u<=min(n,dep+k-1);u++)
{
for(ii v : g[u])
{
p[u][0][v.x%k]=v.x;
s[u][0][v.x%k]=v.y;
}
for(int i=1;i<=15;i++)
{
for(int j=0;j<k;j++)
{
for(int l=0;l<k;l++)
{
if(p[u][i-1][l]!=inf)
{
p[u][i][j]=p[p[u][i-1][l]][i-1][j];
s[u][i][j]=min(s[u][i][j],s[u][i-1][l]+s[p[u][i-1][l]][i-1][j]);
}
}
}
}
}
}
}
void dijkstra(int u)
{
priority_queue<ii,vector<ii>,greater<ii>> pq;
d[u]=0;
pq.push({0,u});
while(!pq.empty())
{
int u=pq.top().y;
int du=pq.top().x;
pq.pop();
if(du!=d[u]) continue;
for(ii e : f[u])
{
int v=e.y,dv=e.x;
if(d[v]>du+dv)
{
d[v]=du+dv;
pq.push({d[v],v});
}
}
}
}
void get(int u,int v)
{
int base=h[v]-h[u];
for(int i=0;i<k;i++) cur[i]=i+u;
d[v]=inf;
for(int i=15;i>=0;i--)
{
if(getbit(base,i))
{
for(int j=0;j<k;j++)
{
if(cur[j]!=inf)
{
for(int l=0;l<k;l++)
{
if(s[cur[j]][i][l]!=inf)
{
del.push_back(cur[j]);
nxt[l]=1;
f[cur[j]].pb({s[cur[j]][i][l],p[cur[j]][i][l]});
}
}
}
}
for(int j=0;j<k;j++)
{
if(nxt[j])
{
cur[j]+=(1<<i)*k;
d[cur[j]]=inf;
}
else cur[j]=inf;
nxt[j]=0;
}
}
}
dijkstra(u);
for(int u : del) f[u].clear();
del.clear();
if(d[v]==inf) cout<<-1<<'\n';
else cout<<d[v]<<'\n';
}
signed 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;
g[u].pb({v,w});
}
memset(p,0x3f,sizeof(p));
memset(s,0x3f,sizeof(s));
init();
for(int i=0;i<n;i++) cout<<h[i]<<'\n';
while(q--)
{
int u,v;
cin>>u>>v;
get(u,v);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
89936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
115 ms |
181264 KB |
Execution killed with signal 7 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
86620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
86620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
89936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |