Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- #include <iostream>
- using namespace std;
- int main()
- {
- int n, m, q;
- cin>>n>>m>>q;
- long long a, b, c;
- long long shortestpath[n][n];
- for(int i=0; i<n; i++){
- for(int j=0; j<n; j++){
- shortestpath[i][j]=1e18;
- }
- shortestpath[i][i] = 0;
- }
- for(int j=0; j<m; j++){
- cin>>a>>b>>c;
- a--;
- b--;
- shortestpath[a][b]=min(shortestpath[a][b], c);
- shortestpath[b][a] = min(shortestpath[a][b], c);
- }
- for(int k=0; k<n; k++){
- for(int i=0; i<n; i++){
- for(int j=0; j<n; j++){
- if((shortestpath[j][k]!=1e18)and(shortestpath[i][k]!=1e18))
- if((i!=k)and(j!=k) and i != j and(shortestpath[i][k]+shortestpath[j][k]<shortestpath[i][j])){
- shortestpath[i][j]=shortestpath[i][k]+shortestpath[j][k];
- }
- }
- }
- }
- for(int j=0; j<q; j++){
- int a,b;
- cin>>a>>b;
- a--; b--;
- if(shortestpath[a][b] == 1e18) {
- cout << -1 << endl;
- }
- else cout<<shortestpath[a][b]<<" "<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement