PAT甲级 2021年春季考试 7-4 Recycling of Shared Bicycles 【floyd最短路】

在线测试传送门:PAT(甲级)2021年春仿真卷

7-4 Recycling of Shared Bicycles (30 分)
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:

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

题意

给你一个带权无向图,从0出发,每次走的点为距离当前点最近的点(如果有多个距离最近,则走点号最小的),按顺序输出走到点的序列。
如果所有点都被走到,再输出路径和;否则再输出所有未走到的点。

思路

多个点对的最短路径,直接上floyd算法。
注意的是floyd的三层循环,其中遍历中间点k的循环一定要写在最外层循环!(我因为写在最里层,这题丢了一个6分测试点QAQ)

AC代码

#include <bits/stdc++.h>
using namespace std;
const int N=210,inf=0x3f3f3f3f;
int n,m,dp[N][N];
bool vis[N];
void floyd() // 求两点间最短路
{
	// 无负权边,可初始化自己到自己距离为0
    for(int i=0;i<=n;i++)
        dp[i][i]=0;
    for(int k=0;k<=n;k++) // 中间点k的遍历一定要放在第一层循环!
        for(int i=0;i<=n;i++)
            for(int j=0;j<=n;j++)
                if(i!=j)dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
}
vector<int>ans;
int sum=0; // 记录最短路径和
void dfs(int u)
{
    ans.push_back(u);
    vis[u]=1;
    int v=-1,mi=inf;
    for(int i=1;i<=n;i++)
    {
        if(i!=u&&!vis[i]&&dp[u][i]<mi) // mi是当前找到的最短路,注意i!=u
        {
            mi=dp[u][i];
            v=i;
        }
    }
    if(v!=-1)
    {
        sum+=mi;
        dfs(v);
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    int x,y,z;
    memset(dp,inf,sizeof(dp));
    for(int i=1;i<=m;i++)
    {
        cin>>x>>y>>z;
        dp[x][y]=dp[y][x]=z;
    }
    floyd();
    dfs(0);
    int sz1=ans.size();
    for(int i=0;i<sz1;i++)
        i==sz1-1?printf("%d\n",ans[i]):printf("%d ",ans[i]);
    vector<int>t;
    for(int i=1;i<=n;i++)
        if(!vis[i])t.push_back(i);
    int sz2=t.size();
    if(sz2)
    {
        for(int i=0;i<sz2;i++)
            i==sz2-1?printf("%d\n",t[i]):printf("%d ",t[i]);
    }
    else printf("%d\n",sum);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

nefu-ljw

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

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

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

打赏作者

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

抵扣说明:

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

余额充值