PAT 1018
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.

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 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->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
又是属于比较烦的题目,直接dijkstra。但在比较路径大小的时候需要考虑:1)先比较到达时间,若时间小则路径小;2)若时间一样,则比较send的自行车数量,数量小的路径小;3)若send
的自行车数量也一样,则比较back的自行车数量,back的数量小则路径小。另外也要注意send与back的更新,后面station多出来的自行车是无法补给前面station的。
代码
#include <stdio.h>
#include <string.h> #define MAXV 501 int map[MAXV][MAXV];
int reminder[MAXV];
int send[MAXV];
int back[MAXV];
int times[MAXV];
int flag[MAXV];
int path[MAXV][MAXV]; int findMinTimesPoint(int);
int main()
{
int Cmax,N,Sp,M,s,e,i;
while(scanf("%d%d%d%d",&Cmax,&N,&Sp,&M) != EOF){
for(i=;i<=N;++i)
scanf("%d",&reminder[i]);
memset(map,,sizeof(map));
for(i=;i<M;++i){
scanf("%d%d",&s,&e);
scanf("%d",&map[s][e]);
map[e][s] = map[s][e];
}
memset(send,,sizeof(send));
memset(back,,sizeof(back));
memset(flag,,sizeof(flag));
memset(path,,sizeof(path));
s = ;
send[s] = ;
back[s] = ;
flag[s] = ;
path[][] = ;
path[][] = ;
for(i=;i<=N;++i){
times[i] = -;
if(map[s][i]){
times[i] = map[s][i];
send[i] = Cmax / - reminder[i];
if(send[i] < )
send[i] = ;
back[i] = reminder[i] - Cmax / ;
if(back[i] < )
back[i] = ;
path[i][] = ;
path[i][] = ;
path[i][] = i;
}
}
times[s] = ;
while(!flag[Sp]){
s = findMinTimesPoint(N);
flag[s] = ;
for(i=;i<=N;++i){
if(!flag[i] && map[s][i]){
if(times[i] == - || (times[i] > times[s] + map[s][i])){
times[i] = times[s] + map[s][i];
path[i][] = path[s][] + ;
int j;
for(j=;j<=path[s][];++j)
path[i][j] = path[s][j];
path[i][j] = i;
int t = Cmax / - reminder[i];
if(t == ){
send[i] = send[s];
back[i] = back[s];
}
else if(t > ){
if(t-back[s] >= ){
back[i] = ;
send[i] = send[s] + t - back[s];
}
else{
send[i] = send[s];
back[i] = back[s] - t;
}
}
else{
send[i] = send[s];
back[i] = back[s] - t;
}
}
else if(times[i] == times[s] + map[s][i]){
int t = Cmax / - reminder[i];
int tSend,tBack;
if(t==){
tSend = send[s];
tBack = back[s];
}
else if(t > ){
if(t-back[s] >= ){
tBack = ;
tSend = send[s] + t - back[s];
}
else{
tSend = send[s];
tBack = back[s] - t;
}
}
else{
tSend = send[s];
tBack = back[s] - t;
}
if(tSend < send[i] || (tSend == send[i] && tBack < back[i])){
send[i] = tSend;
back[i] = tBack;
path[i][] = path[s][] + ;
int j;
for(j=;j<=path[s][];++j)
path[i][j] = path[s][j];
path[i][j] = i;
}
}
}
}
}
printf("%d %d",send[Sp],path[Sp][]);
for(i = ;i<=path[Sp][];++i){
printf("->%d",path[Sp][i]);
}
printf(" %d\n",back[Sp]);
}
return ;
} int findMinTimesPoint(int n)
{
int i = ;
int minPointInd;
while(i<=n && (times[i] == - || flag[i]))
++i;
if(i > n)
return -;
minPointInd = i;
for(;i<=n;++i){
if(!flag[i] && times[i] != - && (times[i] < times[minPointInd] ||
(times[i] == times[minPointInd] && send[i]<send[minPointInd]) ||
(send[i]==send[minPointInd] && back[i]<back[minPointInd])))
minPointInd = i;
}
return minPointInd;
}
PAT 1018的更多相关文章
- PAT 1018 Public Bike Management[难]
链接:https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f来源:牛客网PAT 1018 Public ...
- PAT 1018 锤子剪刀布(20)
1018 锤子剪刀布 (20)(20 分) 大家应该都会玩"锤子剪刀布"的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方 ...
- PAT 1018 Public Bike Management(Dijkstra 最短路)
1018. Public Bike Management (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT 1018. 锤子剪刀布 (20)
现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算最大. 输入格式: 输入第1行给出正整数N(<=105),即双方交锋的次数.随后N行,每行给出一次交锋的信息,即 ...
- PAT 1018. Public Bike Management
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- PAT 1018 锤子剪刀布
https://pintia.cn/problem-sets/994805260223102976/problems/994805304020025344 大家应该都会玩“锤子剪刀布”的游戏:两人同时 ...
- PAT——1018. 锤子剪刀布
大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算最大. 输入格式: 输入第1行给出正整数N( ...
- 【图算法】Dijkstra算法及变形
图示: 模版: /* Dijkstra计算单源最短路径,并记录路径 m个点,n条边,每条边上的权值非负,求起点st到终点et的最短路径 input: n m st et 6 10 1 6 1 2 6 ...
- PAT甲级1018. Public Bike Management
PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...
随机推荐
- XCode修改工程名注意
以下文字转载过来,在使用的过程中遇到几个问题 1.需要在 Build phases 里面,检查下 Link Binary With Libraries 以及Compline Sources 2.Bul ...
- nginx配置:location配置方法及实例详解
今天深入研究了下nginx的location的用法,已经一些需要注意的细节,现在做一个归纳总结,以备后面查询. location匹配的是nginx的哪个变量? $request_uri locatio ...
- 1.2……初识Android开发
Android体系结构 Dalvik VM(Android下的java虚拟机)与传统的JVM的区别 传统JVM 基于堆栈的架构 编写.java文件--->编译为.class文件--->打包 ...
- wuzhicms访问统计实现方法
实现目标:程序实现了对整站页面pv的统计文件的位置:coreframe/app/content/pv.php代码预览: /** * 总站访问次数统计 */ defined('IN_WZ') or ex ...
- ***JAVA多线程的应用场景和应用目的举例
多线程使用的主要目的在于: 1.吞吐量:你做WEB,容器帮你做了多线程,但是他只能帮你做请求层面的.简单的说,可能就是一个请求一个线程.或多个请求一个线程.如果是单线程,那同时只能处理一个用户的请求. ...
- nodejs 5.2.0文档自翻译——Path模块
模块方法概览 Path path.basename(p[, ext]) path.delimiter path.dirname(p) path.extname(p) path.format(pathO ...
- quick-x在windows平台打包加密文件
D:\quick-cocos2d-x-2.2.1-rc\bin>compile_scripts -i ..\mygames\myth\scripts -o ..\mygames\myth\res ...
- 恒天云技术分享系列5 – 虚拟化平台性能对比(KVM & VMware)
恒天云技术分享系列:http://www.hengtianyun.com/download-show-id-14.html 概述 本性能测试报告将详细陈述各虚拟化平台基准性能测试的主要结论和详细结果. ...
- nodejs + socket.io + redis 新手上路
最近要更新网站架构了,决定转入 nodejs + socket.io + redis 方式. 战斗刚开始: 网上的文章太松散,我根据各个网友的分享进行整理 ,让大家可以方便上手. 进入node.js之 ...
- Spring入门(7)-自动检测Bean
Spring入门(7)-自动检测Bean 本文介绍如何自动检测Bean. 0. 目录 使用component-scan自动扫描 为自动检测标注Bean 1. 使用component-scan自动扫描 ...