fork download
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. const int oo = 1e9;
  6.  
  7. int a[1000][1000];
  8. int d[1000][1000];
  9. int n,m,q, u,v,w;
  10. int th[1000][1000];
  11.  
  12. void khoitao(){
  13. for(int i=1;i<=n;i++)
  14. for(int j=1;j<=n;j++) {
  15. a[i][j] = oo;
  16. d[i][j] = oo;
  17. th[i][j] = oo;
  18. }
  19. }
  20.  
  21. main(){
  22. cin >> n >> m >> q;
  23. khoitao();
  24. for (int i = 1; i <= m; i++) {
  25. cin >> u >> v >> w;
  26. a[u][v] = min(w, a[u][v]);
  27. a[v][u] = a[u][v];
  28. th[u][v] = u;
  29. th[v][u] = v;
  30. }
  31.  
  32. for (int k = 1; k <= n; k++)
  33. for (int i = 1; i <= n; i++)
  34. for (int j = 1; j <= n; j++) {
  35. if (d[i][k] + d[k][j] < d[i][j]) {
  36. d[i][j] = d[i][k] + d[k][j];
  37. th[i][j] = th[i][k];
  38. cout << " " << d[i][j] << endl;
  39. }
  40. }
  41.  
  42. for (int i = 1; i <= q; i++) {
  43. cin >> u >> v;
  44. cout << d[u][v] << endl;
  45. }
  46. }
  47.  
Success #stdin #stdout 0.01s 7704KB
stdin
8 15 3
1 2 3 
1 5 2
1 7 15
1 3 6
1 6 30
2 3 2
2 4 7
3 4 2
4 5 9
4 7 1
4 6 9
4 8 3
5 7 12
7 6 6 
6 8 5


1 8
1 5
1 2
stdout
1000000000
1000000000
1000000000