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.


Figure 1

Figure 1 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 (<= 100), always an even number, is the maximum capacity of each station; N (<= 500), 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 (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 Tijtaken 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->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.

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)首先,题目的描述可能不是特别清楚,选择路径的要求是————最短路径->路径长度相同时选择携带自行车数量最少的->携带自行车数量也相同时选择剩余的自行车数量最少的。

(2)其次需要注意的一点是,在后续节点多出的自行车是不能用来补充之前的节点的。具体来说,例如在A节点需要携带5辆自行车出发,即使A节点之后的B节点剩余5辆自行车,还是得带5辆自行车出发。

(3)然后解决这个问题最重要的一点是想明白这道题没有重叠子结构。简单地说,就是某个节点的前驱节点的最优路径,不一定是这个节点的最优路径————具体来说:例如目标节点是B,B的前驱节点为A。有两条从初始节点通向A节点最后通向B节点的最短路径,分别是路径1,到达A节点时剩余5辆自行车;路径2,到达A节点时剩余4辆自行车。从A节点的角度出发,路径2优于路径1,因为剩余的自行车更少;但是如果B节点缺少5辆自行车,那么对于B节点来说,路径1不需要额外的自行车,路径2反倒需要额外的1辆自行车,路径1反而优于路径2了。因此解决这道问题,不可以在迪杰斯特拉算法过程中记录每个节点的最优路径,而是需要先记录所有最短路径,然后分别计算每条最短路径在末尾节点时需要携带和剩余的自行车数量,选出最优的路径。

(4)最后需要注意的一点是…………这道题是无向图…………当初我做的时候把它当成了有向图来做,犯了这么大一个错误,偏偏在pat上只有最后两个测试例不通过,搞得我纠结了很久很久。等到终于意识到之后简直气尿了

下面附上我的代码:

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;

const int maxInt=0x3FFFFFFF;
const int Vmax = 510;

vector<int> *PrePace = new vector<int> [Vmax+1];

int *PaceValue= new int [Vmax+1];//用于迪杰斯特拉算法中的数组,下同。
bool *StationFlag = new bool [Vmax+1]();

int *TakeBike = new int [Vmax+1]();//携带的自行车数量
int *ReturnBike = new int [Vmax+1]();//剩余的自行车数量
int *StationCap= new int [Vmax+1];//每个节点的自行车数量

vector<int> TempPace;//用于在CountBike函数中记录当前递归的路径
vector<int> FinPace;//记录当前最优路径

void CountBike(int BikeCap,int ProblemBike,int k)//用递归的方法对每条最短路径计算携带和剩余自行车数量
{
    if(k==0)//递归边界————到初始点时开始计算
    {
        int s,t;
        TempPace.push_back(k);
        for(int i=TempPace.size()-1;i>0;i--)
        {
            s=TempPace[i];
            t=TempPace[i-1];
            int ReturnTemp=0;
            int TakeTemp=0;
            if(StationCap[t]<BikeCap/2)
            {
                if(ReturnBike[s]>=BikeCap/2-StationCap[t])
                {
                    TakeTemp=TakeBike[s];
                    ReturnTemp=ReturnBike[s]-BikeCap/2+StationCap[t];
                }
                else
                {
                    TakeTemp=TakeBike[s]+BikeCap/2-StationCap[t]-ReturnBike[s];
                    ReturnTemp=0;
                }
            }
            else
            {
                TakeTemp=TakeBike[s];
                ReturnTemp=ReturnBike[s]+StationCap[t]-BikeCap/2;
            }
            if(t==ProblemBike && (TakeTemp<TakeBike[t] || (TakeTemp==TakeBike[t] && ReturnTemp<ReturnBike[t])))
            {
                ReturnBike[t]=ReturnTemp;
                TakeBike[t]=TakeTemp;
                FinPace=TempPace;
            }
            else if(t!=ProblemBike)
            {
                ReturnBike[t]=ReturnTemp;
                TakeBike[t]=TakeTemp;
            }
        }
        TempPace.pop_back();
    }
    else
    {
        TempPace.push_back(k);
        for(size_t p=0;p<PrePace[k].size();p++)
        {
            CountBike(BikeCap,ProblemBike,PrePace[k][p]);//递归
        }
        TempPace.pop_back();
    }
}

void Dijkstra(int BikeCap,int ProblemBike,int **vert,int vNum)//迪杰斯特拉算法
{

    for(int i =0;i<=vNum;i++)
        PaceValue[i]=maxInt;

    //初始结点初始化
    PaceValue[0]=0;

    for(int i=0;i<vNum;i++)
    {
        int s=-1,t,j,minValue=maxInt;


        for(j=0;j<=vNum;j++)
            if(PaceValue[j]<minValue && StationFlag[j]==false)
            {
                s=j;
                minValue=PaceValue[j];
            }
        if(s==-1) return;
        StationFlag[s]=true;


        for(t=0;t<=vNum;t++)
        {
            if(StationFlag[t]==false && vert[s][t]!=maxInt)
            {
                if(vert[s][t]+PaceValue[s]<PaceValue[t])//通过vector记录每条最短路径
                {
                    PaceValue[t]=vert[s][t]+PaceValue[s];
                    PrePace[t].clear();//最短路径更新时别忘记清除旧路径
                    PrePace[t].push_back(s);
                }
                else if(vert[s][t]+PaceValue[s]==PaceValue[t])
                {
                    PrePace[t].push_back(s);
                }
            }
        }
    }

}

void PrintPace()
{
    int i;
    for(i=FinPace.size()-1;i>0;i--)
        cout<<FinPace[i]<<"->";
    cout<<FinPace[i];
}

int main()
{

    int BikeCap,vNum,ProblemBike,eNum;

    scanf("%d %d %d %d",&BikeCap,&vNum,&ProblemBike,&eNum);
    int i,j;
    for(i =1;i<=vNum;i++)
        cin>>StationCap[i];
    int ** vert = new int * [Vmax+1]();
    for(i =0;i<=Vmax;i++)
    {
        vert[i]=new int [Vmax+1];
        for(j=0;j<=Vmax;j++)
            vert[i][j]=maxInt;
    }
    for(i = 0;i<eNum;i++)
    {
        int s,t,value;
        scanf("%d %d %d",&s,&t,&value);
        vert[s][t]=vert[t][s]=value;
    }

    Dijkstra(BikeCap,ProblemBike,vert,vNum);

    TakeBike[ProblemBike]=maxInt;
    ReturnBike[ProblemBike]=maxInt;
    CountBike(BikeCap,ProblemBike,ProblemBike);


    cout<<TakeBike[ProblemBike]<<' ';
    PrintPace();
    cout<<' '<<ReturnBike[ProblemBike];
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值