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.
链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2155
解题思路:
开始忘了用多源最短路了,于是乎SPFA果断TL
用floyd最短路就好了,由于点很少,所以直接矩阵保存就好了。。
#include <iostream> #include <stdio.h> #include <queue> #include <algorithm> #include <string> #include <string.h> using namespace std; #define MAX 500 #define INF 0x3f3f3f3f int mmap[MAX][MAX]; int nodeMark[MAX]; int main (){ int u,v,w; int N,M,Q,cnt=1; while(~scanf("%d%d%d",&N,&M,&Q)&&!(N==0&&M==0&&Q==0)){ printf("Case %d:\n",cnt++); for(int i=0;i<N;i++) for(int j=0;j<N;j++) mmap[i][j]=( i==j ? 0 : INF); memset(nodeMark,0,sizeof(nodeMark)); for(int i=0;i<M;i++){ scanf("%d%d%d",&u,&v,&w); mmap[u][v]=min(w,mmap[u][v]); } int x,y,op; for(int i=0;i<Q;i++){ scanf("%d",&op); if(op){ scanf("%d%d",&x,&y); if(nodeMark[x]==0||nodeMark[y]==0){ printf("City %d or %d is not available.\n",x,y); } else{ if(mmap[x][y]==INF) printf("No such path.\n"); else printf("%d\n",mmap[x][y]); } } else{ scanf("%d",&x); if(nodeMark[x]) printf("City %d is already recaptured.\n",x); else { nodeMark[x]=1; for(int i=0;i<N;i++) for(int j=0;j<N;j++) mmap[i][j]=min(mmap[i][x]+mmap[x][j],mmap[i][j]); } } } printf("\n"); } return 0; }