您的位置:首页 > 其它

准备PAT之Public Bike Management——DFS/DFS+Dijikstra

2017-08-22 16:59 232 查看
Public Bike Management (30)

时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)

题目描述

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.

输入描述:

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.

输出描述:

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.
4000

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.

输入例子:

10 3 3 5

6 7 0

0 1 1

0 2 1

0 3 3

1 3 1

2 3 1

输出例子:

3 0->2->3 0

DFS方法

//若求得是有多条最短路径且根据要求选择,可用sort()+cmp()进行排序
//但若要对路径上的点进行操作,我觉的DFS比Dijikstra要方便
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
const int max_bike = 505;
const int INF = 0x3f3f3f3f;

int cost[max_bike][max_bike];
int cur_cost_time, cur_back_bike, cur_take_bike;
int full_bike, station_num, sp, road_num;
int if_visit[max_bike];
int min_back_bike, min_take_bike, min_cost_time;
int station_bike[max_bike];
vector<int> short_path;
vector<int>cur_path;

void Init() {
for (int i = 0; i <= station_num; i++) {
for (int j = 0; j <= station_num; j++) {
cost[i][j] = INF;
}
}
memset(if_visit, 0, sizeof(if_visit));
cur_cost_time = cur_back_bike = cur_take_bike = 0;
min_cost_time = min_back_bike = min_take_bike = INF;
}
void Input_and_init() {
cin >> full_bike >> station_num >> sp >> road_num;
Init();
for (int i = 1; i <= station_num; i++) {
cin >> station_bike[i];
}
for (int i = 0; i < road_num; i++) {
int from, to, _cost;
cin >> from >> to >> _cost;
cost[from][to] = cost[to][from] = _cost;
}
}
void Dfs(int cur_station) {//当前的站点不压入堆中
//可不可以计算当前的点而不是计算下一个
if (cur_cost_time > min_cost_time)  return;

if (cur_station == sp) {
if (cur_cost_time < min_cost_time) {
min_cost_time = cur_cost_time;
min_back_bike = cur_back_bike;
min_take_bike = cur_take_bike;
short_path = cur_path;
}
else {
if (cur_take_bike < min_take_bike ||
(cur_take_bike == min_take_bike&&cur_back_bike < min_back_bike)) {
min_take_bike = cur_take_bike;
min_back_bike = cur_back_bike;
short_path = cur_path;
}
}
return;
}

for (int i = 1; i <= station_num; i++) {
if (if_visit[i] || cost[cur_station][i] == INF) continue;

int _cur_back = cur_back_bike, _cur_take = cur_take_bike;

if (cur_back_bike + station_bike[i] < full_bike / 2) {
cur_take_bike += full_bike / 2 - cur_back_bike - station_bike[i];
cur_back_bike = 0;
}
else {
cur_back_bike += station_bike[i] - full_bike / 2;
}

if_visit[cur_station] = 1;
cur_path.push_back(i);
cur_cost_time += cost[cur_station][i];
Dfs(i);
cur_path.pop_back();
if_visit[cur_station] = 0;//漏了,找了将近半个小时,得出->先先检查回溯,再看过程
cur_cost_time -= cost[cur_station][i];
cur_back_bike = _cur_back; cur_take_bike = _cur_take;
}
}
void Output() {
cout << min_take_bike << " 0";
for (int i = 0; i < short_path.size(); i++) {
cout << "->" << short_path[i];
}
cout << " " << min_back_bike << endl;
}
int main() {
Input_and_init();
Dfs(0);
Output();
return 0;
}


DFS()+Dijikstra方法

#include<algorithm>
#include<vector>
#include<queue>
#include<fstream>
#include<iostream>
using namespace std;
const int INF = 100000000;
const int max_bike = 505;
int bike_max, num_station, sp, num_road;

int cost[max_bike][max_bike];
int short_dist[max_bike];
int if_visit[max_bike];
int station_bike[max_bike];

struct Prev {
vector<int> prev_station;
int take, back;
};
vector<Prev> _prev;
Prev temp;

void Init() {
fill(short_dist, short_dist + num_station + 3, INF);
fill(if_visit, if_visit + num_station + 3, 0);
for (int i = 0; i <= num_station; i++)
for (int j = 0; j <= num_station; j++)
cost[i][j] = INF;
short_dist[0] = 0;
}
void Input() {
cin >> bike_max >> num_station >> sp >> num_road;
Init();
for (int i = 1; i <= num_station; i++) {
cin >> station_bike[i];
}
for (int i = 0; i < num_road; i++) {
int _from, _to, _cost;
cin >> _from >> _to >> _cost;
cost[_from][_to] = cost[_to][_from] = _cost;
}
}
void Dijiksta(int s) {

while(true) {
int v = -1;
for (int i = 0; i <= num_station; i++) {
if (!if_visit[i] && (v == -1 || short_dist[i] < short_dist[v])) {
v = i;
}
}
if (v == -1)    break;

if_visit[v] = 1;
for (int i = 0; i <= num_station; i++) {
if (short_dist[i] > short_dist[v] + cost[v][i]) {
short_dist[i] = short_dist[v] + cost[v][i];
}
}
}
}
void Dfs(int cur_station, int path_cost) {
if_visit[cur_station] = 1;
//此处不把0压入
if (cur_station) temp.prev_station.push_back(cur_station);

if (path_cost > short_dist[sp]) return;
if (cur_station == sp) {
if (path_cost == short_dist[sp])
_prev.push_back(temp);
return;
}

for (int i = 0; i <= num_station; i++) {
if (!if_visit[i] && cost[cur_station][i] != INF) {
Dfs(i, path_cost + cost[cur_station][i]);
if_visit[i] = 0;
temp.prev_station.pop_back();
}
}
}
bool cmp(const Prev& a,const Prev& b){
if(a.take==b.take)
return a.back < b.back;
return a.take < b.take;
}
void Output() {
sort(_prev.begin(), _prev.end(), cmp);
cout << _prev[0].take<<" "<<0;
for (int i = 0; i < _prev[0].prev_station.size(); i++) {
cout << "->" << _prev[0].prev_station[i];
}
cout << " " << _prev[0].back << endl;
}
void Fix_flag() {
for (int i = 0; i <= num_station; i++) {
if_visit[i] = 0;
}
}
void Calculater() {//计算各点对自行车的影响
for (int i = 0; i < _prev.size(); i++) {
int need_back = 0; int need_take = 0;
for (int j = 0; j <_prev[i].prev_station.size(); j++) {
int n = _prev[i].prev_station[j];
if (station_bike
+ need_back <= bike_max / 2) {
need_take += bike_max / 2 - station_bike
- need_back;
need_back = 0;
}
else {
need_back += station_bike
- bike_max / 2;
}
}
_prev[i].back = need_back;
_prev[i].take = need_take;
}
}
int main() {
Input();
Dijiksta(0);
Fix_flag();//为了DFS对if_visit进行清零
Dfs(0, 0);
Calculater();
Output();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: