首页 技术 正文
技术 2022年11月12日
0 收藏 692 点赞 4,237 浏览 3918 个字

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.

A1018. Public Bike Management
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
 #include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int G[][], v[];
int visit[], dst[];
vector<int> pre[];
const int INF = ;
int Cmax, N, Sp, M;
void dijkstra(int s){
fill(visit, visit + , );
fill(dst, dst + , INF);
dst[s] = ;
for(int i = ; i <= N; i++){
int u = -, minLen = INF;
for(int j = ; j <= N; j++){
if(visit[j] == && dst[j] < minLen){
u = j;
minLen = dst[j];
}
}
if(u == -)
return;
visit[u] = ;
for(int j = ; j <= N; j++){
if(visit[j] == && G[u][j] != INF){
if(G[u][j] + dst[u] < dst[j]){
dst[j] = G[u][j] + dst[u];
pre[j].clear();
pre[j].push_back(u);
}else if(G[u][j] + dst[u] == dst[j]){
pre[j].push_back(u);
}
}
}
}
}
vector<int> path, ans;
int send = INF, back = INF;
void dfs(int vt){
path.push_back(vt);
if(vt == ){
int tempBack = , tempSend = , T = Cmax / ;
for(int i = path.size() - ; i >= ; i--){
if(v[path[i]] < T){
int shortage = T - v[path[i]];
if(shortage <= tempBack)
tempBack = tempBack - shortage;
else{
tempSend = tempSend + shortage - tempBack;
tempBack = ;
}
}else{
tempBack += v[path[i]] - T;
}
}
if(tempSend < send || tempSend == send && tempBack < back){
back = tempBack;
send = tempSend;
ans = path;
}
path.pop_back();
return;
}
for(int i = ; i < pre[vt].size(); i++){
dfs(pre[vt][i]);
}
path.pop_back();
}
int main(){
scanf("%d%d%d%d", &Cmax, &N, &Sp, &M);
for(int i = ; i <= N; i++){
scanf("%d", &v[i]);
}
fill(G[], G[] + *, INF);
for(int i = ; i < M; i++){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
G[a][b] = G[b][a] = c;
}
dijkstra();
dfs(Sp);
printf("%d ", send);
for(int i = ans.size() - ; i > ; i--){
printf("%d->", ans[i]);
}
printf("%d %d", ans[], back);
cin >> N;
return ;
}

总结:

1、题意:求最短路,如果有多条,就求出需要发送自行车最少的一条,如果还有多条,就求出需要带回自行车最少的一条。 注意,路上某个节点多出来的自行车可以被它之后的节点补充。比如0点->A->B->C,如果B超了,C少了,可以把B多出来的给C补充,如果不够再从源点处拿。但如果A也少了,则不能把B多的补充给A,因为是按照源点->目的地的顺序一路前进。

2、错误:虽然节点是1到N, 0为管理处。但求最短路的时候,节点范围应从0到N。

另外,求自行车的send、back时,节点范围应从1到N。

另外,求自行车时,遍历的是 v[path[ i ]]而不是 v[ i ]。

3、读入图之前先对G做INF的初始化。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,020
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,772
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,850