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.
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:
- 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. - 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
(≤100), always an even number, is the maximum capacity of each station; N (≤500), 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
(i=1,…,N) 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−>S
1
−>…−>S
p
. 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
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <cctype>
#include <string.h>
#include <cstdio>
using namespace std;
const int inf=99999999;
int cmax,n,sp,m;
int minNeed=inf,minBack=inf;
int e[510][510],dis[510],weight[510];
bool visit[510];
vector<int> pre[510];
vector<int> temppath,path;
void dfs(int v){
temppath.push_back(v);
if(v==0){
int need=0,back=0;
for(int i=temppath.size()-1;i>=0;i--){
int id=temppath[i];
if(weight[id]>0){
back+=weight[id];
}else{
if(back>abs(weight[id])){
back+=weight[id];
}else{
need+=abs(back+weight[id]);
back=0;
}
}
}
if(need<minNeed){
minNeed=need;
minBack=back;
path=temppath;
}else if(need==minNeed&&back<minBack){
minBack=back;
path=temppath;
}
temppath.pop_back();
return;
}
for(int i=0;i<pre[v].size();i++)
dfs(pre[v][i]);
temppath.pop_back();
}
int main(){
fill(e[0],e[0]+510*510,inf);
fill(dis,dis+510,inf);
cin>>cmax>>n>>sp>>m;
for(int i=1;i<=n;i++){
cin>>weight[i];
weight[i]-=cmax/2;
}
for(int i=0;i<m;i++){
int a,b,c;
cin>>a>>b>>c;
e[a][b]=e[b][a]=c;
}
dis[0]=0;
for(int i=0;i<=n;i++){
int u=-1,minn=inf;
for(int j=0;j<=n;j++){
if(visit[j]==false&&dis[j]<minn){
minn=dis[j];
u=j;
}
}
if(u==-1) break;
visit[u]=true;
for(int v=0;v<=n;v++){
if(visit[v]==false&&e[u][v]!=inf){
if(dis[u]+e[u][v]<dis[v]){
dis[v]=dis[u]+e[u][v];
pre[v].clear();
pre[v].push_back(u);
}else if(dis[u]+e[u][v]==dis[v]){
pre[v].push_back(u);
}
}
}
}
dfs(sp);
cout<<minNeed<<" 0";
for(int i=path.size()-2;i>=0;i--)
cout<<"->"<<path[i];
cout<<" "<<minBack;
return 0;
}
- sql server 触发器
- T-SQL 查询、修改数据表
- sql server 事务处理
- Android P专区免费开放 -- 同样的Android,不同的体验
- YJango:TensorFlow中层API Datasets+TFRecord的数据导入
- CentOS7上ElasticSearch安装填坑记
- ASP.NET Core 阶段性总结
- Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程案例演示
- C# 窗体常用API函数 应用程序窗体查找
- C#进阶系列——WebApi接口测试工具:WebApiTestClient
- 由Dapper QueryMultiple 返回数据的问题
- 【初探IONIC】不会Native可不可以开发APP?
- MVC视图展现模式之移动布局解析-续集
- 在 ASP.NET MVC 中使用异步控制器
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 枚举和List的几个共有方法
- AtomicInteger源码解析
- AtomicIntegerArray源码解析
- AtomicIntegerFieldUpdater源码解析
- “终于懂了” 系列:Android屏幕刷新机制—VSync、Choreographer 全面理解!
- 一些PHP选项参数相关的函数
- 【Flutter 专题】98 易忽略的【小而巧】的技术点汇总 (六)
- PHP脚本设置及获取进程名
- GlasgowSmile-v2通关笔记
- R语言实现输出文本的多样式
- 学习|Unity3d的导航实现循环线路移动
- 【redis】闲得无聊,来聊聊当下爆火的 redis集群,顺便搭一个玩玩呗
- 【redis入门】Centos下安装redis
- LeetCode精选好题(五)
- 【leetcode两题选手】MySQL类题目(一)