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
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "cassert" #include "queue" using namespace std; #define MAXN 93
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedef long long ll; int n , m , q; ll s; int A[MAXN];
ll L[MAXN][MAXN] , C[MAXN][MAXN]; ll W[MAXN][MAXN][2][MAXN]; ll dis[MAXN][MAXN];
int vis[MAXN]; void dijk( int s , ll cur , ll ds[MAXN] ) { rep( i , 1 , n ) vis[i] = 0 , ds[i] = 1e18; ds[s] = 0; while( 233 ) { pair<ll,int> mn = mp( 1e18 , 0 ); rep( i , 1 , n ) if( !vis[i] ) mn = min( mn , mp( ds[i] , i ) ); if( !mn.se ) return; int u = mn.se; vis[u] = 1; rep( v , 1 , n ) if( !vis[v] && C[u][v] - L[u][v] >= cur + ds[u] && ds[u] + L[u][v] < ds[v] ) ds[v] = ds[u] + L[u][v]; } }
void dijk2( int s , ll cur , ll ds[MAXN] ) { rep( i , 1 , n ) vis[i] = 0 , ds[i] = 1e18; ds[s] = 0; while( 233 ) { pair<ll,int> mn = mp( 1e18 , 0 ); rep( i , 1 , n ) if( !vis[i] ) mn = min( mn , mp( ds[i] , i ) ); if( !mn.se ) return; int u = mn.se; vis[u] = 1; rep( v , 1 , n ) if( !vis[v] && C[v][u] >= cur - ds[u] && ds[u] + L[v][u] < ds[v] ) ds[v] = ds[u] + L[v][u]; } }
ll lt[MAXN][MAXN]; vector<pair<ll,ll>> D[MAXN][MAXN];
int G[MAXN][MAXN]; ll ar[MAXN][MAXN];
void solve() { cin >> n >> m >> s >> q; memset( L , 0x3f , sizeof L ); rep( i , 1 , m ) { int u , v; ll l , c; scanf("%d%d%lld%lld",&u,&v,&l,&c); ++ u , ++ v; L[u][v] = L[v][u] = l , C[u][v] = C[v][u] = c; } memset( lt , -1 , sizeof lt ); rep( u , 1 , n ) lt[u][u] = s; rep( u , 1 , n ) rep( v , 1 , n ) if( L[u][v] < 1e18 ) dijk( u , C[u][v] , W[u][v][1] ) , dijk2( u , C[u][v] - L[u][v] , W[u][v][0] ); rep( u , 1 , n ) rep( v , 1 , n ) { rep( s , 1 , n ) rep( t , 1 , n ) if( L[s][t] < 1e18 ) { ll ds = W[s][t][0][u] + L[s][t] + W[t][s][1][v] , tim = C[s][t] - L[s][t] - W[s][t][0][u]; if( ds < 1e18 ) { lt[u][v] = max( lt[u][v] , tim ); D[u][v].eb( mp( tim , ds ) ); } } sort( all( D[u][v] ) ); int s = D[u][v].size() - 1; per( i , s - 1 , 0 ) D[u][v][i].se = min( D[u][v][i].se , D[u][v][i + 1].se ); } memset( G , 0x3f , sizeof G ); rep( u , 1 , n ) { G[u][u] = 0; dijk( u , 0 , dis[u] ); rep( v , 1 , n ) if( u != v && dis[u][v] < 1e18 ) G[u][v] = 1; } rep( k , 1 , n ) rep( i , 1 , n ) rep( j , 1 , n ) G[i][j] = min( G[i][j] , G[i][k] + G[k][j] ); memset( ar , 0x3f , sizeof ar ); rep( u , 1 , n ) rep( v , 1 , n ) { ar[u][v] = dis[u][v]; rep( k , 1 , n ) if( dis[k][v] < 1e18 ) ar[u][v] = min( ar[u][v] , G[u][k] * s + dis[k][v] ); } rep( i , 1 , q ) { int u , v; ll t; scanf("%d%d%lld",&u,&v,&t); ++ u , ++ v; if( lt[u][v] >= t ) { printf("%lld\n",lower_bound( all( D[u][v] ) , mp( t , 0ll ) ) -> se); } else { ll as = 1e18; rep( k , 1 , n ) if( lt[u][k] >= t ) as = min( as , s - t + ar[k][v] ); printf("%lld\n",as); } } }
signed main() {
solve(); }
|