이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
int N, M, Q, K;
vector<vector<long long int>> states;
vector<vector<long long int>> AdjMatrix;
void preprocess(){
bool changed = true;
vector<vector<long long int>> newState(N, vector<long long int>(N));
vector<vector<int>> hasBeenChanged(N, vector<int>(N, 0));
for(int i = 0; i < N; ++i) hasBeenChanged[i][i] = 1;
vector<vector<int>> nextToBeChanged(N, vector<int>(N, 0));
for(int i = 0; i < K and changed; ++i){
newState = states;
changed = false;
nextToBeChanged = vector<vector<int>>(N, vector<int>(N, 0));
for(int nodeFrom = 0; nodeFrom < N; ++nodeFrom){
for(int nodeTo = 0; nodeTo < N; ++nodeTo){
if(nodeFrom == nodeTo and i > 0) continue;
if(!hasBeenChanged[nodeFrom][nodeTo]) continue;
for(int destination = 0; destination < N; ++destination){
if(destination == nodeTo) continue;
long long int dist = (long long)states[nodeFrom][nodeTo] + (long long)AdjMatrix[nodeTo][destination];
int nextNode = destination;
//cout << "Iteration: " << i+1 << "\t From: " << nodeFrom << " and " << nodeTo << " to " << nextNode << " distance " << dist << " versus " << states[nodeFrom][nextNode] << endl;
if(states[nodeFrom][nextNode] > dist){
changed = true;
newState[nodeFrom][nextNode] = min(newState[nodeFrom][nextNode], dist);
nextToBeChanged[nodeFrom][nextNode] = 1;
}
}
}
}
states = newState;
hasBeenChanged = nextToBeChanged;
}
}
int main(){
cin >> N >> M;
AdjMatrix = vector<vector<long long int>>(N, vector<long long int>(N, 1e18));
states = vector<vector<long long int>>(N, vector<long long int>(N, 1e18));
for(int i = 0; i < N; ++i) states[i][i] = 0;
for(int i = 0; i < N; ++i) AdjMatrix[i][i] = 0;
int a, b;
long long w;
for(int i = 0; i < M; ++i){
cin >> a >> b >> w;
a--; b--;
AdjMatrix[a][b] = min(AdjMatrix[a][b], w);
}
cin >> K >> Q;
preprocess();
while(Q--){
cin >> a >> b;
a--; b--;
if(states[a][b] == 1e18) cout << -1 << endl;
else cout << states[a][b] << endl;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |