Round(581)C—— Anna, Svyatoslav and Maps(最短路)

题目链接:https://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,…,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.

 

题目翻译:

主要字符被省略为短字符。

 

给出了一个没有n个顶点的环的有向无权重图和一条由m个顶点的序列p1,p2,…,pm给出的路径(该路径不必简单);对于每个1≤i<m,有一条从pi到pi+1的弧。

 

如果v是p的子序列,v1=p1,v k=pm,并且p是按顺序通过v1,…,vk的最短路径之一,则将k顶点的序列v1,v2,…,vk定义为良好。

 

如果A可以通过删除几个(可能是零或全部)元素从B中获得,则序列A是序列B的子序列。很明显,序列P是好的,但您的任务是找到最短的好的子序列。

 

如果存在多个最短良好子序列,则输出其中任何一个。

 

输入

第一行包含一个整数n(2≤n≤100)-图中顶点的数目。

 

接下来的n行通过邻接矩阵定义图形:如果从顶点i到顶点j有一条弧,则i-st行中的j字符等于1,否则等于0。可以保证图形不包含循环。

 

下一行包含一个整数m(2≤m≤106)-路径中的顶点数。

 

下一行包含m个整数p1,p2,…,pm(1≤pi≤n)-路径中顶点的序列。可以保证,对于任何1≤i<m,有一个从pi到pi+1的弧。

 

产量

在第一行输出一个整数k(2≤k≤m)-最短良好子序列的长度。在第二行中,输出k整数v1,…,vk(1≤vi≤n)-子序列中的顶点。如果存在多个最短子序列,请打印任意子序列。任何两个连续的数字都应该是不同的。

 

输入

4
0110
0010
0001
1000
4
1 2 3 4
输出

3
1 2 4 

 

很容看出是最短路的题,不过题意有点小难懂,其实就是让你求给的路径上能删除部分节点之后,经过删除后节点所能访问的路径仍然是原路径,其实就是结果的每两个点之间的最短路上的点是给的路径上的点。

理解之后就好写了,先把起点加进去。

之后每次求出给定路径上终点到当前点的最短路,如果最短路小于给的路径的长度,那么把路径上该点的上一个点加进去。

最后别忘了把终点加进去。(范围比较小,用floyd就行了,也可以dfs来做)

#include<iostream>
#include<vector>
using namespace std;
const int maxn=107;
const int maxm=1e6+7;
const int INF=0x3f3f3f3f;
int G[maxn][maxn];
char str[maxn];
int p[maxm];
int n,m;
void floyd(){
	for(int k=1;k<=n;++k)
		for(int i=1;i<=n;++i)
			for(int j=1;j<=n;++j)
				G[i][j]=min(G[i][j],G[i][k]+G[k][j]);
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;++i){
		scanf("%s",str+1);
		for(int j=1;j<=n;++j){
			G[i][j]=str[j]=='0'?INF:1;
			if(i==j)
				G[i][j]=0;
		}
	}
	floyd();
	scanf("%d",&m);
	for(int i=1;i<=m;++i)
		scanf("%d",&p[i]);
	vector<int> v;
	v.clear();
	v.push_back(p[1]);
	int dis=0;
	for(int i=2;i<=m;++i){
		dis+=G[p[i-1]][p[i]];
		if(dis>G[v[v.size()-1]][p[i]]){
			v.push_back(p[i-1]);
			dis=G[v[v.size()-1]][p[i]];
		}
	}
	v.push_back(p[m]);
	printf("%d\n",v.size());
	for(int i=0;i<v.size();++i)
		printf("%d ",v[i]);
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值