#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 1e6 + 5;
const int oo = 1e9, mod = 1e9 + 7;
int n, m, k, q, c[75][75], visited[71];
string s;
vector<ii> adj[N];
// precalc
struct Node {
int vec, cnt_=0, sum_=0;
};
void bfs(int s, int t) {
if (s==t) {
cout << 0 << '\n';
return;
}
int ans=oo, pepe=0;
queue<Node> q;
visited[s]=true;
q.push({s, 0, 0});
while (!q.empty()) {
pepe++;
if (pepe >= 70*70-1) break;
Node tmp=q.front(); q.pop();
int u=tmp.vec, cnt=tmp.cnt_, sum=tmp.sum_;
//cout << u << ' ';
if (u==t) {
if (cnt<=k) ans=min(ans, sum);
//cout << cnt << ' ' << sum << '\n';
}
for (ii tmp_: adj[u]) {
int v=tmp_.se, dis=tmp_.fi;
Node newNode;
newNode.vec=v;
newNode.cnt_=cnt+1;
newNode.sum_=sum+dis;
if (cnt+1>k) continue;
q.push(newNode);
}
}
if (ans==oo) ans=-1;
cout << ans << '\n';
return;
}
void process() {
memset(c, 0x3f, sizeof(c));
cin >> n >> m;
foru(i,1,m) {
int u, v, t; cin >> u >> v >> t;
c[u][v]=min(c[u][v], t);
}
// shortest path at most k path
foru(i,1,n) {
foru(j,1,n) {
if (c[i][j]<=1e6+1) {
adj[i].pb({c[i][j], j});
}
}
}
cin >> k >> q;
while (q--) {
int s, t;
cin >> s >> t;
bfs(s,t);
}
return;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
/*
Xét các trường hợp đặc biệt
Kiểm tra lại input/output
Cố gắng trâu
Lật ngược bài toán
Keep calm and get VOI
Flow:
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
23896 KB |
Output is correct |
2 |
Correct |
6 ms |
23900 KB |
Output is correct |
3 |
Correct |
6 ms |
23944 KB |
Output is correct |
4 |
Correct |
6 ms |
23896 KB |
Output is correct |
5 |
Correct |
5 ms |
23924 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
31 ms |
24020 KB |
Output is correct |
4 |
Correct |
33 ms |
23900 KB |
Output is correct |
5 |
Incorrect |
417 ms |
24164 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
23896 KB |
Output is correct |
2 |
Correct |
6 ms |
23900 KB |
Output is correct |
3 |
Correct |
6 ms |
23944 KB |
Output is correct |
4 |
Correct |
6 ms |
23896 KB |
Output is correct |
5 |
Correct |
5 ms |
23924 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Execution timed out |
1045 ms |
25684 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
23896 KB |
Output is correct |
2 |
Correct |
6 ms |
23900 KB |
Output is correct |
3 |
Correct |
6 ms |
23944 KB |
Output is correct |
4 |
Correct |
6 ms |
23896 KB |
Output is correct |
5 |
Correct |
5 ms |
23924 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Correct |
7 ms |
23900 KB |
Output is correct |
8 |
Correct |
10 ms |
23900 KB |
Output is correct |
9 |
Correct |
31 ms |
24020 KB |
Output is correct |
10 |
Correct |
33 ms |
23900 KB |
Output is correct |
11 |
Incorrect |
417 ms |
24164 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |