1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 const int qq=1000+10; 5 double dist[qq]; 6 double city[qq][qq]; 7 int vis[qq]; 8 int n,m; 9 int s,e; 10 void Dijkstra() 11 { 12 double MAX; 13 int u; 14 for(int i=1;i<=n;++i){ 15 vis[i]=0; 16 dist[i]=city[s][i]; 17 } 18 vis[s]=1; 19 for(int l=2;l<=n;++l){ 20 MAX=0;u=s; 21 for(int i=1;i<=n;++i) 22 if(!vis[i] && dist[i] > MAX) 23 MAX = dist[u=i]; 24 if(MAX==0) break; 25 if(u==e) break; 26 vis[u]=1; 27 for(int i=1;i<=n;++i) 28 if(!vis[i]){ 29 if(dist[i]<dist[u]*city[u][i]) 30 dist[i]=dist[u]*city[u][i]; 31 if(dist[i]<dist[u]*city[i][u]) 32 dist[i]=dist[u]*city[u][i]; 33 } 34 35 } 36 if(u!=e) printf("What a pity! "); 37 else printf("%.3lf ",dist[e]); 38 } 39 int main() 40 { 41 while(~scanf("%d",&n)){ 42 for(int j,i=1;i<=n;++i) 43 for(j=1;j<=n;++j) 44 scanf("%lf",&city[i][j]); 45 scanf("%d",&m); 46 for(int i=0;i<m;++i){ 47 scanf("%d %d",&s,&e); 48 Dijkstra(); 49 } 50 } 51 }
将原来的+换成*就行了、模板懂了这题就是水题了