The shortest path.
Dijkstra, spfa, Floyd. The question is clear, and the safest way is to take it.
There is a small optimization, that is, the starting point of spfa calculation is not counted.
There is also an end point. We didn't judge it at first, and Wa got a hit.
# Include <cstdio> # include <cstring> # include <string> # include <queue> # include <algorithm> # include <map> # include <stack> # include <iostream> # include <list> # include <set> # include <cmath> # define INF 0x7fffffff # define EPS 1e-6 # define ll long longusing namespace STD; int n, m; struct edge {int V; double S ;}; vector <edge> G [1001]; double dis [1001] [1001]; bool SPF [1001]; void spfa (INT start) {queue <int> q; bool vis [1001]; MEMS Et (VIS, 0, sizeof (VIS); DIS [start] [start] = 1; vis [start] = 1; q. Push (start); While (! Q. empty () {int u = Q. front (); q. pop (); vis [u] = 0; For (Int J = 0; j <G [u]. size (); j ++) {int v = G [u] [J]. v; double S = G [u] [J]. s; If (DIS [start] [v] <dis [start] [u] * s) {dis [start] [v] = dis [start] [u] * s; If (! Vis [v]) {vis [v] = 1; q. push (v) ;}}}} int main () {While (scanf ("% d", & N )! = EOF) {for (INT I = 1; I <= N; I ++) {G [I]. clear (); SPF [I] = 0 ;}for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) {double S; scanf ("% lf", & S); if (I = J) continue; edge now; now. V = J, now. S = s; G [I]. push_back (now);} scanf ("% d", & M); memset (DIS, 0, sizeof (DIS); While (M --) {int start, thend; scanf ("% d", & START, & thend); If (! SPF [start]) {spfa (start); SPF [start] = 1;} If (DIS [start] [thend]! = 0 & thend> 0 & thend <= N) // judge the end printf ("%. 3f \ n ", DIS [start] [thend]); else puts (" What a pity! ");}}}
HDU 1596 find the safest road