Clever Use of the Floyd algorithm

Source: Internet
Author: User

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"<
    
     

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.