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;
}

全部评论

相关推荐

努力学习的小绵羊:我反倒觉得这种挺好的,给不到我想要的就别浪费大家时间了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
正在热议
# 25届秋招总结 #
443000次浏览 4514人参与
# 春招别灰心,我们一人来一句鼓励 #
42077次浏览 535人参与
# 阿里云管培生offer #
120369次浏览 2220人参与
# 地方国企笔面经互助 #
7969次浏览 18人参与
# 同bg的你秋招战况如何? #
77008次浏览 566人参与
# 实习必须要去大厂吗? #
55793次浏览 961人参与
# 北方华创开奖 #
107458次浏览 600人参与
# 虾皮求职进展汇总 #
116056次浏览 886人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
11650次浏览 289人参与
# 实习,投递多份简历没人回复怎么办 #
2454867次浏览 34858人参与
# 提前批简历挂麻了怎么办 #
149922次浏览 1978人参与
# 在找工作求抱抱 #
906063次浏览 9421人参与
# 如果公司给你放一天假,你会怎么度过? #
4762次浏览 55人参与
# 你投递的公司有几家约面了? #
33209次浏览 188人参与
# 投递实习岗位前的准备 #
1196011次浏览 18550人参与
# 机械人春招想让哪家公司来捞你? #
157641次浏览 2267人参与
# 双非本科求职如何逆袭 #
662333次浏览 7397人参与
# 发工资后,你做的第一件事是什么 #
12793次浏览 62人参与
# 工作中,努力重要还是选择重要? #
35884次浏览 384人参与
# 简历中的项目经历要怎么写? #
86934次浏览 1516人参与
# 参加完秋招的机械人,还参加春招吗? #
20145次浏览 240人参与
# 我的上岸简历长这样 #
452046次浏览 8089人参与
牛客网
牛客企业服务