Anna, Svyatoslav and Maps(思维+folyd求最短路)

http://codeforces.com/contest/1204/problem/C

The main characters have been omitted to be short.

You are given a directed unweighted graph without loops with nn vertexes and a path in it (that path is not necessary simple) given by a sequence p1,p2,…,pm of mm vertexes; for each 1≤i<m there is an arc from pipi 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 aa is a subsequence of a sequence bb if aa can be obtained from bb by deletion of several (possibly, zero or all) elements. It is obvious that the sequence pp 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 nn (2≤n≤100) — the number of vertexes in a graph.

The next nn lines define the graph by an adjacency matrix: the jj-th character in the ii-st line is equal to 1 if there is an arc from vertex ii to the vertex jj else it is equal to 00. It is guaranteed that the graph doesn't contain loops.

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

The next line contains mm 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 pipi to pi+1.

Output

In the first line output a single integer kk (2≤k≤m) — the length of the shortest good subsequence. In the second line output kk 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

Copy

4
0110
0010
0001
1000
4
1 2 3 4

output

Copy

3
1 2 4 

input

Copy

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

Copy

11
1 2 4 2 4 2 4 2 4 2 4 

input

Copy

3
011
101
110
7
1 2 3 1 3 2 1

output

Copy

7
1 2 3 1 3 2 1 

input

Copy

4
0110
0001
0001
1000
3
1 2 4

output

Copy

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.

 

题意:按照所给序列在图上走最短路,要求输出最短的子序列,使得在图上走的最短路不变(说的不太清楚,就是有点迷)

思路:因为n最大才100,先floyd预处理出图中任意两点之间的最短路,然后在序列上求最短路(即下标之差),如果和图中最短路相同,那就不需要限制,如果图中最短路比序列中的最短路小,说明没有按照序列顺序走,所以用前面那个点限制一下

#include<bits/stdc++.h>
#pragma GCC optimize(3)
#define max(a,b) a>b?a:b
using namespace std;
typedef long long ll;
const int N=1e6+5;
const int Inf=0x3f3f3f3f;
char mp[105][105];
int G[105][105];
int p[N];
int q[N];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
    	scanf("%s",mp[i]+1);
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(i==j) G[i][j]=0;
			else if(mp[i][j]=='1') G[i][j]=1;
			else G[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(G[i][k]<Inf&&G[k][j]<Inf)
				G[i][j]=min(G[i][j],G[i][k]+G[k][j]);
			}
		}
	}
    int m;
    scanf("%d",&m);
    for(int i=1;i<=m;i++) scanf("%d",&p[i]);
    int ans=0;
    q[++ans]=p[1];
    int l=1,r=1;
    while(r<=m){
    	if(r-l==G[p[l]][p[r]]) r++;
    	else{
    		q[++ans]=p[r-1];
    		l=r-1;
		}
	}
	q[++ans]=p[m];
	printf("%d\n",ans);
	for(int i=1;i<=ans;i++) printf("%d%c",q[i]," \n"[i==ans]);
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值