PAT(甲级)2021年春季考试 7-4 Recycling of Shared Bicycles (30 分) Floyd+DFS

21 篇文章 1 订阅

PAT(甲级)2021年春季考试
7-4 Recycling of Shared Bicycles (30 分) Floyd+DFS

【题目描述】
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 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

【解题思路】

这道题是一个简单的图论问题,一共有从0到n编号的点,其中0是出发点。要求每次到达距离最短的下一个点(如果距离相等则编号小的点优先),最后输出访问路径。如果所有点都能访问到,输出路径长度之和;否则输出不能访问到的点。

n最大为200,数据范围不大,我们使用邻接矩阵存图。使用Floyd更新每两个点之间的最短路径长度。

从0结点出发,通过DFS记录每个点的访问情况和访问路径即可。

【满分代码】

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int INF=1000000000;
int n,m,G[205][205],vis[205],ans=0;
vector<int> vec;//存储访问路径 
void dfs(int pos,int temp)
{
	if(vec.size()==n)//1-n都已访问过 
	{
		ans=temp;
		return;
	}
	int minpos=0,mindis=INF,flag=0;
	for(int i=0;i<=n;i++)
	{
		if(!vis[i])
			if(G[pos][i]<mindis)
			{
				flag=1;
				mindis=G[pos][i];
				minpos=i;
			}
	}//找到最短距离的点(距离相同时编号小的点优先) 
	if(!flag) return;//没有点可以访问 
	vis[minpos]=1;
	vec.emplace_back(minpos);
	dfs(minpos,temp+mindis);//访问下一个点 
}
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	int a,b,c;
	for(int i=0;i<=n;i++)
	for(int j=0;j<=n;j++)
	{
		if(i==j)
			G[i][j]=0;
		else
			G[i][j]=INF;
	}//邻接矩阵初始化 
	for(int i=0;i<m;i++)
	{
		cin>>a>>b>>c;
		G[a][b]=G[b][a]=c;
	}//输入图 
	for(int i=0;i<=n;i++)
	for(int j=0;j<=n;j++)
	for(int k=0;k<=n;k++)
		if(G[i][k]+G[k][j]<G[i][j])
			G[i][j]=G[i][k]+G[k][j];//Floyd最短路更新 
	vis[0]=1;
	dfs(0,0);//从0点出发开始访问 
	cout<<"0";
	for(int i=0;i<vec.size();i++)
		cout<<" "<<vec[i];
	cout<<endl;//输出路径 
	if(vec.size()==n)
		cout<<ans<<endl;//如果所有点都访问过,输出路径长度之和 
	else
	{
		int flag=1;
		for(int i=0;i<=n;i++)
			if(!vis[i])
			{
				if(flag)
				{
					cout<<i;
					flag=0;
				}
				else
					cout<<" "<<i;
			}
	}//输出未访问到的点 
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

球王武磊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值