7-9 Tourism Planning (25 points) (Dijkstra Shortest Path algorithm)

Source: Internet
Author: User

With a road map of self-driving, you will know the length of the highway between cities and the tolls of the road to be charged. Now you need to write a program to help visitors find a shortest route between the origin and the destination. If you have several paths that are the shortest, you need to output the cheapest path.

Input format:

Input Description: The 1th line of the input data gives 4 positive integersNMSD, whereN2≤N≤500) is the number of cities, by the way assuming the city's number is 0~ (n? 1); m is the number of highways; S is the city number of the departure point; d is the city number of the destination. Subsequent m line, each line gives the information of a highway, respectively: City 1, City 2, highway length, charge amount, the middle with a space separate, The numbers are integers and do not exceed 500. The input guarantees the existence of the solution.

Output format:

In a row, the length of the output path and the total charge, the numbers are separated by a space, the output end can not have extra space.

Input Sample:
4 5 0 30 1 1 201 3 2 300 3 4 100 2 2 202 3 1 20
Sample output:
3 40

Problem Solving Ideas: 1, it is obvious that this is a graph problem, but it has two weights, so I used a ternary array to store the data

2, because it is single source shortest path, so use Dijkstra algorithm

3, the last note if the shortest distance is the same as to determine whether the cost is minimal

1#include <stdio.h>2#include <stdlib.h>3 4 #defineMaxvex 5055 #defineINFINITY 655356 7 voidcreategraph ();8 voidDijkstra (intv);9 Ten  One intg[maxvex][maxvex][2],nv,ne; A intKnow[maxvex];//know[]=1 indicates the shortest path is obtained - intDistance[maxvex];//the shortest distance to express the request - intPay[maxvex];//represents a minimum fee the //int P[maxvex]; //Subscript for storing shortest path -  - intMain () - { +     ints,d; -scanf"%d %d%d%d",&nv,&ne,&s,&d); + creategraph (); A Dijkstra (s); at     if(distance[d]<INFINITY) { -printf"%d%d", Distance[d],pay[d]); -     } -         -     return 0; - } in  - voidcreategraph () to { +     //representation of graphs with adjacency matrices -     inti,j; the     intV1,v2; *     intDn,f;//the DN represents the distance, and F represents the cost $ Panax Notoginseng      for(i=0; i<nv; i++) -     { the          for(j=0; j<nv; J + +) +         { Ag[i][j][0] = INFINITY;//Initialize theg[i][j][1] =INFINITY; +         } -     } $  $      for(i=0; i<ne; i++)//Note that this is a read-in Edge -     { -scanf"%d %d%d%d",&v1,&v2,&dn,&f); theg[v1][v2][0] = g[v2][v1][0]=DN; -g[v1][v2][1] = g[v2][v1][1]=F;Wuyi     } the } -  Wu voidDijkstra (intv) - { About     //the shortest distance from the V junction to each of the other nodes. $     inti,j,k; -     intMin,cost; -  -      for(i=0; i<nv; i++) A     { +Know[i] =0; theDistance[i] =g[v][i][0];//Add a distance to the node with the V point connected -Pay[i] =g[v][i][1]; $     } the  theKNOW[V] =1; theDISTANCE[V] =0;//V to v distance is 0 thePAY[V] =0; -  in      for(i=1; i<nv; i++) the     { themin = INFINITY;//the nearest distance from the V-junction is currently known . About          for(j=0; j<nv; J + +) the         { the             //find the nearest distance from the V-junction. the             if(!know[j] && distance[j]<min) +             { -K =J; theMin =Distance[j];BayiCost =Pay[j]; the             } the         } -  -KNOW[K] =1; the          for(j=0; j<nv; J + +) the         { the             //Fixed shortest path and distance the             if(!know[j] && (min+g[k][j][0]<Distance[j])) -             { theDISTANCE[J] = min+g[k][j][0]; thePAY[J] = Cost + g[k][j][1]; the 94             } the             Else if(!know[j] && (min+g[k][j][0]==DISTANCE[J]) && (cost+g[k][j][1] <Pay[j])) the             { the 98PAY[J] = Cost + g[k][j][1];  About             } -         }101 102     }103 104}

7-9 Tourism Planning (25 points) (Dijkstra Shortest Path 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.