#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];
vector<pair<int,int>> a[72];
void solve()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int u, v, t;
cin >> u >> v >> t;
a[u].pb({v, t});
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int e = 0; e <= n; e++)
dist[i][j][e] = oo;
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;
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 |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
3 ms |
1748 KB |
Output is correct |
3 |
Correct |
3 ms |
1748 KB |
Output is correct |
4 |
Correct |
3 ms |
1748 KB |
Output is correct |
5 |
Correct |
4 ms |
1748 KB |
Output is correct |
6 |
Correct |
7 ms |
1748 KB |
Output is correct |
7 |
Correct |
7 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
19 ms |
1748 KB |
Output is correct |
8 |
Correct |
62 ms |
1916 KB |
Output is correct |
9 |
Correct |
5 ms |
1748 KB |
Output is correct |
10 |
Correct |
42 ms |
1796 KB |
Output is correct |
11 |
Correct |
23 ms |
1900 KB |
Output is correct |
12 |
Correct |
117 ms |
2028 KB |
Output is correct |
13 |
Correct |
227 ms |
6560 KB |
Output is correct |
14 |
Correct |
324 ms |
6496 KB |
Output is correct |
15 |
Execution timed out |
1077 ms |
6984 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
3 ms |
1748 KB |
Output is correct |
9 |
Correct |
3 ms |
1748 KB |
Output is correct |
10 |
Correct |
3 ms |
1748 KB |
Output is correct |
11 |
Correct |
4 ms |
1748 KB |
Output is correct |
12 |
Correct |
7 ms |
1748 KB |
Output is correct |
13 |
Correct |
7 ms |
1876 KB |
Output is correct |
14 |
Correct |
19 ms |
1748 KB |
Output is correct |
15 |
Correct |
62 ms |
1916 KB |
Output is correct |
16 |
Correct |
5 ms |
1748 KB |
Output is correct |
17 |
Correct |
42 ms |
1796 KB |
Output is correct |
18 |
Correct |
23 ms |
1900 KB |
Output is correct |
19 |
Correct |
117 ms |
2028 KB |
Output is correct |
20 |
Correct |
227 ms |
6560 KB |
Output is correct |
21 |
Correct |
324 ms |
6496 KB |
Output is correct |
22 |
Execution timed out |
1077 ms |
6984 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |