(1)思路:
看来不仅要回使用spfa算法,其它算法也有各自的优点呀,
这道题因为要多次查询,所以每次用Ford的更新操作可以减少复杂度,
每次标记一个点x时,松弛一个点x,然后查询时再次询问。
(2)注意:
有重边,而且有环,要避免这种情况,每条边最小即可。
格式要注意;
(3)代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 330;
typedef long long LL;
const LL INF = 1e18+10;
LL mp[maxn][maxn];
int vis[maxn],n,m,q,bj[maxn];
void Ford(int x){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
if(mp[i][x]!=INF&&mp[x][j]!=INF&&mp[i][x]+mp[x][j]<mp[i][j])
mp[i][j] = mp[i][x]+mp[x][j];
}
}
int main(void){
int pt = 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++) mp[i][j] = INF;
mp[i][i] = 0;bj[i] = 0;
}
for(int i=1;i<=m;i++){
int x,y;LL z;
scanf("%d%d%lld",&x,&y,&z);
mp[x][y] = (mp[x][y]<z?mp[x][y]:z);
}
if(pt>1) printf("\n");
printf("Case %d:\n",pt++);
while(q--){
int op;scanf("%d",&op);
if(op==1){
int x,y;
scanf("%d%d",&x,&y);
if(bj[x]==0||bj[y]==0) printf("ERROR! At path %d to %d\n",x,y);
else{
/*if(x==y){
printf("0\n");
continue;
}*/
if(mp[x][y]==INF) printf("No such path\n");
else printf("%lld\n",mp[x][y]);
}
}else{
int x;scanf("%d",&x);
if(bj[x]==1){
printf("ERROR! At point %d\n",x);
}else{
Ford(x);
bj[x] = 1;
}
}
}
}
return 0;
}