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

  1. 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.
  2. 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?? (≤), always an even number, is the maximum capacity of each station; N (≤), 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?? (,) 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. 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

题目大意:给定一个无向图,每个车站的完美容纳量为 cmax/2;从控制中心PBMC出发去目标车站,顺便将经过的车站也调整为完美车站。边权为路途花费时间,输出最短路径;如果有多条最短路径,则输出从中心携带出的车辆最少的,如果仍有多条,则输出返回途中携带最少的。

思路分析:dijkstra + dfs, 回溯路径。dijkstra 寻找最短路径 dis 并 记录前缀 pre 数组。从目标车站 dfs 到 PBMC,记录最小的 minneed 和 minback 值。最后输出 path 的路径。

#include <bits/stdc++.h>
using namespace std;
const int inf = 99999999;
int G[510][510];
int dis[510], weight[510];
vector <int> pre[510], path, temppath;
int Cmax, N, Sp, M;
bool visit[510] = {false};
int minneed = inf, minback = inf;

void dfs(int v){
    temppath.push_back(v);
    if (v == 0){
        int neednum = 0, backnum = 0;
        for (int i = temppath.size() - 1; i >= 0 ; i--){
            int id  = temppath[i];
            if (weight[id] > 0){
                backnum += weight[id];
            }else {
                if (backnum > 0 - weight[id]){
                    backnum += weight[id];
                }else{
                    neednum += 0 - weight[id] - backnum;
                    backnum = 0;
                }
            }
        }

        if (neednum < minneed){
            minneed = neednum;
            minback = backnum;
            path = temppath;
        }else if (neednum == minneed && backnum < minback){
            minback = backnum;
            path = temppath;
        }
        temppath.pop_back();//回溯弹出
        return ;
    }
    for (int i = 0; i < pre[v].size(); i++){
        dfs(pre[v][i]);
    }
    temppath.pop_back();
}

int main(void){
    cin >> Cmax >> N >> Sp >> M;
    fill(G[0], G[0] + 510*510, inf);
    //fill(weight, weight + 510, inf);
    fill(dis, dis + 510, inf);
    for (int i = 1; i <= N; i++){
        scanf("%d", &weight[i]);
        weight[i] = weight[i] - Cmax / 2;
    }
    int tem_a, tem_b;
    for (int i = 0; i < M; i++){
        scanf("%d%d", &tem_a, &tem_b);
        scanf("%d", &G[tem_a][tem_b]);
        G[tem_b][tem_a] = G[tem_a][tem_b];
    }

    dis[0] = 0;
    for (int i = 0; i < N; i++){
        int u = -1, minn = inf;
        for (int j = 0; j < N; j++){
            if (visit[j] == false && minn > dis[u]){
                u = j;
                minn = dis[j];
            }

        }

        if (u == -1) break;
        visit[u] = true;

        for (int v = 0; v <= N; v++){
            if (visit[v] == false && G[u][v] != inf){
                if (dis[u] + G[u][v] < dis[v]){
                    dis[v] = dis[u] + G[u][v];
                    pre[v].clear();
                    pre[v].push_back(u);
                }else if(dis[u] + G[u][v] == dis[v]){
                    pre[v].push_back(u);
                }
            }
        }
    }

    dfs(Sp);

    printf("%d 0", minneed);
    for(int i = path.size() - 2; i >= 0; i--)
        printf("->%d", path[i]);
    printf(" %d", minback);

    return 0;
}

 

原文地址:https://www.cnblogs.com/yellowzunzhi/p/11094661.html

时间: 2024-11-11 16:20:19

PAT A1018.Public Bike Management的相关文章

pat 1018 Public Bike Management

题意有三:1.时间最短 2.送出车辆最少 3.回收车辆最少 陷阱有一:调整路径上站点的车辆数目时,不能把后面站点多出来的车辆返补回前面车辆数不够的站点.乍看之下这是符合逻辑的,因为在前面的站点的时候不能知道后面的站点是什么情况,所以按理应该逐个调整合理,后面的站点影响不到前面的调整.但是细想之后发现这其实是很死板的做法,现实当中设计这样一个管理系统肯定能够实时收集每个站点的自行车数,所以在出发前应该就能得出这条路径上总的自行车数目,继而就能得到最优的送出数.但四十是介样子素通不过滴. #incl

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) kee

A1018. Public Bike Management (30)

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) kee

Pat(Advanced Level)Practice--1018(Public Bike Management)

Pat1018代码 题目描述: 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 C

PAT Public Bike Management

Public Bike Management 比较复杂 甚入. 我的思路是直接用一个DFS递归回溯做了 关键是题意也比较模糊 注意点如下 1.去的路上可调整,回来不可调整 2.路径最短 3.满足2 且 送出去的车最少 4.满足3 且 拿回来的车最少 AC代码如下 1 #include <stdio.h> 2 #include <stdlib.h> 3 #define MAX 1000000 4 int C,N,Sp,M; 5 int g[501][501]; 6 int flag[

PAT 甲级 1018 Public Bike Management (30 分)(dijstra+dfs case 7 过不了求助!!!)

1018 Public Bike Management (30 分)   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 Pub

PAT.Public bike management(SPFA + DFS)

1018 Public Bike Management (30分) 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

PAT1018. 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) ke

1018. Public Bike Management (30)

比较复杂的dfs 注意算好到底需要send多少take back多少 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 an