PAT-A1018 Public Bike Management 题目内容及题解

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.

PAT-A1018 Public Bike Management 题目内容及题解_第1张图片

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 S​3​​, we have 2 different shortest paths:

  •     PBMC -> S​1​​ -> S​3​​. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S​1​​ and then take 5 bikes to S​3​​, so that both stations will be in perfect conditions.
  •     PBMC -> S​2​​ -> S​3​​. 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: C​max​​ (≤100), always an even number, is the maximum capacity of each station; N (≤500), the total number of stations; S​p​​, 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 C​i​​ (i=1,⋯,N) where each C​i​​ is the current number of bikes at S​i​​ respectively. Then M lines follow, each contains 3 numbers: S​i​​, S​j​​, and T​ij​​ which describe the time T​ij​​ taken to move betwen stations S​i​​ and S​j​​. 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−>S​1​​−>⋯−>S​p​​. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of S​p​​ 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

题目大意

题目给出每个站点的容纳量、站点总数和需要调节的站点编号,并给出各站点间的路径信息。在前往目的站点时总选择最短路径,并调节途中所有结点到完美状态,如果有多条最短路径则选择需要从中心携带最少自行车的路径。并将该路径输出。

解题思路

  1. 初始化邻接矩阵及相关数据;
  2. 用Dijkstra算法求得最短路径;
  3. 用DFS求得所有路径,并计算出每条路径需要携带的自行车信息,从中选取代价最小的路径(简化为打欠条的方式计算需要携带多少自行车);
  4. 按题目要求输出路径,并返回0值。

代码

#include

#define maxn 510
#define INF 100000000

int G[maxn][maxn];//邻接矩阵
int d[maxn];//距离数组
int vis[maxn];//访问数组 
int needs[maxn];//需求的数量
int C,N,S,M;//最大数量C,站点数N,目标站点S,路数M
int temp[maxn]={0},path[maxn]={0};//临时路径,最短路径 
int mindis,mintake=INF,minremain=INF;//最小距离
int pre[maxn][maxn];
int prenum[maxn];

void Init(){
    int i,j;
    int a,b,v;
    scanf("%d%d%d%d",&C,&N,&S,&M);
    C=C/2;
    for(i=1;i<=N;i++){
        scanf("%d",&needs[i]);
        needs[i]=C-needs[i];
    }//初始化needs数组
    for(i=0;i<=N;i++){
        d[i]=INF;
        vis[i]=0;
        for(j=0;j<=N;j++){
            G[i][j]=INF;
        }
        G[i][i]=0;
    }
    for(i=0;i=0;i--){
            v=temp[i];
            if(remain>=needs[v]){//剩余数量可以满足 
                remain=remain-needs[v];//更新剩余数量 
            }else{//不够数 
                take+=needs[v]-remain;//不够数,remain清零,剩余部分打白条 
                remain=0;
            }
        } 
        if(take=0;i--){
                path[j++]=temp[i];//将temp记录于path 
            }
        }else if(take==mintake&&remain=0;i--){
                path[j++]=temp[i];//将temp记录于path 
            }
        }
        return;
    }
    for(i=0;i%d",path[i]);
        if(path[i]==S){
            break;
        }
    }
    printf(" %d\n",minremain);
    return 0;
}

运行结果

PAT-A1018 Public Bike Management 题目内容及题解_第2张图片

 

你可能感兴趣的:(PAT甲级)