#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define fo(i,a,b) for (i=a; i<=b; i++)
using namespace std;
const int N=200005, M=400005;
int t0, n, m, q, k0, s, i, j, x, y, t1, t2, t, lans, ans;
int lst[N], e[M*2][4];
int f[N], d[N], h[N];
bool bz[N];
bool flag, flag2;
struct que{int x, h, i;}c[N];
struct map{int x, y, h;}c2[M];
void link(int x,int y)
{
e[++t][0]=lst[x], e[t][1]=y, e[t][2]=t1, e[t][3]=t2, lst[x]=t;
}
void up(int x)
{
int y=x>>1;
while (y && f[d[x]]<f[d[y]])
{
swap(d[x],d[y]), h[d[x]]=x, h[d[y]]=y;
x=y, y=x>>1;
}
}
void down()
{
int x=1, y=2;
while ((y<=t && f[d[x]]>f[d[y]]) || (y<t && f[d[x]]>f[d[y+1]]))
{
if (y<t && f[d[y+1]]<f[d[y]]) y++;
swap(d[x],d[y]), h[d[x]]=x, h[d[y]]=y;
x=y, y=x<<1;
}
}
void spfa()
{
t=d[1]=h[1]=1;
memset(f,120,sizeof(f)), f[1]=0; int w0=f[2];
memset(bz,0,sizeof(bz));
while (t)
{
int x=d[1], i=lst[x]; bz[x]=1;
while (i)
{
int y=e[i][1];
if (!bz[y] && f[x]+e[i][2]<f[y])
{
bool flag=(f[y]==w0); f[y]=f[x]+e[i][2];
if (flag) h[y]=++t, d[t]=y, up(t); else up(h[y]);
}
i=e[i][0];
}
h[x]=0, h[d[t]]=1, d[1]=d[t], t--, down();
}
}
bool cmp(que a,que b) {return a.h>b.h;}
bool cmp2(map a,map b) {return a.h>b.h;}
int find(int x)
{
if (h[x]==x) return x;
return h[x]=find(h[x]);
}
void merge(int x,int y)
{
x=find(x), y=find(y), h[x]=y, f[y]=min(f[x],f[y]);
}
int main()
{
scanf("%d",&t0);
while (t0)
{
memset(lst,0,sizeof(lst)), t=lans=0;
t0--, scanf("%d%d",&n,&m), flag=flag2=1;
fo(i,1,m)
{
scanf("%d%d%d%d",&x,&y,&t1,&t2), link(x,y), link(y,x);
if (t2>1) flag=0; if (x+1!=y) flag2=0;
}
scanf("%d%d%d",&q,&k0,&s);
if (flag)
{
spfa(); //
fo(i,1,q)
{
scanf("%d%d",&x,&y);
if (!y) printf("0\n"); else printf("%d\n",f[x]);
}
}
else
{
spfa(); //
fo(i,1,q) scanf("%d%d",&c[i].x,&c[i].h), c[i].i=i;
sort(c+1,c+q+1,cmp), t=0;
fo(x,1,n)
{
i=lst[x], h[x]=x;
while (i)
{
y=e[i][1];
if (y<x) c2[++t].x=y, c2[t].y=x, c2[t].h=e[i][3];
i=e[i][0];
}
}
sort(c2+1,c2+m+1,cmp2);
j=1;
fo(i,1,q)
{
while (j<=m && c2[j].h>c[i].h) merge(c2[j].x,c2[j].y), j++;
d[c[i].i]=f[find(c[i].x)];
}
fo(i,1,q) printf("%d\n",d[i]);
}
}
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 224.52 us | 1 MB + 760 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #2 | 248.84 us | 1 MB + 764 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #3 | 407.3 us | 1 MB + 768 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #4 | 484.36 us | 1 MB + 772 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #5 | 3.106 ms | 1 MB + 924 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #6 | 382.61 ms | 16 MB + 296 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #7 | 3.153 ms | 1 MB + 904 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #8 | 3.127 ms | 1 MB + 908 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #9 | 3.129 ms | 1 MB + 904 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #10 | 309.997 ms | 14 MB + 988 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #11 | 304.684 ms | 15 MB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #12 | 613.935 ms | 21 MB + 996 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #13 | 612.905 ms | 21 MB + 992 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #14 | 613.837 ms | 21 MB + 1000 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #15 | 4.604 ms | 1 MB + 992 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #16 | 4.472 ms | 1 MB + 992 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #17 | 613.338 ms | 21 MB + 948 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #18 | 613.205 ms | 21 MB + 948 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #19 | 229.445 ms | 22 MB + 352 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #20 | 228.688 ms | 22 MB + 372 KB | Runtime Error | Score: 0 | 显示更多 |