The input consists of several test cases.
The first line of input in each test case contains three integers N (0<N≤300), M (0<M≤100000) and Q (0<Q≤100000), which represents the number of cities, the numbers of inter-city roads and the number of operations.
Each of the next M lines contains three integer x, y and z, represents there is an inter-city road starts from x, end up with y and the length is z. You can assume that 0<z≤10000.
Each of the next Q lines contains the operations with the following format:
a) 0 x – means city x has just been recaptured.
b) 1 x y – means asking the shortest path from x to y only passing the recaptured cities.
The last case is followed by a line containing three zeros.
3 3 6 0 1 1 1 2 1 0 2 3 1 0 2 0 0 0 2 1 0 2 1 2 0 0 2 0 0 0
Case 1: City 0 or 2 is not available. 3 No such path. City 2 is already recaptured.
#include <stdio.h> #include <string.h> #define maxint 99999999 int main() { int n , m, q , i , j , k = 1 ; int a[303][303] , b[303][303] ; int flag[303] ; while(scanf("%d %d %d", &n, &m, &q)!=EOF) { if(n == 0 && m == 0 && q == 0) break; memset(flag,0,sizeof(flag)); for(i = 0 ; i < n ; i++) for(j = 0 ; j < n ; j++) { if(i == j ) a[i][j] = 0 ; else a[i][j] = maxint ; b[i][j] = a[i][j] ; } int u ,v , w ; for(i = 0 ; i < m ; i++) { scanf("%d %d %d", &u, &v, &w); if(w < a[u][v]) a[u][v]= w ; } int s , x , y ; printf("Case %d:\n", k++); while(q--) { scanf("%d", &s); if(s) { scanf("%d %d", &x, &y); if(!flag[x] ||!flag[y]) printf("City %d or %d is not available.\n", x , y); else if(b[x][y] < maxint) printf("%d\n",b[x][y]); else printf("No such path.\n"); } else { scanf("%d", &x); if(flag[x]) { printf("City %d is already recaptured.\n", x); continue ; } flag[x] = 1 ; for(i = 0 ; i < n ; i++) { if(flag[i] == 0) continue ; if(a[x][i] < b[x][i]) b[x][i] = a[x][i] ; for(j = 0 ; j < n ; j++) { if(flag[j] == 0) continue ; if( b[x][j] > b[x][i] + b[i][j] ) b[x][j] = b[x][i] + b[i][j] ; } } for(i = 0 ; i < n ; i++) { if(flag[i] == 0) continue ; if(a[i][x] < b[i][x]) b[i][x] = a[i][x] ; for(j = 0 ; j < n ; j++) { if(flag[j] == 0) continue ; if(b[j][x] > b[j][i] + b[i][x] ) b[j][x] = b[j][i] + b[i][x] ; } } for(i = 0 ; i < n ; i++) for(j = 0 ; j < n ; j ++) if(b[i][j] > b[i][x] + b[x][j]) b[i][j] = b[i][x] + b[x][j] ; } } printf("\n"); } return 0; }