zoukankan      html  css  js  c++  java
  • PAT 甲级 1018 Public Bike Management (30 分)(dijstra+dfs,dfs记录路径,做了两天)

    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 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​​ (≤), always an even number, is the maximum capacity of each station; N (≤), 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​​ (,) 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.

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

    题意:

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

     

    思路:

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

    #include<iostream>
    #include<stack>
    using namespace std;
     
    int maze[10][10];//迷宫
    int vis[10][10];//记录迷宫中的某个位置是否访问过
    int n,m;
     
    int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};//四个方向
     
    struct point//位置
    {
        int x,y;
    } p;
     
    stack<point> path,temp;//记录路径,temp是一个临时变量,和path一起处理路径
     
    int count;//路径条数
     
    void dfs(int x,int y)//x,y:当前位置
    {
        if(x==n-1 && y==m-1)//成功---下面处理路径问题
        {
            cout << "******************路径"<< ++count <<  "******************" << endl;
            while(!path.empty())//将path里面的点取出来,放在temp里面
            {//path从栈顶-栈底的方向,路径是从终点-起点的顺序
                point p1 = path.top();
                path.pop();
                temp.push(p1);
            }
            while(!temp.empty())
            {//输出temp里面的路径,这样刚好是从起点到终点的顺序
                point p1 = temp.top();
                temp.pop();
                path.push(p1);//将路径放回path里面,因为后面还要回溯!!!
                cout << "(" << p1.x << "," << p1.y << ")" << endl;
            }
            return;
        }
     
        if(x<0 || x>=n || y<0 || y>=m)//越界
            return;
        
        //如果到了这一步,说明还没有成功,没有出界
        for(int i=0;i<4;i++)//从4个方向探测
        {
            int nx = x + dir[i][0];
            int ny = y + dir[i][1];//nx,ny:选择一个方向,前进一步之后,新的坐标
            if(0<=nx && nx<n && 0<=ny && ny<m && maze[nx][ny]==0 && vis[nx][ny]==0)
            {//条件:nx,ny没有出界,maze[nx][ny]=0这个点不是障碍可以走,vis[nx][ny]=0说明(nx,ny)没有访问过,可以访问
                
                vis[nx][ny]=1;//设为访问过
                p.x = nx;
                p.y = ny;
                path.push(p);//让当前点进栈
     
                dfs(nx,ny);//进一步探测
     
                vis[nx][ny]=0;//回溯
                path.pop();//由于是回溯,所以当前点属于退回去的点,需要出栈
            }
        }
    }
     
    int main()
    {
        count = 0;
        freopen("in.txt","r",stdin);//读取.cpp文件同目录下的名为in.txt的文件
     
        p.x = 0;
        p.y = 0;
        path.push(p);//起点先入栈
     
        cin >> n >> m;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                vis[i][j] = 0;
                cin >> maze[i][j];
            }
        }
        dfs(0,0);
     
        return 0;
    }
    View Code

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

    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

    AC代码:

      1 #include<bits/stdc++.h> 
      2 using namespace std;
      3 int INF = 9999999;
      4 int C,SP,N,M;
      5 int h[505];//拥有多少自行车 
      6 int e[505][505];
      7 int d[505];
      8 int v[505];
      9 int min_go=INF;//带去的越少越好 
     10 int min_back=INF;//带回来的越少越好 
     11 vector<int>p[505];//记录所有最短路径中各个点的前一个有哪些 
     12 stack<int>route;
     13 stack<int>final_route; 
     14 void clear(stack<int> &s){//清空栈 
     15     stack<int> empty;
     16     swap(empty,s); 
     17 }
     18 void dijstra()
     19 {
     20     d[0]=0;
     21     memset(v,0,sizeof(v));
     22     for(int i=1;i<=N;i++){
     23         d[i]=e[0][i];
     24         if(d[i]!=INF)
     25         {
     26             p[i].push_back(0);
     27         }
     28     }
     29     for(int i=1;i<=N-1;i++){
     30         int k=-1;
     31         int min=INF;
     32         for(int j=1;j<=N;j++){
     33             if(v[j]==0 && min>d[j]){
     34                 k=j;
     35                 min=d[j];
     36             }
     37         }
     38         if(k==-1){
     39             break;
     40         }
     41         v[k]=1;
     42         for(int j=1;j<=N;j++){
     43             if(v[j]==1){
     44                 continue;
     45             }
     46             if(d[j]>d[k]+e[k][j])
     47             {
     48                 p[j].clear();
     49                 p[j].push_back(k);
     50                 d[j]=d[k]+e[k][j]; 
     51             }
     52             else if(d[j]==d[k]+e[k][j]){
     53                 p[j].push_back(k);
     54             }    
     55         }
     56     }    
     57 }
     58 void dfs(int s,int go,int back){
     59     for(int i=0;i<p[s].size();i++){
     60         int x=p[s].at(i);
     61         if(x==0){
     62             if(min_go>go){//根据优先级更新! 
     63                 min_go=go;
     64                 min_back=back;
     65                 final_route=route;//更新最终路线 
     66                 final_route.push(x);
     67             }else if(min_go==go && min_back>back){
     68                 min_go=go;
     69                 min_back=back;
     70                 final_route=route;//更新最终路线 
     71                 final_route.push(x);
     72             }
     73         }else{         
     74             int g=go+C-h[x];
     75             int b=back;//后面多的不能补到前面! 
     76             if(g<0){//需要送去的为负数了 
     77                 b+=(-1*g);//反而还要带点回来
     78                 g=0;//那就不送了 
     79             }
     80             route.push(x);
     81             dfs(x,g,b);
     82             route.pop();
     83         }    
     84     }
     85 }
     86 int main(){
     87     cin>>C>>N>>SP>>M;    
     88     C/=2;
     89     for(int i=1;i<=N;i++){
     90         cin>>h[i];
     91         p[i].clear();
     92     }    
     93     for(int i=0;i<=N;i++)
     94     {
     95         for(int j=0;j<=N;j++)
     96         {
     97             if (i==j) e[i][j]=0;
     98             else e[i][j]=INF;
     99         }
    100     }
    101     for(int i=1;i<=M;i++){
    102         int u,v,t;
    103         cin>>u>>v>>t;
    104         e[u][v]=e[v][u]=t;
    105     }
    106     dijstra();
    107     
    108     int go=C-h[SP];
    109     int back=0;
    110     if(go<0){
    111          back=-1*go;
    112          go=0;
    113     }
    114     clear(route);
    115     clear(final_route);
    116     route.push(SP);
    117     
    118     dfs(SP,go,back);
    119     //输出 
    120     cout<<min_go<<" ";
    121     while(!final_route.empty()){
    122         cout<<final_route.top();
    123         final_route.pop();
    124         if(!final_route.empty()){
    125             cout<<"->";
    126         }        
    127     }
    128     cout<<" "<<min_back<<endl; 
    129      
    130     return 0;
    131 }
  • 相关阅读:
    Java Object类及其常用方法
    Java 抽象类和抽象方法
    Java 多态
    Java 继承
    Java Scanner类
    正则表达_1
    「暑期集训day14」掠影
    「暑期集训day13」苦闷
    「暑期集训day12」苦楚
    「暑期集训day11」旧殤
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/11324729.html
Copyright © 2011-2022 走看看