Anna, Svyatoslav and Maps(弗洛伊德)

一、题目描述

The main characters have been omitted to be short.

You are given a directed unweighted graph without loops with n vertexes and a path in it (that path is not necessary simple) given by a sequence p1,p2,…,pm of m vertexes; for each 1≤i<m there is an arc from pi to pi+1.

Define the sequence v1,v2,…,vk of k vertexes as good, if v is a subsequence of p, v1=p1, vk=pm, and p is one of the shortest paths passing through the vertexes v1, …, vk in that order.

A sequence a is a subsequence of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements. It is obvious that the sequence p is good but your task is to find the shortest good subsequence.

If there are multiple shortest good subsequences, output any of them.

Input
The first line contains a single integer n (2≤n≤100) — the number of vertexes in a graph.

The next n lines define the graph by an adjacency matrix: the j-th character in the i-st line is equal to 1 if there is an arc from vertex i to the vertex j else it is equal to 0. It is guaranteed that the graph doesn’t contain loops.

The next line contains a single integer m (2≤m≤106) — the number of vertexes in the path.

The next line contains m integers p1,p2,…,pm (1≤pi≤n) — the sequence of vertexes in the path. It is guaranteed that for any 1≤i<m there is an arc from pi to pi+1.

Output
In the first line output a single integer k (2≤k≤m) — the length of the shortest good subsequence. In the second line output k integers v1, …, vk (1≤vi≤n) — the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct.

Examples
Input
4
0110
0010
0001
1000
4
1 2 3 4
Output
3
1 2 4
Input
4
0110
0010
1001
1000
20
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
Output
11
1 2 4 2 4 2 4 2 4 2 4
Input
3
011
101
110
7
1 2 3 1 3 2 1
Output
7
1 2 3 1 3 2 1
Input
4
0110
0001
0001
1000
3
1 2 4
Output
2
1 4
Note
Below you can see the graph from the first example:

在这里插入图片描述

The given path is passing through vertexes 1, 2, 3, 4. The sequence 1−2−4 is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes 1, 2 and 4 in that order is 1−2−3−4. Note that subsequences 1−4 and 1−3−4 aren’t good because in both cases the shortest path passing through the vertexes of these sequences is 1−3−4.

In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes.

In the fourth example, the paths 1−2−4 and 1−3−4 are the shortest paths passing through the vertexes 1 and 4.

二、解题思路

题意是:给你一个序列,然后删除节点使得删除后的序列权值和之前想等,,也就是求一条点最少的路径,使用佛洛依德算法跑出最短路的矩阵,然后循环判断是否能去掉该结点,不能去掉标记为1,最后输出就行。

三、AC代码

#include <iostream>
using namespace std;
const int MX = 107;
const int inf = 0x3f3f3f3f;
const int N = 1e6 + 3;
int mp[MX][MX], p[N], f[N];

int main() {

	int n, m;
	ios::sync_with_stdio(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			char tmp;
			cin >> tmp;

			if (tmp == '1')   mp[i][j] = 1;
			else              mp[i][j] = inf;
		}
	}

	for (int k = 1; k <= n; k++) {
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				if (mp[i][k] < inf && mp[k][j] < inf)
					mp[i][j] = min(mp[i][k] + mp[k][j], mp[i][j]);
			}
		}
	}

	cin >> m;
	int t = m;

	for (int i = 1; i <= m; i++) {
		cin >> p[i];
	}
		
	int first = p[1];

	for (int i = 2; i <= m - 1; i++) {
	    /*不能删除的节点为到中间节点加上中间节点到下一个节点的距离小于等于
	    节点到最后一个节点的距离*/
		if (mp[first][p[i]] + mp[p[i]][p[i + 1]] <= mp[first][p[i + 1]] && first != p[i + 1]) {
			f[i] = 1;
			t--;
		}
		else first = p[i];
	}
	cout << t << endl;
	for (int i = 1; i <= m; i++) {
		if (f[i])continue;
		cout << p[i] << " ";
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值