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 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...
随机推荐
- IOS Swizzle(hook)
/////////////////////////////////////////////////////////////////////////////////////////////////// ...
- 微信多媒体上传图片,创建卡券上传 LOGO
//*****************************************多媒体上传图片 begin******************************************** ...
- 试验笔记 - Eclipse的.class反编译插件
常用的反编译工具有: JAD Java Decompiler Download Mirror(?) http://varaneckas.com/jad/ JadClipse (较好) http://j ...
- 瞬间从IT屌丝变大神——分工安排
分工安排主要包含以下内容: 公共组件(包括common.css和common.js)一人维护,各子频道专人负责,每个频道正常情况下由一人负责,要详细写明注释,如多人合作,维护的人员注意添加注释信息,具 ...
- es 的集群状态
es的集群状态一共有三种 : green yellow red 状态是基于 碎片的 等级进行划分的 .
- Git Book 中文版 - Git的撤消操作 - 重置, 签出 和 撤消
Git Book 中文版 - Git的撤消操作 - 重置, 签出 和 撤消 Git的撤消操作 - 重置, 签出 和 撤消 Git提供了多种修复你开发过程中的错误的方法. 方法的选择取决于你的情况: 包 ...
- 【现代程序设计】【homework-07】
C++11 中值得关注的几大变化 1.Lambda 表达式 Lambda表达式来源于函数式编程,说白就了就是在使用的地方定义函数,有的语言叫“闭包”,如果 lambda 函数没有传回值(例如 void ...
- hive UDTF函数
之前说过HIVE,UDF(User-Defined-Function)函数的编写和使用,现在来看看UDTF的编写和使用. 1. UDTF介绍 UDTF(User-Defined Table-Gener ...
- CodeForces 689B Mike and Shortcuts (bfs or 最短路)
Mike and Shortcuts 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/F Description Recently ...
- hdu4614Vases and Flowers(线段树,段设置,更新时范围的右边值为变量)
Problem Description Alice is so popular that she can receive many flowers everyday. She has N vases ...