PAT_A1018#Public Bike Management

Source:

PAT A1018 Public Bike Management (30 分)

Description:

There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.

The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.

When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.

The above figure illustrates an example. The stations are represented by vertices and the roads correspond to the edges. The number on an edge is the time taken to reach one end station from another. The number written inside a vertex S is the current number of bikes stored at S. Given that the maximum capacity of each station is 10. To solve the problem at S3​​, we have 2 different shortest paths:

  • PBMC -> S1​​ -> S3​​. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S1​​ and then take 5 bikes to S3​​, so that both stations will be in perfect conditions.
  • PBMC -> S2​​ -> S3​​. This path requires the same time as path 1, but only 3 bikes sent from PBMC and hence is the one that will be chosen.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 numbers: Cmax​​ (≤), always an even number, is the maximum capacity of each station; N (≤), the total number of stations; Sp​​, the index of the problem station (the stations are numbered from 1 to N, and PBMC is represented by the vertex 0); and M, the number of roads. The second line contains N non-negative numbers Ci​​ (,) where each Ci​​ is the current number of bikes at Si​​ respectively. Then Mlines follow, each contains 3 numbers: Si​​, Sj​​, and Tij​​ which describe the time Tij​​ taken to move betwen stations Si​​ and Sj​​. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print your results in one line. First output the number of bikes that PBMC must send. Then after one space, output the path in the format: 0. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of Sp​​is adjusted to perfect.

Note that if such a path is not unique, output the one that requires minimum number of bikes that we must take back to PBMC. The judge's data guarantee that such a path is unique.

Sample Input:

10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1

Sample Output:

3 0->2->3 0

Keys:

Code:

  1 /*
  2 Data: 2019-04-20 19:10:26
  3 Problem: PAT_A1018#Public Bike Management
  4 AC: 34:36
  5 
  6 题目大意:
  7 站点最佳状态时,有一半的自行车;
  8 从起点选择最短路径终点,路径上的其他站点同样调整至最佳状态(补充/回收);
  9 多条最短路径时,选择需要携带且回收数量最少的最条路径
 10 输入:
 11 第一行给出,最大容量Cmax,结点数N,Sp终点(默认起点为0),路径数M
 12 第二行给出,各站点现有库存Ci
 13 输出;
 14 携带车辆数,路径,回收车辆数
 15 */
 16 
 17 #include<cstdio>
 18 #include<vector>
 19 #include<algorithm>
 20 using namespace std;
 21 const int M=550,INF=1e9;
 22 int grap[M][M],vis[M],d[M],c[M];
 23 int n,m,Cmax,st=0,dt,optSent=INF,optBring=INF;
 24 vector<int> temp,opt,pre[M];
 25 
 26 void Dijskra(int s)
 27 {
 28     fill(vis,vis+M,0);
 29     fill(d,d+M,INF);
 30     d[s]=0;
 31     for(int i=0; i<=n; i++)
 32     {
 33         int u=-1,Min=INF;
 34         for(int j=0; j<=n; j++)
 35         {
 36             if(vis[j]==0 && d[j]<Min)
 37             {
 38                 u=j;
 39                 Min=d[j];
 40             }
 41         }
 42         if(u==-1)   return;
 43         vis[u]=1;
 44         for(int v=0; v<=n; v++)
 45         {
 46             if(vis[v]==0 && grap[u][v]!=INF)
 47             {
 48                 if(d[u]+grap[u][v] < d[v])
 49                 {
 50                     d[v]=d[u]+grap[u][v];
 51                     pre[v].clear();
 52                     pre[v].push_back(u);
 53                 }
 54                 else if(d[u]+grap[u][v]==d[v])
 55                     pre[v].push_back(u);
 56             }
 57         }
 58     }
 59 }
 60 
 61 void DFS(int v)
 62 {
 63     if(v == st)
 64     {
 65         int sent=0,bring=0;
 66         for(int i=temp.size()-1; i>=0; i--)
 67         {
 68             int v = temp[i];
 69             if(bring+(c[v]-Cmax/2) > 0)
 70                 bring = bring + (c[v]-Cmax/2);
 71             else
 72             {
 73                 sent += (Cmax/2-bring-c[v]);
 74                 bring=0;
 75             }
 76         }
 77         if(sent < optSent)
 78         {
 79             optSent = sent;
 80             optBring = bring;
 81             opt = temp;
 82         }
 83         else if(sent==optSent && bring<optBring)
 84         {
 85             optBring = bring;
 86             opt = temp;
 87         }
 88         return;
 89     }
 90 
 91     temp.push_back(v);
 92     for(int i=0; i<pre[v].size(); i++)
 93         DFS(pre[v][i]);
 94     temp.pop_back();
 95 }
 96 
 97 int main()
 98 {
 99 #ifdef ONLINE_JUDGE
100 #else
101     freopen("Test.txt", "r", stdin);
102 #endif // ONLINE_JUDGE
103 
104     fill(grap[0],grap[0]+M*M,INF);
105     scanf("%d%d%d%d", &Cmax,&n,&dt,&m);
106     for(int i=1; i<=n; i++)
107         scanf("%d", &c[i]);
108     for(int i=0; i<m; i++)
109     {
110         int v1,v2;
111         scanf("%d%d",&v1,&v2);
112         scanf("%d", &grap[v1][v2]);
113         grap[v2][v1]=grap[v1][v2];
114     }
115     Dijskra(st);
116     DFS(dt);
117     printf("%d %d", optSent,st);
118     for(int i=opt.size()-1; i>=0; i--)
119         printf("->%d", opt[i]);
120     printf(" %d", optBring);
121 
122     return 0;
123 }

猜你喜欢

转载自www.cnblogs.com/blue-lin/p/11051669.html