Ingenious use of Floyd algorithm

Source: Internet
Author: User

The topic probably means: input n,m,n represents n points, then enter the distance between n points (N*n matrix), then M inquiry, input a,b,c if there is a C point between the shortest path between a and B, output yes, otherwise the output no

The game did not make it out, after the game sail brother a on-demand to know .... When I write, I directly use the Floy algorithm to find the distance and record the path. Then tle to die ... I was surprised that the data n,m are less than 100, how can tle ah ... The Pit Daddy Ah ... I have always suspected that it is not using other algorithms .... Sail brother said I recorded the path is only a path in the correct path, such as 1-3 Direct shortest path is 2, but may be added up to 2, but you recorded the path is 1-3 instead of the answer is not the right .... Suddenly understand,,. Why didn't I think of Qaq ... And tle is not necessarily time-out ah, there may be a variety of wonderful mistakes caused by ....

Correct solution:

How to determine whether the minimum path can have a C point, as long as the judge d[a][b] = = D[a][c] + d[c][b] equality exists, not equal to the existence of ...

 #include < iostream> #include <cstdio> #include <cstring>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" <<endl;        else cout<< "No" <<endl; }} return 0;} 


Ingenious use of Floyd algorithm

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.