#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
#define ll long long
int n, m, q, k;
vector<pair<int, int> > adjl[75];
bool visited[75];
int adjm[75][75];
bool check(ll time, int c, int d){
memset(visited, false, sizeof(visited));
queue<pair<int, pair<int, ll> > > q;
q.push(make_pair(c, make_pair(0, 0)));
while(!q.empty()){
pair<int, pair<int, int> > iii = q.front(); q.pop();
if(iii.second.first > k) continue;
if(iii.second.second > time) continue;
if(iii.first == d) return true;
if(visited[iii.first]) continue;
visited[iii.first] = true;
for(int i = 0; i < (int)adjl[iii.first].size(); i++){
q.push(make_pair(adjl[iii.first][i].first, make_pair(iii.second.first + 1, iii.second.second + adjl[iii.first][i].second)));
}
}
return false;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++){
int a, b, t; cin >> a >> b >> t;
if(adjm[a][b] == 0) adjm[a][b] = t;
else adjm[a][b] = min(adjm[a][b], t);
}
ll tmax = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(adjm[i][j] != 0){
adjl[i].push_back(make_pair(j, adjm[i][j]));
tmax += adjm[i][j];
adjm[i][j] = 1;
}
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(adjm[i][j] != 1){
adjm[i][j] = 1000000001;
if(i == j) adjm[i][j] = 0;
}
}
}
cin >> k >> q;
for(int x = 1; x <= n; x++){
for(int y = 1; y <= n; y++){
for(int z = 1; z <= n; z++){
adjm[y][z] = min(adjm[y][z], adjm[y][x] + adjm[x][z]);
}
}
}
for(int i = 0; i < q; i++){
int c, d; cin >> c >> d;
if(adjm[c][d] > k){
cout << -1 << "\n"; continue;
}
ll lower = 0;
ll upper = tmax;
while(lower < upper){
ll mid = (ll)(lower + upper) / 2;
if(check(mid, c, d))upper = mid;
else lower = mid + 1;
}
cout << lower << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
360 KB |
Output is correct |
2 |
Correct |
99 ms |
344 KB |
Output is correct |
3 |
Incorrect |
325 ms |
484 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |