Tourism Planning (25)

Source: Internet
Author: User

At first I wrote this question with DFS = = result time overrun

For this shortest path problem, the higher efficiency is certainly the Floyd algorithm and the Dijkstra algorithm

Single source shortest path is best naturally with Dijkstra, but Floyd is easy to write, I wrote it.

#include <iostream>#defineINF 99999using namespacestd;intN, M, S, D;intmap[ -][ -];intprice[ -][ -];intMain () {/*the purity of the initialization! */Cin>> N >> m >> s >>D;  for(inti =0; I < -; i++){         for(intj =0; J < -; J + +) {Map[i][j]= Price[i][j] =INF; }    }     for(inti =0; I < m; i++){        intW, x, Y, Z; CIN>> x >> y >> z >>W; Map[x][y]= Map[y][x] =Z; Price[x][y]= Price[y][x] =W; }    //Floyd Algorithm     for(intK =0; K < n; k++){         for(inti =0; I < n; i++){             for(intj =0; J < N; J + +){                if(Map[i][j] > Map[i][k] +Map[k][j]) {Map[i][j]= Map[i][k] +Map[k][j]; PRICE[I][J]= Price[i][k] +Price[k][j]; }                Else if(Map[i][j] = = Map[i][k] +Map[k][j]) {                    if(Price[i][j] >price[i][k] +Price[k][j]) {Price[i][j]= Price[i][k] +Price[k][j]; } }}} cout<< Map[s][d] <<" "<<price[s][d];}

Tourism Planning (25)

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.