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 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,如果一个车站里面自行车的数量恰好为cmax / 2,那么称处于完美状态。如果一个车展容量是满的或者空的,控制中心(处于结点0处)就会携带或者从路上手机一定数量的自行车前往该车站,一路上会让所有的车展沿途都达到完美。现在给出cmax,车站的数量n,问题车站sp,m条边,还有距离,求最短路径。如果最短路径有多个,求能带的最少的自行车数目的那条。如果还是有很多条不同的路,那么就找一个从车站带回的自行车数目最少的。带回的时候是不调整的

思路:

首先用dijkstra算法求出PBMC(0点)到目标点的最短距离。然后深搜穷举,每次深搜找到一条最短路径后依次记录路径上的各点,然后扫描,如果有的station少于最大容量的一半,带出的车数就加到take中;如果多了,带回去的车数加到back中。注意,每一站多的车辆只能放到后面一站去,而不能补充前面车站中少的数目。

这个题目有两个比较坑的点:

1.仔细阅读题目,会发现其实有三个优先级判断点:题目正文中要求第一优先级为最短路径,第二优先级为派出自行车最少,而在结果输出中有第三优先级,即收回自行车最少。遗漏后两个优先级会导致有的点通不过;
2.在统计派出数和收回数时,要注意一个站点中多出来的自行车只能向后面的站点补充,而不能向前面的站点补充,这也是很容易忽视的问题。

我的样例:

10 2  2 2
2 10
0 1 1
1 2 1

3 0->1->2 5

10 3 3 3
11 0 10
0 1 1
1 2 1
2 3 1

0 0->1->2->3 6

10 4 4 5
6 7 5 0
0 1 1
0 2 1
1 3 1
2 3 1
3 4 1

3 0->2->3->4 0

10 4 4 4
6 0 11 0
0 1 1
1 2 1
2 3 1
3 4 1

4 0->1->2->3->4 1

10 4 4 8
0 11 3 0
0 1 2
0 2 3
0 3 2
1 2 1
2 3 1
1 4 2
2 4 1
3 4 2
0 0->2->4 1

10 6 6 8
8 9 4 6 7 2
0 1 1
0 2 1
1 3 1
2 3 1
3 4 1
3 5 2
4 6 1
5 6 1
0 0->1->3->4->6 0

case 7 过不了的代码:

#include<bits/stdc++.h>
using namespace std;
int INF = 9999999;
int C,SP,N,M;
int h[505];//拥有多少自行车
int e[505][505];
int d[505];
int v[505];
int min_go=INF;//带去的越少越好
int min_back=INF;//带回来的越少越好
vector<int>p[505];
int fo[505];
int f[505];
void dijstra()
{
    d[0]=0;
    memset(v,0,sizeof(v));
    for(int i=1;i<=N;i++){
        d[i]=e[0][i];
        if(d[i]!=INF)
        {
            p[i].push_back(0);
        }
    }
    for(int i=1;i<=N-1;i++){
        int k=-1;
        int min=INF;
        for(int j=1;j<=N;j++){
            if(v[j]==0 && min>d[j]){
                k=j;
                min=d[j];
            }
        }
        if(k==-1){
            break;
        }
        v[k]=1;
        for(int j=1;j<=N;j++){
            if(v[j]==1){
                continue;
            }
            if(d[j]>d[k]+e[k][j])
            {
                p[j].clear();
                p[j].push_back(k);
                d[j]=d[k]+e[k][j];
            }
            else if(d[j]==d[k]+e[k][j]){
                p[j].push_back(k);
            }
        }
    }
}
void dfs(int s,int go,int back){
    //cout<<"s:"<<s<<" go:"<<go<<" back:"<<back<<endl;
    for(int i=0;i<p[s].size();i++){
        //f=0;
        int x=p[s].at(i);
        //cout<<"fffffs:"<<s<<" x:"<<x<<endl;
        if(x==0){
            if(min_go>go){//根据优先级更新!
                min_go=go;
                min_back=back;
                //cout<<"clear"<<endl;
                memset(f,0,sizeof(f));
                f[s]=1;
                //cout<<s<<"为1"<<endl;
            }else if(min_go==go && min_back>back){
                min_go=go;
                min_back=back;
                //cout<<"clear"<<endl;
                memset(f,0,sizeof(f));
                f[s]=1;
                //cout<<s<<"为1"<<endl;
            }
        }else{
            int g=go+C-h[x];
            int b=back;//后面多的不能补到前面!
            if(g<0){//需要送去的为负数了
                b+=(-1*g);//反而还要带点回来
                g=0;//那就不送了
            }
            //cout<<"x:"<<x<<" g:"<<g<<" b:"<<b<<endl;
            dfs(x,g,b);
            //cout<<"s:"<<s<<" x: "<<x<<" f:"<<f[x]<<endl;
            if(f[x]){
                fo[s]=x;
                f[s]=1;
                //cout<<"s:"<<s<<" x: "<<x<<endl;
            }
        }
    }
}
int main(){
    cin>>C>>N>>SP>>M;
    C/=2;
    for(int i=1;i<=N;i++){
        cin>>h[i];
        p[i].clear();
        fo[i]=0;
    }
    for(int i=0;i<=N;i++)
    {
        for(int j=0;j<=N;j++)
        {
            if (i==j) e[i][j]=0;
            else e[i][j]=INF;
        }
    }
    for(int i=1;i<=M;i++){
        int u,v,t;
        cin>>u>>v>>t;
        e[u][v]=e[v][u]=t;
    }
    dijstra();
    int go=C-h[SP];
    int back=0;
    if(go<0){
         back=-1*go;
         go=0;
    }
    /*for(int i=0;i<p[SP].size();i++){
        cout<<p[SP].at(i)<<endl;
    }*/
    memset(f,0,sizeof(f));
    dfs(SP,go,back);
    stack<int>sta;
    while(!sta.empty()){
        sta.pop();
    }
    int x=SP;
    sta.push(x);
    while(fo[x]!=0){
        sta.push(fo[x]);
        x=fo[x];
    }
    sta.push(fo[x]);
    cout<<min_go<<" ";
    while(!sta.empty()){
        cout<<sta.top();
        sta.pop();
        if(!sta.empty()){
            cout<<"->";
        }
    }
    cout<<" "<<min_back<<endl; 

    return 0;
}

原文地址:https://www.cnblogs.com/caiyishuai/p/11324729.html

时间: 2024-11-07 21:15:49

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 Public Bike Management Center (PBMC) kee

1018 Public Bike Management (30)(30 分)

时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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 Pu

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

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

PAT (Advanced Level) 1018. Public Bike Management (30)

先找出可能在最短路上的边,图变成了一个DAG,然后在新图上DFS求答案就可以了. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; const int INF=0x7FFFFFFF; co

pat 1018 Public Bike Management

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

PTA (Advanced Level)1018 Public Bike Management

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 Manag

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甲级——1131 Subway Map (30 分)

可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30 分) In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing