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 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...
随机推荐
- python 本地文档查看
本地安装Python文档本地查看,在命令行中运行: python -m pydoc -p 1234 在浏览器中访问如下链接,就可以访问到本地文档: http://localhost:1234/ 本地文 ...
- bzoj 2843 极地旅行社(LCT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2843 [题意] 给定一个森林,要求提供连边,修改点值,查询路径和的操作. [思路] L ...
- Spark系列(六)Master注册机制和状态改变机制
各组件的注册流程如下图: 注册机制源码说明: 入口:org.apache.spark.deploy.master文件下的receiveWithLogging方法中的case RegisterAppli ...
- [转]Ubuntu下GitHub的使用
转自Pythoner 本文将对Ubuntu下Git的安装,以及如何连接GitHub进行讲解. 1.环境 OS: Ubuntu13.04 64bitsGit: 1.8.1.2 2.Git安装 执行如下命 ...
- 并行开发——Parallel的使用 -摘自网络
随着多核时代的到来,并行开发越来越展示出它的强大威力,像我们这样的码农再也不用过多的关注底层线程的实现和手工控制, 要了解并行开发,需要先了解下两个概念:“硬件线程”和“软件线程”. 1. 硬件线程 ...
- Mysql数据库插入的中文字段值显示问号的问题解决
最近我使用myeclipse连接mysql数据库查询表中的数据,表中字段值为中文的字段显示问号,查了很多资料将解决方法总结如下: 步骤一:修改mysql数据库的配置文件my.ini或者my-defau ...
- Hive自定义UDAF详解
遇到一个Hive需求:有A.B.C三列,按A列进行聚合,求出C列聚合后的最小值和最大值各自对应的B列值.这个需求用hql和内建函数也可完成,但是比较繁琐,会解析成几个MR进行执行,如果自定义UDAF便 ...
- 关于INTRAWEB ISAPI DLL发布
怎样将Stand Alone App变为ISAPI Dll? 一是将工程文件中的program改成library,二是将uses里的IWInitStandAlone改成IWInitISAPI,没有该文 ...
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- MySQL select into outfile用法
select into outfile用法 SELECT ... FROM TABLE_A INTO OUTFILE "/path/to/file" FIELDS TERMINAT ...