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的更多相关文章

  1. PAT 1018 Public Bike Management[难]

    链接:https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f来源:牛客网PAT 1018  Public ...

  2. PAT 1018 锤子剪刀布(20)

    1018 锤子剪刀布 (20)(20 分) 大家应该都会玩"锤子剪刀布"的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方 ...

  3. PAT 1018 Public Bike Management(Dijkstra 最短路)

    1018. Public Bike Management (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  4. PAT 1018. 锤子剪刀布 (20)

    现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算最大. 输入格式: 输入第1行给出正整数N(<=105),即双方交锋的次数.随后N行,每行给出一次交锋的信息,即 ...

  5. PAT 1018. Public Bike Management

    There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...

  6. PAT 1018 锤子剪刀布

    https://pintia.cn/problem-sets/994805260223102976/problems/994805304020025344 大家应该都会玩“锤子剪刀布”的游戏:两人同时 ...

  7. PAT——1018. 锤子剪刀布

    大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算最大. 输入格式: 输入第1行给出正整数N( ...

  8. 【图算法】Dijkstra算法及变形

    图示: 模版: /* Dijkstra计算单源最短路径,并记录路径 m个点,n条边,每条边上的权值非负,求起点st到终点et的最短路径 input: n m st et 6 10 1 6 1 2 6 ...

  9. PAT甲级1018. Public Bike Management

    PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...

随机推荐

  1. VC++6.0连接Access数据库

    建立一个连接数据库的类: 1.头文件:ADOConn.h #import "C:\Program Files\Common Files\System\ado\msado15.dll" ...

  2. 关于ShareSDK接入的各种问题,以及解决方案

    随着社交网络的流行,游戏接入分享已经是必然.毕竟这是非常好的一种推广方式.ShareSDK是一个非常好的内分享提供商!但是接入后发生的各种问题,下面给大家提供几个本人遇到的问题,以及解决方法: 1)微 ...

  3. 【转】JS设计模式开篇

        (原文地址:http://blog.chinaunix.net/uid-26672038-id-3904513.html)     本文主要讲述一下,什么是设计模式(Design patter ...

  4. [微软实习生2014]K-th string

    很久之前的事情了,微软2014实习生的在线测试题,记录下来以备后用. 题目描述: Description Consider a string set that each of them consist ...

  5. bzoj 3218 a + b Problem(最小割+主席树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3218 [题意] 给n个格子涂白或黑色,白则wi,黑则bi的好看度,若黑格i存在: 1& ...

  6. php pdo(二)

    定义:PDO(PHP Data Object)是PHP5才支持的扩展,它为PHP访问各种数据库定义了一个轻量级的.一致性的接口. PDO是PHP5中的一个重大功能,PHP6中将只默认使用PDO来处理数 ...

  7. 内核源码分析之tasklet(基于3.16-rc4)

    tasklet是在HI_SOFTIRQ和TASKLET_SOFTIRQ两个软中断的基础上实现的(它们是在同一个源文件中实现,由此可见它们的关系密切程度),它的数据结构和软中断比较相似,这篇博文将分析t ...

  8. 五、python使用模块

    if __name__=='__main__':用法: 当我们在命令行运行模块文件时,Python解释器把一个特殊变量__name__置为__main__,而如果在其他地方导入该hello模块时,if ...

  9. Lucene Query Term Weighting

    方法 public static Query TermWeighting(Query tquery,Map<String,Float>term2weight){ BooleanQuery ...

  10. 你想成为优秀的Java程序员吗?

    Java是全世界最受欢迎的3大编程语言之一,它可以开发出许多实用的WEB应用程序和桌面应用程序,更重要的一点,Java是跨平台的语言——编写一次,可以再任何地方运行.另外,Java也很容易入门,如果你 ...