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 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...
随机推荐
- 【转】javascript-图片预加载技术
1,脚本代码: /** * 图片头数据加载就绪事件 - 更快获取图片尺寸 * @version 2011.05.27 * @author TangBin * @see http://www.plane ...
- 如何用chrome修改js代码,跳过网站等待时间
用chrome修改js代码 By Z.H. Fu 切问录 [maplewizard.github.io](http://maplewizard.github.io ) 网页中大部分的限制都是由js编写 ...
- CDH5.5.1版HBase安装使用LZO压缩
1.安装 RHEL/CentOS/Oracle 5 Navigate to this link and save the file in the /etc/yum.repos.d/ dire ...
- Android 添加系统服务
原创文章,转载请注明出处:http://blog.csdn.net/t5721654/article/details/7480696 Android系统本身提供了很多系统服务,如WindowManag ...
- 第二百九十七天 how can I 坚持
算是在家宅了一天吧,下午睡了会觉,晚上一起做了个饭,中午一起吃的炒菜和徐斌他同学. 还是那么冷啊. 整天都是东扯西扯的. 睡觉. 忘了件重要的事,就是今天第一次喝鸡尾酒,还有,常人之所以是常人,不是因 ...
- 北京Uber优步司机奖励政策(3月5日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 附加题-stack的理解
这次的附加题推荐的博客是http://www.ruanyifeng.com/blog/2013/11/stack.html阮一峰的,感觉讲的深入浅出,比较适合对计算机刚刚接触的人: 下面谈谈感想: 这 ...
- JAVA中的常见面试题1
1.线程同步的方法的使用. sleep():使一个正在运行的线程处于睡眠状态,是一个静态方法,调用此方法要捕捉InterruptedException异常. wait():使一个线程处于等待状态,并且 ...
- iOS中的谓词(NSPredicate)使用
http://www.cocoachina.com/ios/20160111/14926.html 首先,我们需要知道何谓谓词,让我们看看官方的解释: The NSPredicate class is ...
- PLSQL Developer 常用设置及快捷键
1.登录后自动选中My Objects(已验证可用) 默认情况下,PLSQL Developer登录后,Brower里会选择all Objects,如果你登录的用户是DBA, 要展开tables目录, ...