-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathROHAAN.cpp
64 lines (48 loc) · 1.14 KB
/
ROHAAN.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// AC , ALGO : Graph Theory : Floyd–Warshall algorithm, All Pairs Shortest Path problem.
/* Some Helpful Links :
http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm
http://www.geeksforgeeks.org/dynamic-programming-set-16-floyd-warshall-algorithm/
*/
// For any clarifications, contact me at : osinha6792@gmail.com
#include <cstdio>
using namespace std ;
int main( )
{
int t , n , r , i , j , k , cs , d ;
for( scanf("%d",&t) , cs = 1 ; cs <= t ; cs++ )
{
scanf("%d",&n) ;
int dist[n+1][n+1] ;
for( i = 1 ; i <= n ; i++ )
{
for( j = 1 ; j <= n ; j++ )
scanf("%d",&dist[i][j]) ;
}
for( k = 1 ; k <= n ; k++ )
{
for( i = 1 ; i <= n ; i++ )
{
for( j = 1 ; j <= n ; j++ )
{
if( dist[i][k] + dist[k][j] < dist[i][j] )
dist[i][j] = dist[i][k] + dist[k][j] ;
}
}
}
/* for( i = 1 ; i <= n ; i++ )
{
for( j = 1 ; j <= n ; j++ )
printf("%d ",dist[i][j]) ;
printf("\n") ;
}
printf("\n\n") ; */
scanf("%d",&r) ;
for( d = 0 , k = 1 ; k <= r ; k++ )
{
scanf("%d %d",&i,&j) ;
d += dist[i][j] ;
}
printf("Case #%d: %d\n",cs,d) ;
}
return 0 ;
}