PAT 甲级 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.

输入

Each input file contains one test case. For each case, the first line contains 4 numbers: C m a x C_{max} Cmax(≤100), always an even number, is the maximum capacity of each station; N (≤500), the total number of stations; S p S_p 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(i=1,⋯,N) 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.

输出

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−>S1 −>⋯−>Sp . 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.

思路

求最短路径问题,给定起点和终点,深搜就行。

代码

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<vector>
using namespace std;
vector<int> temp;
vector<int> destination;
int graph[501][501];
int station[501];
int C, N, s, M;
int require = 0, sent = 0;
int minn;
int pan[501] = { 0 };
void change()
{
    destination.clear();
    for (int i = 0; i < temp.size(); i++)
    {
        destination.push_back(temp[i]);
    }
    //temp.clear();
    //temp.push_back(0);
}
void dfs(int cur, int re, int se, int length)
{
    if (cur == s)
    {
        if (length < minn)
        {
            minn = length;
            sent = se;
            require = re;
            change();
        }
        else if (length == minn)
        {
            //int bike = re - se;
            if (re < require)
            {
                change();
                require = re;
                sent = se;
            }
            else if (re == require && se < sent)
            {
                change();
                require = re;
                sent = se;
            }
        }
        return;
    }
    if (length > minn)
    {
        return;
    }
    for (int i = 0; i <= N; i++)
    {
        if (graph[cur][i] != -1 && pan[i] == 0)
        {
            pan[i] = 1;
            temp.push_back(i);
            int bike = C / 2 - station[i];
            int next_re = re, next_se = se;
            if (bike > 0)
            {
                int ju = next_se - bike;
                if (ju > 0)
                    dfs(i, re, ju, length + graph[cur][i]);
                else
                    dfs(i, re - ju, 0, length + graph[cur][i]);
            }
            else
            {
                dfs(i, re, se - bike, length + graph[cur][i]);
            }

            pan[i] = 0;
            temp.pop_back();
        }
    }
}
int main()
{
    for (int i = 0; i <= 500; i++)
    {
        for (int j = 0; j <= 500; j++)
            graph[i][j] = -1;
    }
    scanf("%d%d%d%d", &C, &N, &s, &M);
    for (int i = 1; i <= N; i++)
    {
        scanf("%d", &station[i]);
    }
    for (int i = 0; i < M; i++)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        scanf("%d", &graph[a][b]);
        graph[b][a] = graph[a][b];
        minn += graph[a][b];
    }
    temp.push_back(0);
    pan[0] = 1;
    dfs(0, 0, 0, 0);
    printf("%d", require);
    for (int i = 0; i < destination.size(); i++)
    {
        if (i == 0)
            printf(" ");
        else
            printf("->");
        printf("%d", destination[i]);
    }
    printf(" %d\n", sent);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值