浙大 PAT Advanced level 1018. Public Bike Management (30)
2016-05-13 18:03
459 查看
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算法获取所有的最短路径,然后利用递归的方式获取每种最短路径的完整路径,最后分析所有的最短路径,挑选出符合要求的一条。
当然也可以用“剪枝”的方法直接得出符合要求的路径。
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算法获取所有的最短路径,然后利用递归的方式获取每种最短路径的完整路径,最后分析所有的最短路径,挑选出符合要求的一条。
当然也可以用“剪枝”的方法直接得出符合要求的路径。
#include <iostream> #include <vector> using namespace std; #define MAX_TIME 0xffffff vector< vector<int> > vpre; // 记录最短路径 vector<int> bikes; int C, N, Sp, M; vector<int> sTime; vector< vector<int> > records; vector<int> visited; vector< vector<int> > ways; vector< pair<int, int> > results; // first->send second->bring /* 注意计算每种最短路径对应的send和bring bikes数量的方法!!!*/ void get_results() { results.resize(ways.size()); int index; // 获取每种方法的send和bring数值 for (unsigned int i = 0; i < ways.size(); ++i) { for (int j = ways[i].size()-1; j >= 0; --j) { index = ways[i][j]; results[i].second += bikes[index] - C/2; #if 0 if (bikes[index] > C/2) { results[i].second += bikes[index] - C/2; } if (bikes[index] < C/2) { results[i].second -= C/2 - bikes[index]; } #endif if (results[i].second < 0) { results[i].first -= results[i].second; results[i].second = 0; } } } } int best_result() { int bestindex = 0; for (unsigned int index = 1; index < ways.size(); ++index) { if (results[bestindex].first > results[index].first) { bestindex = index; } else { if (results[bestindex].first == results[index].first) { if (results[bestindex].second > results[index].second) { bestindex = index; } } } } return bestindex; } void restore_ways(int index, vector<int> partialway) { partialway.push_back(index); for (unsigned int i = 0; i < vpre[index].size(); ++i) { if (0 == vpre[index][i]) { ways.push_back(partialway); } else { restore_ways(vpre[index][i], partialway); } } } void initialpara(unsigned int size) { vpre.resize(size); bikes.resize(size); visited.resize(size); sTime.resize(size); sTime[0] = 0; for (int i = 1; i != size; ++i) { sTime[i] = MAX_TIME; } records.resize(size); for (int i = 0; i != size; ++i) { records[i].resize(size); for (int j = 0; j != size; ++j) { records[i][j] = MAX_TIME; } } } int main() { int ni, nj, nt; int min_time = MAX_TIME; int min_index; vector<int> temp; cin >> C >> N >> Sp >> M; ++N; initialpara(N); for (int i = 1; i != N; ++i) { cin >> bikes[i]; } for (int i = 0; i != M; ++i) { cin >> ni >> nj >> nt; records[ni][nj] = records[nj][ni] = nt; } for (int i = 0; i != N; ++i) { for (int j = 0; j != N; ++j) { if (0 == visited[j] && sTime[j] < min_time) { min_time = sTime[j]; min_index = j; } } visited[min_index] = 1; for (int k = 0; k != N; ++k) { if (sTime[k] > sTime[min_index] + records[min_index][k]) { sTime[k] = sTime[min_index] + records[min_index][k]; vpre[k].clear(); vpre[k].push_back(min_index); } else { if (sTime[k] == sTime[min_index] + records[min_index][k]) { vpre[k].push_back(min_index); } } } min_time = MAX_TIME; } restore_ways(Sp, temp); get_results(); min_index = best_result(); cout << results[min_index].first << ' ' << '0'; for (int i = ways[min_index].size()-1; i >= 0; --i) { cout << "->" << ways[min_index][i]; } cout << ' ' << results[min_index].second << endl; return 0; }
相关文章推荐
- jQuery 操作Cookie 存储 读取 删除等
- JAVA基础之表达式,语句,和块
- target与currentTarget区别
- consumer抓取消息--一个线程抓取,N个consumer的n个线程消费
- poj2594 treasure exploration
- nodejs express req.body 为undefined的一个bug
- Android的udp回显测试源码
- PhpStorm 快捷键大全 PhpStorm 常用快捷键和配置
- 如何查找在python中的模块和方法
- Linux常用命令汇总--cd
- NSSet和NSArray的异同点
- Building Apps with Over 64K Methods
- js替换字符中的斜杠反斜杠
- 单例模式
- iOS 预留帖子
- poj3680 区间图的最大权独立集问题
- 超低成本----音视频通讯+共享屏幕+电子白板
- Shell脚本学习指南(二)——变量、条件、循环
- 扬州鉴真国际半程马拉松
- hdu 2004 成绩转换