PAT-A1018

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 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:

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

//1018 Public Bike Management
#include <iostream>
#include <vector>
#define inf 100000
using namespace std;
int C, N, S, M;
int m[500][500];
int res[500];
vector<int> v1;
vector<int> v2;
int m1 = inf;
int m2 = inf;
int judge = 0;
int half = 0;
void dfs(int pos, int vis[500][500], int l, int t, int c)
{
    if (pos == S)
    {
        int d = half*t-c;
        if (l < m1)
        {
            m1 = l;
            judge = d;
            m2 = abs(d);
            //复制路径
            v2.assign(v1.begin(),v1.end());
        }
        else if (l == m1)
        {
            m1 = l;
            int k = abs(d);
            if (k < m2)
            {
                m2 = k;
                judge = d;
                //复制路径
                v2.assign(v1.begin(),v1.end());
            }
        }
        return;
    }
    for (int k = 1; k <= N; ++k)
    {
        if (vis[pos][k] == 0 && m[pos][k] != inf)
        {
            vis[pos][k] = 1;
            v1.push_back(k);
            dfs(k, vis, l + m[pos][k], t+1, c + res[k]);
            v1.pop_back();
        }
    }
}

void outTrace(){
    int s = v2.size();
    for(int k=0;k<s-1;++k){
        cout << v2[k] << "->";
    }
    cout << v2[s-1] << " ";
}

int main(int argc, char const *argv[])
{
    cin >> C >> N >> S >> M;
    int vis[500][500];
    int t = 0;
    int l = 0;
    int c = 0;
    half = C / 2;
    fill(m[0], m[0] + 500 * 500, inf);
    fill(vis[0],vis[0]+500*500,0);
    int s1, s2;
    for (int i = 1; i <= N; ++i)
    {
        cin >> res[i];
    }
    for (int i = 0; i < M; ++i)
    {
        cin >> s1 >> s2;
        cin >> m[s1][s2];
        m[s2][s1] = m[s1][s2];
    }
    v1.push_back(0);
    dfs(0, vis, l, t, c);

    //输出最后结果
    if(judge>0){
        cout << judge << " ";
        outTrace();
        cout  <<  0;
    }else{
        cout << 0 << " ";
        outTrace();
        cout << -judge;
    }
   
    return 0;
}

全部评论

相关推荐

爱看电影的杨桃allin春招:我感觉你在炫耀
点赞 评论 收藏
分享
已老实求offer😫:有点像徐坤(没有冒犯的意思哈)
点赞 评论 收藏
分享
评论
点赞
收藏
分享
正在热议
# 25届秋招总结 #
440928次浏览 4493人参与
# 春招别灰心,我们一人来一句鼓励 #
41537次浏览 524人参与
# 阿里云管培生offer #
119934次浏览 2219人参与
# 地方国企笔面经互助 #
7933次浏览 18人参与
# 同bg的你秋招战况如何? #
75751次浏览 552人参与
# 虾皮求职进展汇总 #
114497次浏览 885人参与
# 北方华创开奖 #
107331次浏览 599人参与
# 实习,投递多份简历没人回复怎么办 #
2454159次浏览 34849人参与
# 实习必须要去大厂吗? #
55696次浏览 960人参与
# 提前批简历挂麻了怎么办 #
149839次浏览 1977人参与
# 投递实习岗位前的准备 #
1195754次浏览 18547人参与
# 你投递的公司有几家约面了? #
33181次浏览 188人参与
# 双非本科求职如何逆袭 #
661963次浏览 7394人参与
# 如果公司给你放一天假,你会怎么度过? #
4734次浏览 55人参与
# 机械人春招想让哪家公司来捞你? #
157606次浏览 2267人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
11402次浏览 275人参与
# 发工资后,你做的第一件事是什么 #
12447次浏览 61人参与
# 工作中,努力重要还是选择重要? #
35638次浏览 384人参与
# 参加完秋招的机械人,还参加春招吗? #
20093次浏览 240人参与
# 我的上岸简历长这样 #
451937次浏览 8088人参与
# 实习想申请秋招offer,能不能argue薪资 #
39248次浏览 314人参与
# 非技术岗是怎么找实习的 #
155855次浏览 2120人参与
牛客网
牛客企业服务