Problem
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
Method
本题的主要知识点涉及floyd和dfs。题目的输出情形根据图是否连通有着两种形式,因此首先需要判断输入的图是否是连通的。判断是否连通可通过dfs进行图的遍历,当从起点出发的遍历结束时,统计vis数组中是否有未访问的顶点。若有,则按照顺序输出相关顶点。
若没有,则要输出最优路径以及路程距离。最优路径即每次需要选择为未访问过的最近顶点,这需要我们知道图中任意两点的距离,因此可以采用floyd算法(当然加个循环dijkstra也可以,有点麻烦)。最后根据顶点访问的顺序进行输出即可。
Code
#include<bits/stdc++.h>
using namespace std;
int n, m, e[2050][205];
vector<int> res;
bool vis[205];
void dfs(int x)
{
res.push_back(x);
vis[x] = true;
int u = -1, mini = 0x3f3f3f3f;
for(int i = 0; i <= n ; i++)
{
if(i != x && vis[i] == false && e[x][i] < mini)
{
mini = e[i][x];
u = i;
}
}
if(u == -1)
return;
dfs(u);
}
int main()
{
scanf("%d %d", &n, &m);
int a, b, t;
memset(e, 0x3f3f3f3f, sizeof(e));
for(int i = 0; i < m; i++)
{
scanf("%d %d %d", &a, &b, &t);
e[a][b] = e[b][a] = t;
}
for(int k = 0; k <= n; k++)
for(int i = 0; i <= n; i++)
for(int j = 0; j <= n; j++)
if(e[i][k] + e[k][j] < e[i][j])
e[i][j] = e[i][k] + e[k][j];
dfs(0);
if(res.size() == n+1)
{
int sum = 0;
for(int i = 0; i < res.size(); i++)
{
if(i)
printf(" ");
printf("%d", res[i]);
if(i)
sum += e[res[i-1]][res[i]];
}
printf("\n%d\n", sum);
}
else
{
for(int i = 0; i < res.size(); i++)
{
if(i)
printf(" ");
printf("%d", res[i]);
}
printf("\n");
int flag = 0;
for(int i = 1; i <= n; i++)
{
if(vis[i] == false)
{
if(flag)
printf(" %d", i);
else
{
printf("%d", i);
flag = true;
}
}
}
}
return 0;
}