7-4 Recycling of Shared Bicycles (30 分)--PAT甲级(Dfs+Flody)

There are many spots for parking the shared bicycles in Hangzhou. When some of the bicycles are broken, the management center will receive a message for sending a truck to collect them. Now given the map of city, you are supposed to program the collecting route for the truck. The strategy is a simple greedy method: the truck will always move to the nearest spot to collect the broken bicycles. If there are more than one nearest spot, take the one with the smallest index.

Input Specification:
Each input file contains one test case. For each case, the first line contains two positive integers: N (≤ 200), the number of spots (hence the spots are numbered from 1 to N, and the management center is always numbered 0), and M, the number of streets connecting those spots. Then M lines follow, describing the streets in the format:

S1 S2 Dist
where S1 and S2 are the spots at the two ends of a street, and Dist is the distance between them, which is a positive integer no more than 1000. It is guaranteed that each street is given once and S1 is never the same as S2.

Output Specification:
For each case, first print in a line the sequence of spots in the visiting order, starting from 0. If it is impossible to collect all the broken bicycles, output in the second line those spots that cannot be visited, in ascending order of their indices. Or if the job can be done perfectly, print in the second line the total moving distance of the truck.

All the numbers in a line must be separated by 1 space, and there must be no extra space at the beginning or the end of the line.

Sample Input 1 (shown by the figure below):
7 10
0 2 1
0 4 5
0 7 3
0 6 4
0 5 5
1 2 2
1 7 2
2 3 4
3 4 2
6 7 9
结尾无空行
sample.JPG

Sample Output 1:
0 2 1 7 6 3 4 5
33
结尾无空行
Sample Input 2:
7 8
0 2 1
0 4 5
0 7 3
1 2 2
1 7 2
2 3 4
3 4 2
6 5 1
结尾无空行
Sample Output 2:
0 2 1 7 3 4
5 6
结尾无空行

题目分析:拿到题目的时候懵了,主要原因是第一次做了40分钟,有点缓不过来了,头脑中一点思绪都没有,甚至题目读的也是朦朦胧胧,之后看了题解才发现题目大意。

给出N个结点相当于每个结点需要回收的自行车数量为(0~N),即自己的下标,给出M条路径。
要求的是从0点开始至其他所有能到达的点,(遍历要求是只走离当前最近的点,如果距离相同,则下标最小的点)。
如果是连通图,则输出第二行给出总路径。
如果是非连通图,输出第二行给出未走的结点(从小到大)。

在输出前需要两步操作(也是我没想到的):floyd求多源最短路+dfs求联通性,将结果保存。

下面给出具体实现。

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
const int N = 210, INF = 0x3f3f3f3f;
int d[N][N];
int n, m;
vector<int> res;
bool st[N], book[N];
void floyd(){
	for(int k = 0; k <= n; k ++)
		for(int i = 0; i <= n; i ++)
			for(int j = 0; j <= n; j ++)
				d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
void dfs(int x){
	res.push_back(x);
	st[x] = true;
	int t = -1;
	for(int i = 0; i <= n; i ++)
		if(st[i] == false && d[x][i] != INF && (t == -1 || d[x][t] > d[x][i]))//选择没在集合中的,数值最小的, 
			t = i;
	if(t == -1) return;
	dfs(t);
}
int main(){
	memset(d, INF, sizeof d);
	scanf("%d%d", &n, &m);
	while(m --){
		int x, y, z;
		scanf("%d%d%d", &x, &y, &z);
		d[x][y] = d[y][x] = z;
	}
	floyd();
	dfs(0);
	int sum = 0;
	for(int i = 0; i < res.size(); i ++){
		if(i != 0) sum += d[res[i]][res[i - 1]];
		cout <<res[i] << ' ';
		book[res[i]] = true;
	} 
	cout << endl;
	if(res.size() == n + 1){
		cout << sum << endl;
	}else{
		bool flag = false;
		for(int i = 0; i <= n; i ++){
			if(book[i] == false){
				if(flag == true) cout << ' ';
				cout << i;
				flag = true;
			}
		}
	} 
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值