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 withy 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.
2010年山东省第一届ACM大学生程序设计竞赛
#include <map> #include <queue> #include <cmath> #include <cstdio> #include <cstring> #include <stdlib.h> #include <iostream> #include <algorithm> #define maxn 100100 #define inf 0x3f3f3f using namespace std; bool isused[maxn]; int cost[500][500]; void Floyd(int n,int k){ int i,j; for(i=0;i<n;i++) for(j=0;j<n;j++) if(cost[i][j]>cost[i][k]+cost[k][j]) cost[i][j]=cost[i][k]+cost[k][j]; } void add(){ int u,v,w; scanf("%d%d%d",&u,&v,&w); if(w<cost[u][v])cost[u][v]=w; } int main(){ int n,m,q,nn,loop=1; while(~scanf("%d%d%d",&n,&m,&q)&&n+m+q){ for(int i=0;i<n;++i)for(int j=0;j<n;++j){ cost[i][j]=inf; if(i==j)cost[i][j]=0; } while(m--)add(); memset(isused,false,sizeof(isused)); printf("Case %d:\n",loop++); while(q--){ int u,v; scanf("%d",&u); if(u==0){ scanf("%d",&v); if(isused[v]) printf("City %d is already recaptured.\n",v); else { isused[v]=true; Floyd(n,v); } } else { scanf("%d%d",&u,&v); if(isused[u]&&isused[v]){ if(cost[u][v]==inf) printf("No such path.\n"); else printf("%d\n",cost[u][v]); } else printf("City %d or %d is not available.\n",u,v); } } printf("\n"); } return 0; } /* 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 */ /************************************** Problem id : SDUT OJ 2155 User name : acmer Result : Accepted Take Memory : 1188K Take Time : 140MS Submit Time : 2016-04-01 21:03:14 **************************************/