fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int T,n,r,x,y;
  4. int a[1000][1000];
  5. int main()
  6. {
  7. cin>>T;
  8. for (int l=1;l<=T;l++){
  9. cin>>n;
  10.  
  11. for (int j=1; j<=n; j++) {
  12. for(int k = 1; k <= n; k++){
  13. int cost;
  14. cin >> cost;
  15. a[j][k] = cost;
  16. }
  17.  
  18. }
  19.  
  20. for (int i=1; i<=n; i++) {
  21. for (int j=1;j<=n;j++){
  22. for (int k=1;k<=n;k++){
  23. if (a[j][k]==0 && j!=k){
  24. a[j][k]=100000;
  25. }
  26. if (a[j][k]>(a[j][i]+a[i][k]) && a[j][i]!=100000 && a[i][k]!=100000){
  27. a[j][k]=a[j][i]+a[i][k];
  28. //a[k][j]=a[j][i]+a[i][k];
  29. }
  30. }
  31. }
  32. }
  33. cin >> r;
  34. int s=0;
  35. for (int i=1;i<=r;i++){
  36. cin>>x>>y;
  37. s+=a[x][y];
  38. }
  39. cout<<"Case #"<<l<<": "<<s<<'\n';
  40. }
  41. }
  42.  
Success #stdin #stdout 0s 5272KB
stdin
Standard input is empty
stdout
Standard output is empty