#pragma GCC optimize("O3") #include "bits/stdc++.h" using namespace std; #define rep(i,a,b) for(int i=(a); i<(b); ++i) #define all(x) x.begin(),x.end() #define sz(x) int(x.size()) typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; const int oo = 1.1e9; mt19937 rng(69); const int N = 4000; int Tl[N], Tr[N], Sl[N], Sr[N]; struct segs{ int ub; // maximum allowed here + 1 int L[N]; int R[N]; int size = 0; void add(int l, int r){ L[size] = l; R[size] = r; ++size; } void clear(){ size = 0; } void unite(segs& seg, int w){ memcpy(Tl,L,4*size); memcpy(Tr,R,4*size); Tl[size] = oo; memcpy(Sl,seg.L,4*seg.size); memcpy(Sr,seg.R,4*seg.size); Sl[seg.size] = oo; int ss = size, os = seg.size; clear(); int i = 0, j = 0; int lastL=0, lastR=0; while(i<ss or j<os){ int ovl = Sl[j]<oo?(Sl[j]+w-1)/w:oo; if (Tl[i] <= ovl){ if (Tl[i] <= lastR){ lastR = max(Tr[i],lastR); }else{ if (lastR) add(lastL,lastR); lastL = Tl[i]; lastR = Tr[i]; } ++i; }else{ int ovr = min((Sr[j]+w-1)/w, ub); if (ovl < ovr){ if (ovl <= lastR){ lastR = max(ovr,lastR); }else{ if (lastR) add(lastL,lastR); lastL = ovl; lastR = ovr; } } ++j; } } if (lastR) add(lastL,lastR); } }; void solve(){ int n,m; cin >> n >> m; vi cap(n); for(auto& c : cap){ cin >> c; ++c; } // vector<array<int,3>> es; vvi adj1(n); rep(i,0,m){ int x,y,w; cin >> x >> y >> w; --x, --y; if (w>1) { es.push_back({x,y,w}); } else adj1[x].push_back(y); } // compress one graph int diam = 100; { { vvi d(n,vi(n,1e9)); rep(i,0,n) d[i][i] = 0; rep(i,0,n) for(auto j : adj1[i]) d[i][j] = 1; rep(k,0,n) rep(i,0,n) rep(j,0,n){ d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } diam = 0; rep(i,0,n) rep(j,0,n) if (d[i][j] < 1000000){ diam = max(diam, d[i][j]); } } while(diam>4){ vi ord(n); iota(all(ord),0); shuffle(all(ord),rng); for(auto st : ord){ vi d(n,-1); vvi par(__lg(n)+1, vi(n)); par[0][st] = st; d[st] = 0; queue<array<int,2>> q; q.push({st,0}); while(sz(q)){ auto [at,dist] = q.front(); q.pop(); d[at] = dist; for(auto to : adj1[at]) if (d[to]<0){ q.push({to,dist+1}); d[to] = dist+1; par[0][to] = at; } } rep(i,0,__lg(n)) rep(j,0,n) par[i+1][j] = par[i][par[i][j]]; // add powers rep(i,0,n) if (d[i]>1){ for(int w = 2, j = 1; w<=d[i]; ++j, w*=2){ adj1[par[j][i]].push_back(i); } } } vvi d(n,vi(n,1e9)); rep(i,0,n) d[i][i] = 0; rep(i,0,n) for(auto j : adj1[i]) d[i][j] = 1; rep(k,0,n) rep(i,0,n) rep(j,0,n){ d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } diam = 0; rep(i,0,n) rep(j,0,n) if (d[i][j] < 1000000){ diam = max(diam, d[i][j]); } } rep(i,0,n){ sort(all(adj1[i])); adj1[i].erase(unique(all(adj1[i])),end(adj1[i])); for(auto j : adj1[i]){ es.push_back({i,j,1}); } } } // cout << "SIZE EDGES: " << sz(es) << '\n'; // cout << "DIAMETER: " << diam << '\n'; // cout << endl; // int lo = 0, hi = cap[n-1]-1; while(lo<hi){ int mid = (lo+hi+1)/2; vector<segs> a(n); rep(i,0,n) a[i].ub = cap[i]; a[n-1].add(mid, cap[n-1]); rep(i,0,30*(diam+1)){ shuffle(all(es),rng); for(auto [x,y,w] : es){ a[x].unite(a[y], w); } if (a[0].size and *a[0].L == 1) break; } if (a[0].size and *a[0].L == 1){ lo = mid; }else{ hi = mid-1; } } if (lo==0) cout << "-1\n"; else cout << lo << '\n'; } int main(){ cin.tie(NULL),cin.sync_with_stdio(false); int t; cin >> t; while(t--) solve(); }
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | #pragma GCC optimize("O3") #include "bits/stdc++.h" using namespace std; #define rep(i,a,b) for(int i=(a); i<(b); ++i) #define all(x) x.begin(),x.end() #define sz(x) int(x.size()) typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; const int oo = 1.1e9; mt19937 rng(69); const int N = 4000; int Tl[N], Tr[N], Sl[N], Sr[N]; struct segs{ int ub; // maximum allowed here + 1 int L[N]; int R[N]; int size = 0; void add(int l, int r){ L[size] = l; R[size] = r; ++size; } void clear(){ size = 0; } void unite(segs& seg, int w){ memcpy(Tl,L,4*size); memcpy(Tr,R,4*size); Tl[size] = oo; memcpy(Sl,seg.L,4*seg.size); memcpy(Sr,seg.R,4*seg.size); Sl[seg.size] = oo; int ss = size, os = seg.size; clear(); int i = 0, j = 0; int lastL=0, lastR=0; while(i<ss or j<os){ int ovl = Sl[j]<oo?(Sl[j]+w-1)/w:oo; if (Tl[i] <= ovl){ if (Tl[i] <= lastR){ lastR = max(Tr[i],lastR); }else{ if (lastR) add(lastL,lastR); lastL = Tl[i]; lastR = Tr[i]; } ++i; }else{ int ovr = min((Sr[j]+w-1)/w, ub); if (ovl < ovr){ if (ovl <= lastR){ lastR = max(ovr,lastR); }else{ if (lastR) add(lastL,lastR); lastL = ovl; lastR = ovr; } } ++j; } } if (lastR) add(lastL,lastR); } }; void solve(){ int n,m; cin >> n >> m; vi cap(n); for(auto& c : cap){ cin >> c; ++c; } // vector<array<int,3>> es; vvi adj1(n); rep(i,0,m){ int x,y,w; cin >> x >> y >> w; --x, --y; if (w>1) { es.push_back({x,y,w}); } else adj1[x].push_back(y); } // compress one graph int diam = 100; { { vvi d(n,vi(n,1e9)); rep(i,0,n) d[i][i] = 0; rep(i,0,n) for(auto j : adj1[i]) d[i][j] = 1; rep(k,0,n) rep(i,0,n) rep(j,0,n){ d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } diam = 0; rep(i,0,n) rep(j,0,n) if (d[i][j] < 1000000){ diam = max(diam, d[i][j]); } } while(diam>4){ vi ord(n); iota(all(ord),0); shuffle(all(ord),rng); for(auto st : ord){ vi d(n,-1); vvi par(__lg(n)+1, vi(n)); par[0][st] = st; d[st] = 0; queue<array<int,2>> q; q.push({st,0}); while(sz(q)){ auto [at,dist] = q.front(); q.pop(); d[at] = dist; for(auto to : adj1[at]) if (d[to]<0){ q.push({to,dist+1}); d[to] = dist+1; par[0][to] = at; } } rep(i,0,__lg(n)) rep(j,0,n) par[i+1][j] = par[i][par[i][j]]; // add powers rep(i,0,n) if (d[i]>1){ for(int w = 2, j = 1; w<=d[i]; ++j, w*=2){ adj1[par[j][i]].push_back(i); } } } vvi d(n,vi(n,1e9)); rep(i,0,n) d[i][i] = 0; rep(i,0,n) for(auto j : adj1[i]) d[i][j] = 1; rep(k,0,n) rep(i,0,n) rep(j,0,n){ d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } diam = 0; rep(i,0,n) rep(j,0,n) if (d[i][j] < 1000000){ diam = max(diam, d[i][j]); } } rep(i,0,n){ sort(all(adj1[i])); adj1[i].erase(unique(all(adj1[i])),end(adj1[i])); for(auto j : adj1[i]){ es.push_back({i,j,1}); } } } // cout << "SIZE EDGES: " << sz(es) << '\n'; // cout << "DIAMETER: " << diam << '\n'; // cout << endl; // int lo = 0, hi = cap[n-1]-1; while(lo<hi){ int mid = (lo+hi+1)/2; vector<segs> a(n); rep(i,0,n) a[i].ub = cap[i]; a[n-1].add(mid, cap[n-1]); rep(i,0,30*(diam+1)){ shuffle(all(es),rng); for(auto [x,y,w] : es){ a[x].unite(a[y], w); } if (a[0].size and *a[0].L == 1) break; } if (a[0].size and *a[0].L == 1){ lo = mid; }else{ hi = mid-1; } } if (lo==0) cout << "-1\n"; else cout << lo << '\n'; } int main(){ cin.tie(NULL),cin.sync_with_stdio(false); int t; cin >> t; while(t--) solve(); } |