Clever Use of the Floyd algorithm
Enter n, m, and n to represent n points, then enter the distance between n points (n * n matrix), then ask m, enter, b, c if the shortest path between a and B contains a cpoint, Yes is output; otherwise, No is output.
I didn't make it during the competition. After the game, I knew it as soon as I got an on-demand video .... I used the floy algorithm to calculate the distance and record the path .. Then TLE is dead... I am surprised that data n and m are all smaller than 100. How can we use it... Ah... I have been wondering if other algorithms are used ..... Fan ge said that the recorded path is only one path in the correct path. For example, the direct Shortest Path of 1-3 is 2, but the 1-2-3 may also be 2, however, if the recorded path is 1-3 instead of 1-2-3, the answer is incorrect .... Suddenly understood ,,.. Why did I not think of QAQ... There is also TLE, which may be caused by some strange errors, not necessarily timeout ....
Correct Solution:
How can we determine whether a cpoint exists in the minimum path? If d [a] [B] = d [a] [c] + d [c] [B] is equal, does not exist...
#include
#include
#include
using namespace std;int d[150][150];int w[150][150];const int INF = 127;int main(){ int n,m; #ifdef xxz freopen("in.txt","r",stdin); #endif // xxz while(cin>>n>>m) { memset(d,INF,sizeof(d)); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) cin>>d[i][j]; for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); while(m--) { int start,end,mid; cin>>start>>end>>mid; if(d[start][mid] + d[mid][end] == d[start][end]) cout<<"Yes"<