1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| #include <bits/stdc++.h> using namespace std;
namespace FastIO { #define iL (1 << 20) char ibuf[iL],*iS = ibuf + iL,*iT = ibuf + iL; #define gc() (iS == iT ? (iT = (iS = ibuf) + fread(ibuf,1,iL,stdin),iS == iT ? EOF : *iS++) : *iS++) template<typename T> inline void read(T &a) { char ch;int sign = 0; for(ch = gc();!isdigit(ch);ch = gc()) if(ch == '-') sign = 1; a = ch & 15; for(ch = gc();isdigit(ch);ch = gc()) a = (a << 3) + (a << 1) + (ch & 15); if(sign) a = -a; } char Out[iL],*iter = Out; #define flush() fwrite(Out,1,iter - Out,stdout),iter = Out template<typename T> inline void write(T x,char end = '\n') { int c[40],l = 0;if(x < 0) *iter++ = '-',x = -x; do c[++l] = x % 10,x /= 10; while(x); while(l) *iter++ = c[l--] + '0'; *iter++ = end;flush(); } #undef iL #undef gc #undef flush } using namespace FastIO;
const int N = 95,M = N * N,Ts = N * N * N; const int MaxQ = 3e6 + 5; typedef long long ll; int n,m,Q; ll e[N][N],Lim[N][N],dis1[N][N],dis2[N][N],dis3[N][N],dis4[N][N]; ll S;
ll dsx[M][N],dsy[M][N];
bool vst[N];
void Dijx(int st,ll dst,ll *dis) { for(int i = 1;i <= n;i++) dis[i] = -1,vst[i] = 0; dis[st] = dst; for(int i = 1;i <= n;i++) { int k = 0; for(int j = 1;j <= n;j++) if(!vst[j] && (!k || dis[j] > dis[k])) k = j; if(!k) break; vst[k] = true; for(int j = 1;j <= n;j++) if(dis[k] <= Lim[j][k] && dis[j] < dis[k] - e[j][k]) dis[j] = dis[k] - e[j][k]; } } void Dijy(int st,ll dst,ll *dis) { for(int i = 1;i <= n;i++) dis[i] = 1e18,vst[i] = 0; dis[st] = dst; for(int i = 1;i <= n;i++) { int k = 0; for(int j = 1;j <= n;j++) if(!vst[j] && (!k || dis[j] < dis[k])) k = j; if(!k) break; vst[k] = true; for(int j = 1;j <= n;j++) if(dis[k] + e[k][j] <= Lim[k][j] && dis[j] > dis[k] + e[k][j]) dis[j] = dis[k] + e[k][j]; } }
int ecnt,tcnt; struct Node { int st,eid;ll tim; Node(){} Node(const int _st,const int _eid,const ll _tim): st(_st),eid(_eid),tim(_tim){} bool operator < (const Node &rhs) const { return tim < rhs.tim;} }; Node ts[Ts]; void Init() { memset(dis1,0x3f,sizeof dis1); memset(dis2,0x3f,sizeof dis2); memset(dis3,0x3f,sizeof dis3); memset(dis4,0x3f,sizeof dis4); for(int x = 1;x <= n;x++) for(int y = 1;y <= n;y++) if(Lim[x][y]) { ++ecnt; Dijx(x,Lim[x][y] - e[x][y],dsx[ecnt]); Dijy(y,Lim[x][y],dsy[ecnt]); for(int u = 1;u <= n;u++) if(dsx[ecnt][u] >= 0) { ts[++tcnt] = Node(u,ecnt,dsx[ecnt][u]); for(int v = 1;v <= n;v++) if(dsy[ecnt][v] < S) dis1[u][v] = min(dis1[u][v],dsy[ecnt][v] - dsx[ecnt][u]),dis2[u][v] = 1; } } for(int i = 1;i <= n;i++) dis1[i][i] = dis2[i][i] = dis3[i][i] = dis4[i][i] = 0; for(int k = 1;k <= n;k++) for(int i = 1;i <= n;i++) for(int j = 1;j <= n;j++) dis2[i][j] = min(dis2[i][j],dis2[i][k] + dis2[k][j]); for(int i = 1;i <= n;i++) for(int k = 1;k <= n;k++) for(int j = 1;j <= n;j++) if(dis2[i][k] <= n && dis1[k][j] < S) dis3[i][j] = min(dis3[i][j],dis2[i][k] * S + dis1[k][j]); } void Upd(int st,int eid) { for(int v = 1;v <= n;v++) if(dsy[eid][v] < S) dis4[st][v] = min(dis4[st][v],dsy[eid][v] - dsx[eid][st]); }
ll Qry(int u,int v,ll t) { if(dis4[u][v] + t < S) return dis4[u][v]; ll res = LONG_LONG_MAX; for(int i = 1;i <= n;i++) if(dis4[u][i] < S) res = min(res,dis3[i][v]); return res + S - t; } int qu[MaxQ],qv[MaxQ]; ll qt[MaxQ],ans[MaxQ]; int srt[MaxQ];
int main() { read(n);read(m);read(S);read(Q); memset(e,0x3f,sizeof e); for(int i = 1,x,y;i <= m;i++) read(x),read(y),++x,++y,read(e[x][y]),read(Lim[x][y]),e[y][x] = e[x][y],Lim[y][x] = Lim[x][y]; Init(); for(int i = 1;i <= Q;i++) read(qu[i]),read(qv[i]),++qu[i],++qv[i],read(qt[i]),srt[i] = i; sort(srt + 1,srt + Q + 1,[&](const int &x,const int &y) { return qt[x] < qt[y];}); sort(ts + 1,ts + tcnt + 1); for(int i = tcnt,j = Q;j >= 1;j--) { while(i >= 1 && ts[i].tim >= qt[srt[j]]) Upd(ts[i].st,ts[i].eid),--i; ans[srt[j]] = Qry(qu[srt[j]],qv[srt[j]],qt[srt[j]]); } for(int i = 1;i <= Q;i++) write(ans[i]); return 0; }
|