在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例C/C++语言基础 → 【Dijkstra+dfs】1018. Public Bike Management (30).cpp

【Dijkstra+dfs】1018. Public Bike Management (30).cpp

C/C++语言基础

下载此实例
  • 开发语言:C/C++
  • 实例大小:7.22KB
  • 下载次数:6
  • 浏览次数:56
  • 发布时间:2021-01-27
  • 实例类别:C/C++语言基础
  • 发 布 人:2065265632
  • 文件格式:.cpp
  • 所需积分:2
 相关标签: c c++ Dijkstra dfs sort

实例介绍

【实例简介】
Dijkstra算法   DFS

【实例截图】

1018. Public Bike Management (30) [Dijkstra算法   DFS]
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.
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 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 afer one space, output the path in the format: 0->S1->…->Sp. Finally afer another space, output
the number of bikes that we must take back to PBMC afer 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   DFS。如果只有Dijkstra是不可以的,因为minNeed和minBack在路径上的传递不满⾜最
优⼦结构,不是简单的相加的过程,只有在所有路径都确定了之后才能区选择最⼩的need和最⼩的back~
Dijkstra求最短路径,dfs求minNeed和minBack和path,dfs的时候模拟⼀遍需要调整的过程,求出最后
得到的need和back,与minNeed和minBack⽐较然后根据情况更新path,最后输出minNeed path 和
minBack,记得path是从最后⼀个结点⼀直到第⼀个结点的,所以要倒着输出

【核心代码】


void dfs(int v){
    temp_path.push_back(v);
    if(v==0){
        int need=0,back=0;
        for(int i=temp_path.size()-1;i>=0;i--){
            int id=temp_path[i];
            //第一件事:搞清楚总共要借(返)多少,多退少补
            if(weight[id]>0){
                back =weight[id];
            }else{
                //返多了,少返一些,给到还缺的站点
                if(back>(0-weight[id])){
                    back =weight[id];
                }
                //互相匀完还不够,从PBMC借
                else{
                    need =((0-weight[id])-back);
                    back=0;
                }
            }
        }
        //第二件事:找到need更小的路径,替换之
        if(need<min_need){
            min_need=need;
            min_back=back;
            path=temp_path;
        }
        //第三件事:若存在多条最小need路径,选择back数量最少的路径
        else if(need==min_need && back<min_back){
            min_back=back;
            path=temp_path;
        }
        temp_path.pop_back();
        return;
    }
    for(int i=0;i<pre[v].size();i ){
        dfs(pre[v][i]);
    }
    temp_path.pop_back();
}


标签: c c++ Dijkstra dfs sort

实例下载地址

【Dijkstra+dfs】1018. Public Bike Management (30).cpp

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警