pta programming question 20 travel planning

For other pta data structure programming questions, see: pta

topic

This shortest path problem only requires the shortest path between two points, so in Dijikstra's algorithm, the loop can be jumped out after finding the shortest path of the target point.

  1 #include <iostream>
  2 using namespace std;
  3 
  4 struct Node
  5 {
  6     int length;
  7     int price;
  8 };
  9 
 10 int V, E;
 11 Node **G;
 12 
 13 void buildGraph();
 14 void deleteGraph();
 15 void dijkstra(int s, int d);
 16 int findMinDist(Node* dist, bool* collected);
 17 bool cmp(Node a, Node b);
 18 
 19 int main()
 20 {
 21     int S, D;
 22     cin >> V >> E >> S >> D;
 23     buildGraph();
 24     dijkstra(S, D);
 25     deleteGraph();
 26     return 0;
 27 }
 28 
 29 void buildGraph()
 30 {
 31     int a, b, l, p, i;
 32     G = new Node*[V];
 33     for (a = 0; a < V; a++)
 34     {
 35         G[a] = new Node[V];
 36         for (b = 0; b < V; b++)
 37         {
 38             G[a][b].length = INT_MAX;
 39             G[a][b].price = INT_MAX;
 40         }
 41     }
 42 
 43     for (i = 0; i < E; i++)
 44     {
 45         cin >> a >> b >> l >> p;
 46         G[a][b].length = l;
 47         G[a][b].price = p;
 48         G[b][a].length = l;
 49         G[b][a].price = p;
 50     }
 51 }
 52 
 53 void deleteGraph()
 54 {
 55     for (int i = 0; i < V; i++)
 56         delete G[i];
 57     delete G;
 58 }
 59 
 60 bool cmp(Node a, Node b)
 61 {
 62     if (a.length != b.length)
 63         return a.length < b.length;
 64     else
 65         return a.price < b.price;
 66 }
 67 
 68 int findMinDist(Node* dist, bool* collected)
 69 {
 70     int minV;
 71     Node minDist;
 72     minDist.length = INT_MAX;
 73     minDist.price = INT_MAX;
 74 
 75     for (int i = 0; i < V; i++)
 76     {
 77         if (!collected[i] && cmp(dist[i], minDist))
 78         {
 79             minDist = dist[i];
 80             minV = i;
 81         }
 82     }
 83 
 84     if (minDist.length == INT_MAX)
 85         return -1;
 86     else
 87         return minV;
 88 }
 89 
 90 void dijkstra(int s, int d)
 91 {
 92     int v, w;
 93     Node *dist, temp;
 94     bool *collected;
 95     dist = new Node[V];
 96     collected = new bool[V];
 97     for (v = 0; v < V; v++)
 98     {
 99         dist[v] = G[s][v];
100         collected[v] = false;
101     }
102 
103     dist[s].length = 0;
104     dist[s].price = 0;
105     collected[s] = true;
106 
107     while (true)
108     {
109         v = findMinDist(dist, collected);
110         if (v == d) break;
111         collected[v] = true;
112 
113         for (w = 0; w < V; w++)
114         {
115             if (!collected[w] && G[v][w].length < INT_MAX)
116             {
117                 temp.length = dist[v].length + G[v][w].length;
118                 temp.price = dist[v].price + G[v][w].price;
119                 if (cmp(temp, dist[w]))
120                     dist[w] = temp;
121             }
122         }
123     }
124 
125     cout << dist[d].length << " " << dist[d].price;
126     delete dist;
127     delete collected;
128 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325640983&siteId=291194637