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,…,pmp1,p2,…,pm of mm vertexes; for each 1≤i<m1≤i<m there is an arc from pipi to pi+1pi+1.
Define the sequence v1,v2,…,vkv1,v2,…,vk of kk vertexes as good, if vv is a subsequence of pp, v1=p1v1=p1, vk=pmvk=pm, and pp is one of the shortest paths passing through the vertexes v1v1, ……, vkvk 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≤1002≤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 11 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≤1062≤m≤106) — the number of vertexes in the path.
The next line contains mm integers p1,p2,…,pmp1,p2,…,pm (1≤pi≤n1≤pi≤n) — the sequence of vertexes in the path. It is guaranteed that for any 1≤i<m1≤i<m there is an arc from pipi to pi+1pi+1.
Output
In the first line output a single integer kk (2≤k≤m2≤k≤m) — the length of the shortest good subsequence. In the second line output kk integers v1v1, ……, vkvk (1≤vi≤n1≤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 11, 22, 33, 44. The sequence 1−2−41−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 11, 22 and 44 in that order is 1−2−3−41−2−3−4. Note that subsequences 1−41−4 and 1−3−41−3−4 aren't good because in both cases the shortest path passing through the vertexes of these sequences is 1−3−41−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−41−2−4 and 1−3−41−3−4 are the shortest paths passing through the vertexes 11 and 44.
思路:
注意数据范围会影响数组的大小,
最短路floyd,可以得知a到b的最短路径,详细内容请看注释。
代码:
int w[110][110];//floyd能准确得知a到b的距离
int n;
int res[maxj];
bool vis[maxj];//路线有1e6个
void floyd(){
for(int k=1;k<=n;++k){
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
w[i][j]=min(w[i][j],w[i][k]+w[k][j]);//选择最小路径
}
}
}
}
void solve(){//题意:给你一个路径,你输出一个以此为最短路径的节点,其实在删除一些节点
scanf("%d",&n);
for(int i=1;i<=n;++i){
for(int j=1;j<=n+1;++j){//j==1是输入回车
char x;
scanf("%c",&x);
if(x=='1'){
w[i][j-1]=1;//初始化数组
}else w[i][j-1]=inf;
}
w[i][i]=0;
}
// char c;c=getchar();
floyd();
int k;scanf("%d",&k);
for(int i=1;i<=k;++i){
scanf("%d",res+i);//
}
int now=res[1];//开头和结尾必须有
int m=k;
for(int i=2;i<=k-1;++i){
if(w[now][res[i]]+w[res[i]][res[i+1]]<=w[now][res[i+1]]&&now!=res[i+1]){//从res【i】经过是最短路时,不要这个点
vis[i]=1;m--;
}else now=res[i];//不是最短路时,now必须经过这个点才可以符合题意
}
printf("%d\n",m);
for(int i=1;i<=k;++i){
if(!vis[i])printf("%d ",res[i]);
}cout<<'\n';
}