【PAT】A1018 Public Bike Management

[Thinking]

Subject words

figure

n. Digital

. V think, finds; computing; the important part is ......

The stations are represented by vertices and the roads correspond to the edges.

Vertices represent the station, represents a side road.

correspond to the equivalent of

capacity

. N ability; Capacity

 

 

 

 

 

 

 

Subject to the effect: given the need to adjust the station number, starting from 0, adjust the way the way the way the station, so that the number of vehicles per station is half the Cmax, much less padded away. Selected from the shortest path, the shortest is selected from the same minimum band from 0 carriageway, if the same further is selected minimal back 0 carriageway.

Number of the vehicle to bring the output of the path, the number of the back of the vehicle (directly back to the return, no adjustment).

Methods: Dijkstra + DFS. First with Dijkstra algorithm to calculate the shortest path, consider only the shortest, the precursor node establishes save vector path. DFS then traverse each path, after obtaining a path (i.e., traverse to the start node 0) is calculated to bring the vehicle back to determine the best scheme.

Calculating brought, back number of a vehicle: For each site difference, consider the results of a site before the vehicle passed down their number and the number of vehicles trans bike [i] and added cmax / 2, the two cases - one, the difference is negative, i.e. the car is not enough, at from 0 with a car, so the value of the absolute value of the difference bring increases, the number of transmitted down to the vehicle is set to 0 trans; Second, the non-negative difference shows enough car, the car is assigned to multiple trans passed to the next station, the bring value remains. Trans bring the beginning and a value of 0, a traversing node is calculated starting from the next node (i.e., node number 0 to the next node). Eventually bring the number from 0 to carry vehicles, namely the number of trans back of the vehicle. Note related to how much the car from 0 band, only with the current through the station concerned, that is behind the site regardless of the car more, in front of the car is not enough, we must walk from point 0 with because not look back, carrying the number of vehicles ( with the advance path is varies). The DFS is pushed forward from end to end of the starting point, you must request the full path to be counted out. Finally, the output value of the variable-length array backwards, namely the path.

[Tips] want to skillfully use DFS algorithm Dijkstra + for the best path!

[AC] Code

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<iostream>
  3 #include<vector>
  4 using namespace std;
  5 #define N 502
  6 #define INF 100000000
  7 int cmax, stations, goal, roads;
  8 int bike[N];
  9 int length[N][N] = { {0} };
 10 bool vis[N] = {};
 11 int d[N];
 12 vector<int>pre[N];
 13 vector<int> Path;
 14  int minbring = INF, minback = INF;
 15 Vector < int > bestpath;
 16  void the DFS ( int V)
 . 17  {
 18 is      IF (V == 0 ) // boundary - starting node. 
19      {    
 20          // number of second and third scale value calculation, i.e. brought, back to the vehicle. 
21 is          path.push_back (V);
 22 is          int the bring = 0 ;
 23 is          int Trans = 0 ;
 24          for ( int i = path.size() - 2; i >= 0; i--)
 25         {
 26             int  u = path[i];
 27             if (bike[u] + trans >= cmax / 2)
 28             {
 29                 trans += bike[u] - cmax / 2;
 30             }
 31             else
 32             {
 33                 bring += cmax / 2 - bike[u] - trans;
 34                 trans = 0;
 35             }
 36         }
 37         //更新最优值。
 38         if (bring < minbring)
 39         {
 40             bestpath = path;
 41             minbring = bring;
 42             minback = trans;
 43         }
 44         else if (bring == minbring)
 45         {
 46             if (trans < minback)
 47             {
 48                 bestpath = path;
 49                 minback = trans;
 50             }
 51         }
 52         path.pop_back();
 53         return;
 54     }
 55     int i;
 56     path.push_back(v);
 57     for (i = 0; i < pre[v].size(); i++)
 58     {
 59         DFS(pre[v][i]);
 60     }
 61     path.erase(path.end() - 1);//可以写成path.pop_back();
 62 }
 63 void Dijkstra(int s)
 64 {
65      int I, J;
 66      Fill (D, D + N, INF);
 67      D [s] = 0 ;
 68      for (I = 0 ; I <= Stations; I ++ )
 69      {
 70          // find not s concentrated d minimum 
71 is          int min = INF, U = - . 1 ;
 72          for (J = 0 ; J <= Stations; J ++ )
 73 is          {
 74              IF (min> d [J] && VIS [J] == 0 )
 75              {
 76                  = min D [J];
77                  u = J;
 78              }
 79          }
 80          IF (u == - . 1 ) return ;
 81          VIS [u] = to true ;
 82          // for u by s energy to a point v, update path 
83          for (J = 0 ; J <= Stations; J ++ )
 84          {
 85              IF (length [U] [J] && VIS [J] == 0 )
 86              {
 87                  IF (D [U] + length [U] [J] < D [J] )    
 88                  {
 89                     d[j] = d[u] + length[u][j];
 90                     pre[j].clear();
 91                     pre[j].push_back(u);
 92                 }
 93                 else if (d[u] + length[u][j] == d[j])
 94                 {
 95                     pre[j].push_back(u);
 96                 }
 97             }
 98         }
 99 
100     }
101 }
102 
103 int main()
104 {
105     cin >> cmax >> stations >> goal >> roads;
106     int i;
107     for (i = 1; i <= stations; i++)
108         cin >> bike[i];
109     for (i = 0; i < roads; i++)
110     {
111         int u, v;
112         cin >> u >> v;
113         cin >> length[u][v];
114         length[v][u] = length[u][v];
115     }
116     Dijkstra(0);
117     DFS(goal);
118     cout << minbring << " 0";
119     for (i = bestpath.size() - 2; i >= 0; i--)
120     {
121         cout << "->" << bestpath[i];
122     }
123     cout << " " << minback;
124     return 0;
125 }

 

Guess you like

Origin www.cnblogs.com/yue36/p/12316123.html
Recommended