#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
using namespace std;
#define NMOD 3
#define ll long long
#define fi first
#define se second
#define pb push_back
#define log 17
#define mask(i) (1ll << (i))
#define setp(x) setprecision(x)
#define ALL(v) v.begin(), v.end()
#define ck(n, i) (((n) >> (i)) & 1)
#define getbit(x) __builtin_popcount(x)
const double PI = acos(-1);
const long long MOD = 1e9 + 7;
const long long MOD1 = 998244353;
const long long MODo = 123456789;
const int oo = 1e9;
const long long oo15 = 1e15, oo18 = 1e18+3, oooo = 922372036854775807;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct DATA
{
int st, tt, len;
bool operator < (const DATA &o) const
{
return len > o.len;
}
};
int dist[72][72][72], low[77][77];
vector<pair<int,int>> a[72];
void solve()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
low[i][j] = oo;
for (int e = 0; e <= n; e++)
dist[i][j][e] = oo;
}
for (int i = 1; i <= m; i++)
{
int u, v, t;
cin >> u >> v >> t;
low[u][v] = min(low[u][v], t);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (low[i][j] < oo)
{
a[i].pb({j, low[i][j]});
}
int k, q;
cin >> k >> q;
k = min(k, n);
for (int st = 1; st <= n; st++)
{
priority_queue<DATA> qe;
dist[st][st][0] = 0;
qe.push({st, 0, 0});
// cout << st << endl;
while(!qe.empty())
{
int u = qe.top().st, tt = qe.top().tt, len = qe.top().len; qe.pop();
// cout << u << " " << tt << " " << len << endl;
if (dist[st][u][tt] != len || tt == k) continue;
for (auto v : a[u])
if (dist[st][v.fi][tt + 1] > len + v.se)
{
dist[st][v.fi][tt + 1] = len + v.se;
if (tt + 1 < k)
qe.push({v.fi, tt + 1, dist[st][v.fi][tt + 1]});
}
}
}
while(q--)
{
int u, v;
cin >> u >> v;
int ans = oo;
for (int i = 0; i <= k; i++) ans = min(ans, dist[u][v][i]);
if (ans == oo) cout << "-1\n"; else cout << ans << "\n";
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int testcase = 1;
//cin >> testcase;
while(testcase--)
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1748 KB |
Output is correct |
2 |
Correct |
2 ms |
1748 KB |
Output is correct |
3 |
Correct |
3 ms |
1748 KB |
Output is correct |
4 |
Correct |
2 ms |
1748 KB |
Output is correct |
5 |
Correct |
3 ms |
1804 KB |
Output is correct |
6 |
Correct |
4 ms |
1748 KB |
Output is correct |
7 |
Correct |
6 ms |
1820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
25 ms |
1748 KB |
Output is correct |
8 |
Correct |
77 ms |
1876 KB |
Output is correct |
9 |
Correct |
4 ms |
1748 KB |
Output is correct |
10 |
Correct |
48 ms |
1816 KB |
Output is correct |
11 |
Correct |
20 ms |
1856 KB |
Output is correct |
12 |
Correct |
107 ms |
1976 KB |
Output is correct |
13 |
Correct |
87 ms |
1756 KB |
Output is correct |
14 |
Correct |
93 ms |
1764 KB |
Output is correct |
15 |
Correct |
236 ms |
2124 KB |
Output is correct |
16 |
Correct |
134 ms |
1880 KB |
Output is correct |
17 |
Correct |
88 ms |
1756 KB |
Output is correct |
18 |
Correct |
87 ms |
1760 KB |
Output is correct |
19 |
Correct |
222 ms |
1884 KB |
Output is correct |
20 |
Correct |
154 ms |
1872 KB |
Output is correct |
21 |
Correct |
84 ms |
1768 KB |
Output is correct |
22 |
Correct |
119 ms |
1900 KB |
Output is correct |
23 |
Correct |
135 ms |
1852 KB |
Output is correct |
24 |
Correct |
190 ms |
1768 KB |
Output is correct |
25 |
Correct |
87 ms |
1752 KB |
Output is correct |
26 |
Correct |
115 ms |
1888 KB |
Output is correct |
27 |
Correct |
130 ms |
1908 KB |
Output is correct |
28 |
Correct |
126 ms |
1884 KB |
Output is correct |
29 |
Correct |
89 ms |
1756 KB |
Output is correct |
30 |
Correct |
116 ms |
1896 KB |
Output is correct |
31 |
Correct |
194 ms |
2140 KB |
Output is correct |
32 |
Correct |
327 ms |
2300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
1748 KB |
Output is correct |
8 |
Correct |
2 ms |
1748 KB |
Output is correct |
9 |
Correct |
3 ms |
1748 KB |
Output is correct |
10 |
Correct |
2 ms |
1748 KB |
Output is correct |
11 |
Correct |
3 ms |
1804 KB |
Output is correct |
12 |
Correct |
4 ms |
1748 KB |
Output is correct |
13 |
Correct |
6 ms |
1820 KB |
Output is correct |
14 |
Correct |
25 ms |
1748 KB |
Output is correct |
15 |
Correct |
77 ms |
1876 KB |
Output is correct |
16 |
Correct |
4 ms |
1748 KB |
Output is correct |
17 |
Correct |
48 ms |
1816 KB |
Output is correct |
18 |
Correct |
20 ms |
1856 KB |
Output is correct |
19 |
Correct |
107 ms |
1976 KB |
Output is correct |
20 |
Correct |
87 ms |
1756 KB |
Output is correct |
21 |
Correct |
93 ms |
1764 KB |
Output is correct |
22 |
Correct |
236 ms |
2124 KB |
Output is correct |
23 |
Correct |
134 ms |
1880 KB |
Output is correct |
24 |
Correct |
88 ms |
1756 KB |
Output is correct |
25 |
Correct |
87 ms |
1760 KB |
Output is correct |
26 |
Correct |
222 ms |
1884 KB |
Output is correct |
27 |
Correct |
154 ms |
1872 KB |
Output is correct |
28 |
Correct |
84 ms |
1768 KB |
Output is correct |
29 |
Correct |
119 ms |
1900 KB |
Output is correct |
30 |
Correct |
135 ms |
1852 KB |
Output is correct |
31 |
Correct |
190 ms |
1768 KB |
Output is correct |
32 |
Correct |
87 ms |
1752 KB |
Output is correct |
33 |
Correct |
115 ms |
1888 KB |
Output is correct |
34 |
Correct |
130 ms |
1908 KB |
Output is correct |
35 |
Correct |
126 ms |
1884 KB |
Output is correct |
36 |
Correct |
89 ms |
1756 KB |
Output is correct |
37 |
Correct |
116 ms |
1896 KB |
Output is correct |
38 |
Correct |
194 ms |
2140 KB |
Output is correct |
39 |
Correct |
327 ms |
2300 KB |
Output is correct |
40 |
Correct |
17 ms |
1804 KB |
Output is correct |
41 |
Correct |
73 ms |
1812 KB |
Output is correct |
42 |
Correct |
9 ms |
1748 KB |
Output is correct |
43 |
Correct |
98 ms |
1828 KB |
Output is correct |
44 |
Correct |
26 ms |
1748 KB |
Output is correct |
45 |
Correct |
180 ms |
2144 KB |
Output is correct |
46 |
Correct |
90 ms |
1768 KB |
Output is correct |
47 |
Correct |
167 ms |
1980 KB |
Output is correct |
48 |
Correct |
113 ms |
1888 KB |
Output is correct |
49 |
Correct |
274 ms |
2228 KB |
Output is correct |
50 |
Correct |
101 ms |
1756 KB |
Output is correct |
51 |
Correct |
151 ms |
1872 KB |
Output is correct |
52 |
Correct |
96 ms |
1756 KB |
Output is correct |
53 |
Correct |
215 ms |
1868 KB |
Output is correct |
54 |
Correct |
97 ms |
1772 KB |
Output is correct |
55 |
Correct |
96 ms |
1884 KB |
Output is correct |
56 |
Correct |
244 ms |
1848 KB |
Output is correct |
57 |
Correct |
257 ms |
1768 KB |
Output is correct |
58 |
Correct |
87 ms |
1764 KB |
Output is correct |
59 |
Correct |
162 ms |
1884 KB |
Output is correct |
60 |
Correct |
204 ms |
1904 KB |
Output is correct |
61 |
Correct |
238 ms |
1912 KB |
Output is correct |
62 |
Correct |
88 ms |
1752 KB |
Output is correct |
63 |
Correct |
127 ms |
1880 KB |
Output is correct |
64 |
Correct |
142 ms |
2176 KB |
Output is correct |
65 |
Correct |
301 ms |
2236 KB |
Output is correct |