PAT甲级练习1018. Public Bike Management (30)

1018. Public Bike Management (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 Spis 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
一个调度的问题。从起点出发,在到目标点经过的路径上都将自行车数调整到容量的一半。路径选择的标准是路径、送出车的数量、及最后送回来的车的数量。

想到的方法是dfs,记录路径上的点以及相应的车数和长度,最后结果并不对,甚至还有超时。。先贴上代码

#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;


int c, n, target, m;
int minLength, minNum, minSend, minBack;
int cn[501];
bool visited[501];
int r[501][501];
vector<int> bestPath;

int adjust(int x, int capacity){
  return capacity/2 - x;
}

void dfs(int start, int end, int length, int num, vector<int> path){
  visited[start] = true;
  if(start == end){
    if(bestPath.empty()){
      bestPath.swap(path);
      minLength = length;
      minNum = num;
      if(num>=0){
        minSend = num;
        minBack = 0;
      }else{
        minSend = 0;
        minBack = -num;
      }
    }
    else{
      if(length<minLength){//首先比较路程长度
        minLength = length;
        minNum = num;
        if(num>=0){
          minSend = num;
          minBack = 0;
        }else{
          minSend = 0;
          minBack = -num;
        }
        bestPath.swap(path);
      }else if(length==minLength){//再比较需要送出的车数
        if(num>=0){
          if(minNum>=0){
            if(num<minNum){
              minSend = num;
              minBack = 0;
              bestPath.swap(path);
            }
          }
        }else{
          minSend = 0;
          if(minNum>=0){
            minBack = -num;
            bestPath.swap(path);
          }else{
            if(num>minNum){
              minBack = -num;
              bestPath.swap(path);
            }
          }
        }
      }else;
    }
  }
  for(int i=1; i<=n; i++){
    if(visited[i]==false && r[start][i]!=0){
      vector<int> tmp;
      tmp.swap(path);
      tmp.push_back(i);
      dfs(i, end, length+r[start][i], num+adjust(cn[i],c), tmp);
    }
  }
  visited[start] = false;
}


int main(){
  
  scanf("%d %d %d %d", &c, &n, &target, &m);
  for(int i=1; i<=n; i++){
    scanf("%d", &cn[i]);
  }
  for(int i=1; i<=m; i++){
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    r[x][y] = r[y][x] = z;
  }

  int length=0, num=0;
  vector<int> path;
  dfs(0, target, length, num, path);

  printf("%d 0", minSend);
  for(int i=0; i<bestPath.size(); i++){
    printf("->%d", bestPath[i]);
  }
  printf(" %d", minBack);

  cin>>n;
  return 0;
}

评测结果

时间 结果 得分 题目 语言 用时(ms) 内存(kB) 用户
2月16日 22:20 部分正确 24 1018 C++ (g++ 4.7.2) 14 392 shower

测试点

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 14 380 12/12
1 答案正确 5 384 2/2
2 答案正确 4 384 2/2
3 答案正确 7 384 2/2
4 答案正确 13 384 2/2
5 答案错误 6 340 0/2
6 答案正确 7 384 2/2
7 答案错误 13 384 0/3
8 答案正确 7 392 2/2
9 运行超时     0/1

确实有逻辑爆炸的点:只有当到达目标点时才对length和num进行处理,实际上过程中num>0,即中间点需要补充车辆时,是无法通过后续点的车来补充的,即中间的send值要累加才对。

-----------------------------------------------------------------------------------------------------------

之后想法是首先用dfs求出所有可能得最短路径,再对这些路径按评价标准进行筛选找出题目要求的结果。但是倒数第三个测试点始终通不过,有点无能为力啊。。先贴上代码

#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>

using namespace std;

int c, n, target, m;
int minLength=10000000, minSend, minBack;
int cn[501];
bool visited[501];
int r[501][501];
vector<vector<int>> sPath;
//vector<vector<int>> blank;
vector<int> bestPath;//最优路线

int adjust(int x, int capacity){
	return capacity/2 - x;
}

void dfs_length(int start, int end, int length, vector<int> path){
	if(start == end){
		if(sPath.empty()){
			sPath.push_back(path);
			minLength = length;
		}else{
			if(length<minLength){
				minLength = length;
				vector<vector<int>> blank;
				sPath.swap(blank);
				sPath.push_back(path);
			}else if(length==minLength){
				sPath.push_back(path);
			}else;
		}
	}

	if(length>minLength){
		//visited[start] = false;
		return;
	}

	for(int i=1; i<=n; i++){
		if(visited[i]==false && r[start][i]!=0){
			visited[i] = true;
			vector<int> tmp;
			tmp.swap(path);
			tmp.push_back(i);
			dfs_length(i, end, length+r[start][i], tmp);
			visited[i] = false;
		}
	}
	
}

void getNeed(){
	for(int i=0; i<sPath.size(); i++){
		int num=0, send=0, back=0;
		for(int j=0; j<sPath[i].size(); j++){
			int x=adjust(cn[sPath[i][j]], c);
			if(x+num>0){
				send += x + num;
				num = 0;
			}else{
				num += x; 
			}
		}
		//对整条路径按评价原则判断取舍
		if(i==0){
			minSend = send;
			if(num>=0)minBack = 0;
			else minBack = num;
			bestPath.swap(sPath[i]);
		}else{
			if(send<minSend){
				bestPath.swap(sPath[i]);
				minSend = send;
				if(num>=0)minBack = 0;
				else minBack = num;
			}else if(send==minSend){
				if(num<0){
					if(num>minBack){//送回数更少
						minBack = num;
						bestPath.swap(sPath[i]);
					}
				}else if(num>minBack){//不需送回
					minBack = 0;
					bestPath.swap(sPath[i]);
				}
			}
		}
	}
}

int main(){
	
	scanf("%d %d %d %d", &c, &n, &target, &m);
	for(int i=1; i<=n; i++){
		scanf("%d", &cn[i]);
	}
	for(int i=1; i<=m; i++){
		int x, y, z;
		scanf("%d %d %d", &x, &y, &z);
		r[x][y] = r[y][x] = z;
	}

	int length=0;
	vector<int> path;
	dfs_length(0, target, length, path);
	getNeed();

	printf("%d 0", minSend);
	for(int i=0; i<bestPath.size(); i++){
		printf("->%d", bestPath[i]);
	}
	printf(" %d\n", -minBack);

	//cin>>n;
	return 0;
}

评测结果

时间 结果 得分 题目 语言 用时(ms) 内存(kB) 用户
2月18日 11:11 部分正确 27 1018 C++ (g++ 4.7.2) 17 2508 shower

测试点

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 8 384 12/12
1 答案正确 4 384 2/2
2 答案正确 3 376 2/2
3 答案正确 6 316 2/2
4 答案正确 8 384 2/2
5 答案正确 3 416 2/2
6 答案正确 6 384 2/2
7 答案错误 8 348 0/3
8 答案正确 6 324 2/2
9 答案正确 17 2508 1/1

--------------------------------------------------------------------------------------------------------------

我!的!天!终于让我找到错的地方了!

在网上找了一个测试集试了一下,结果发现结果就不对了,然后debug后发现是我代码里传递path的tmp赋值使用swap的问题,因为在下次赋值的时候就变成空的赋进去,没想到前面那些测试点都能通过,真是醉了

10 4 4 5
6 7 5 0
0 1 1
0 2 1
1 3 1
2 3 1
3 4 1

#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>

using namespace std;

int c, n, target, m;
int minLength=10000000, minSend, minBack;
int cn[501];
bool visited[501];
int r[501][501];
vector<vector<int>> sPath;
vector<int> bestPath;//最优路线

int adjust(int x, int capacity){
	return capacity/2 - x;
}

void dfs_length(int start, int end, int length, vector<int> path){
	if(start == end){
		if(sPath.empty()){
			sPath.push_back(path);
			minLength = length;
		}else{
			if(length<minLength){
				minLength = length;
				vector<vector<int>> blank;
				sPath.swap(blank);
				sPath.push_back(path);
			}else if(length==minLength){
				sPath.push_back(path);
			}else;
		}
	}

	if(length>minLength){//剪枝解决最后一个测试点超时的问题
		return;
	}

	for(int i=1; i<=n; i++){
		if(visited[i]==false && r[start][i]!=0){
			visited[i] = true;
			vector<int> tmp(path);
			//tmp.swap(path);此处这样赋值是有问题的!应该改成上面这样
			tmp.push_back(i);
			dfs_length(i, end, length+r[start][i], tmp);
			visited[i] = false;
		}
	}
	
}

void getNeed(){
	for(int i=0; i<sPath.size(); i++){
		int num=0, send=0, back=0;
		for(int j=0; j<sPath[i].size(); j++){
			int x=adjust(cn[sPath[i][j]], c);
			if(x+num>=0){
				send += x + num;
				num = 0;
			}else{
				num += x; 
			}
		}
		//对整条路径按评价原则判断取舍
		if(i==0){
			minSend = send;
			if(num>=0)minBack = 0;
			else minBack = num;
			bestPath.swap(sPath[i]);
		}else{
			if(send<minSend){
				bestPath.swap(sPath[i]);
				minSend = send;
				if(num>=0)minBack = 0;
				else minBack = num;
			}else if(send==minSend){
				if(num<0){
					if(num>minBack){//送回数更少
						minBack = num;
						bestPath.swap(sPath[i]);
					}
				}else if(num>minBack){//不需送回
					minBack = 0;
					bestPath.swap(sPath[i]);
				}
			}
		}
	}
}

int main(){
	
	scanf("%d %d %d %d", &c, &n, &target, &m);
	for(int i=1; i<=n; i++){
		scanf("%d", &cn[i]);
	}
	for(int i=1; i<=m; i++){
		int x, y, z;
		scanf("%d %d %d", &x, &y, &z);
		r[x][y] = r[y][x] = z;
	}

	int length=0;
	vector<int> path;
	dfs_length(0, target, length, path);
	getNeed();

	printf("%d 0", minSend);
	for(int i=0; i<bestPath.size(); i++){
		printf("->%d", bestPath[i]);
	}
	printf(" %d\n", -minBack);

	cin>>n;
	return 0;
}

评测结果

时间 结果 得分 题目 语言 用时(ms) 内存(kB) 用户
2月18日 12:35 答案正确 30 1018 C++ (g++ 4.7.2) 69 3236 shower

测试点

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 19 384 12/12
1 答案正确 18 384 2/2
2 答案正确 15 372 2/2
3 答案正确 2 380 2/2
4 答案正确 18 384 2/2
5 答案正确 4 384 2/2
6 答案正确 18 384 2/2
7 答案正确 6 384 3/3
8 答案正确 7 248 2/2
9 答案正确 69 3236 1/1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值