zoukankan      html  css  js  c++  java
  • 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.

    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:

    1. 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.

    2. 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 M lines 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
    
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <vector>
     4 using namespace std;
     5 const int inf = 99999999;
     6 int cmax, n, sp, m;
     7 int minNeed = inf, minBack = inf;
     8 int e[510][510], dis[510], weight[510];
     9 bool visit[510];
    10 vector<int> pre[510], path, temppath;
    11 void dfs(int v) {
    12     temppath.push_back(v);
    13     if (v == 0) {
    14         int need = 0, back = 0;
    15         for (int i = temppath.size() - 1; i >= 0; i--) {
    16             int id = temppath[i];
    17             if (weight[id] > 0) {
    18                 back += weight[id];
    19             }
    20             else {
    21                 if (back > (0 - weight[id])) {
    22                     back += weight[id];
    23                 }
    24                 else {
    25                     need += ((0 - weight[id]) - back);
    26                     back = 0;
    27                 }
    28             }
    29         }
    30         if (need < minNeed) {
    31             minNeed = need;
    32             minBack = back;
    33             path = temppath;
    34         }
    35         else if (need == minNeed && back < minBack) {
    36             minBack = back;
    37             path = temppath;
    38         }
    39         temppath.pop_back();
    40         return;
    41     }
    42     for (int i = 0; i < pre[v].size(); i++)
    43         dfs(pre[v][i]);
    44     temppath.pop_back();
    45 }
    46 int main() {
    47     fill(e[0], e[0] + 510 * 510, inf);
    48     fill(dis, dis + 510, inf);
    49     scanf("%d%d%d%d", &cmax, &n, &sp, &m);
    50     for (int i = 1; i <= n; i++) {
    51         scanf("%d", &weight[i]);
    52         weight[i] = weight[i] - cmax / 2;
    53     }
    54     for (int i = 0; i < m; i++) {
    55         int a, b;
    56         scanf("%d%d", &a, &b);
    57         scanf("%d", &e[a][b]);
    58         e[b][a] = e[a][b];
    59     }
    60     dis[0] = 0;
    61     for (int i = 0; i <= n; i++) {
    62         int u = -1, minn = inf;
    63         for (int j = 0; j <= n; j++) {
    64             if (visit[j] == false && dis[j] < minn) {
    65                 u = j;
    66                 minn = dis[j];
    67             }
    68         }
    69         if (u == -1) break;
    70         visit[u] = true;
    71         for (int v = 0; v <= n; v++) {
    72             if (visit[v] == false && e[u][v] != inf) {
    73                 if (dis[v] > dis[u] + e[u][v]) {
    74                     dis[v] = dis[u] + e[u][v];
    75                     pre[v].clear();
    76                     pre[v].push_back(u);
    77                 }
    78                 else if (dis[v] == dis[u] + e[u][v]) {
    79                     pre[v].push_back(u);
    80                 }
    81             }
    82         }
    83     }
    84     dfs(sp);
    85     printf("%d 0", minNeed);
    86     for (int i = path.size() - 2; i >= 0; i--)
    87         printf("->%d", path[i]);
    88     printf(" %d", minBack);
    89     return 0;
    90 }
  • 相关阅读:
    JdbcTemplate
    C# 将内存中的datatable数据导出为Excel(方法一,以文件流方式导出)【转载】
    DEV GridControl小结【转载】
    DEV控件:gridControl常用属性设置2【转载】
    DevExpress控件学习总结【转载】
    DEV中的TreeList控件应用的一个小效果实现【转载】
    DEV控件:gridControl常用属性设置【转载】
    工具箱修复Dev控件显示【转载】
    C#如何实现记住密码,自动登录功能?【转载】
    将MenuStrip控件中的信息添加到TreeView控件中【转载】
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11235709.html
Copyright © 2011-2022 走看看