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

C. Anna, Svyatoslav and Maps
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

题意:

给出n个点和他们的邻接关系,再给出一个序列,问如何删去尽可能多的点使得剩下的点的最短路仍是原序列

思路:

如果当前点到当前遍历到的点的距离大于最短路,则说明当前点是必须被加进答案的,因为要保证最短路是给出的序列,如果不选这个点就会让序列最短路变短,
所以更新答案的当前节点为遍历到的节点的上一个节点,当前节点到遍历的点的最短距离也要更新
比如样例1中原序列为1 2 3 4,其中1->2->3距离为2,而1->3的最短路为1,所以我们要加入2节点才能保证最短路仍是原来的序列,否则最短路就会变短

 #include<bits/stdc++.h>
using namespace std;
const int amn=1e2+,amn1=1e6+,inf=0x3f3f3f3f;
int dis[amn][amn],a[amn][amn],p[amn1],ans[amn1];
int main(){
int n,m;char in;
ios::sync_with_stdio();
cin>>n;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
cin>>in;
a[i][j]=in-'';
if(i==j)dis[i][j]=;
else{
if(a[i][j])dis[i][j]=;
else dis[i][j]=inf;
}
}
}
///floyd求任意两点间的最短路
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
cin>>m;
for(int i=;i<=m;i++)cin>>p[i];
int d=,tp=; ///d为当前点到当前遍历到的点的距离
ans[++tp]=p[]; ///题目要求开头必须在序列中
for(int i=;i<=m;i++){
d+=dis[p[i-]][p[i]];
if(d<=dis[ans[tp]][p[i]])continue;
ans[++tp]=p[i-]; ///如果当前点到当前遍历到的点的距离大于最短路,则说明当前点是必须被加进答案的,因为要保证最短路是给出的序列,如果不选这个点就会让最短路变短,所以更新答案的当前节点,当前节点到遍历的点的最短距离也要更新
d=dis[ans[tp]][p[i]];
}
ans[++tp]=p[m]; ///题目要求结尾必须在序列中
printf("%d\n",tp);
for(int i=;i<=tp;i++)printf("%d%c",ans[i],i<tp?' ':'\n');
}
/**
给出n个点和他们的邻接关系,再给出一个序列,问如何删去尽可能多的点使得剩下的点的最短路仍是原序列
如果当前点到当前遍历到的点的距离大于最短路,则说明当前点是必须被加进答案的,因为要保证最短路是给出的序列,如果不选这个点就会让序列最短路变短,
所以更新答案的当前节点为遍历到的节点的上一个节点,当前节点到遍历的点的最短距离也要更新
比如样例1中原序列为1 2 3 4,其中1->2->3距离为2,而1->3的最短路为1,所以我们要加入2节点才能保证最短路仍是原来的序列,否则最短路就会变短
**/

[最短路,floyd] Codeforces 1204C Anna, Svyatoslav and Maps的更多相关文章

  1. codeforces 1204C Anna, Svyatoslav and Maps(floyd+dp)

    题目链接:http://codeforces.com/problemset/problem/1204/C 给定一组序列,P1,P2,P3...Pm,这是一组合法路径的序列,即任意的Pi和Pi+1之间有 ...

  2. 1204C Anna, Svyatoslav and Maps

    题目大意 给你一个有向图和一个路径 让你在给定路径中选出尽量少的点使得新路径的最短路长度和原路径相等 给定路径相邻两点间距离为1 分析 先floyd求出两点间最短路 之后每次对于点i找到所有跟它的最短 ...

  3. Codeforces Round #581 (Div. 2) C. Anna, Svyatoslav and Maps (Floyd 算法,最短路)

    C. Anna, Svyatoslav and Maps time limit per test2 seconds memory limit per test256 megabytes inputst ...

  4. C. Anna, Svyatoslav and Maps

    C. Anna, Svyatoslav and Maps 给定一个有向图,给定一条有向路径,求一条顶点最少的路径,使得给定的路径是它的最短路 folyd预处理出任意两点间的最短路,然后判断是否可以缩点 ...

  5. [最短路,floyd] Codeforces 1202B You Are Given a Decimal String...

    题目:http://codeforces.com/contest/1202/problem/B B. You Are Given a Decimal String... time limit per ...

  6. Codeforces1204C. Anna, Svyatoslav and Maps (贪心 + Floyd)

    题目链接:传送门 题目大意: 给出n<=100的有向图,和路径p,求p的最短子序列v,使得依次经过v中所有点的路径为p. 思路: 题意其实就是让我们求路径上的一些关键点v,对于所有的关键点:vi ...

  7. ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)

    这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在 ...

  8. 模板C++ 03图论算法 2最短路之全源最短路(Floyd)

    3.2最短路之全源最短路(Floyd) 这个算法用于求所有点对的最短距离.比调用n次SPFA的优点在于代码简单,时间复杂度为O(n^3).[无法计算含有负环的图] 依次扫描每一点(k),并以该点作为中 ...

  9. 最短路 - floyd算法

    floyd算法是多源最短路算法 也就是说,floyd可以一次跑出所以点两两之间的最短路 floyd类似动态规划 如下图: 用橙色表示边权,蓝色表示最短路 求最短路的流程是这样的: 先把点1到其他点的最 ...

随机推荐

  1. (为容器分配独立IP方法二)通过虚拟IP实现docker宿主机增加对外IP接口

    虚拟IP.何为虚拟IP,就是一个未分配给真实主机的IP,也就是说对外提供数据库服务器的主机除了有一个真实IP外还有一个虚IP,使用这两个IP中的任意一个都可以连接到这台主机,所有项目中数据库链接一项配 ...

  2. 使用Google BBR加速 VPS

    0X00 预备知识 在使用Google BBR之前,我们首先要了解它是什么. 了解计算机网络的人都知道,在TCP连接中,由于需要维持连接的可靠性,引入了拥塞控制和流量管理的方法.Google BBR就 ...

  3. Bitstream or PCM?

    背景 提问 讨论精选 一 二 三 四 五 最后 电视上同轴输出的做法. 背景 USB通道下播放声音格式为AAC的视频文件,同轴输出设置为Auto,功放没有声音,设置成PCM,有声音. 提问 Auto/ ...

  4. 安卓权威编程指南-笔记(第22章 深入学习intent和任务)

    本章,我们会使用隐式intent创建一个替换android默认启动器的应用.名为NerdLauncher. NerdLauncher应用能列出设备上的其他应用,点选任意列表项会启动相应应用. 1. 解 ...

  5. Web网页布局的主要方式

    一.静态布局(static layout) 即传统Web设计,网页上的所有元素的尺寸一律使用px作为单位. 1.布局特点 不管浏览器尺寸具体是多少,网页布局始终按照最初写代码时的布局来显示.常规的pc ...

  6. JVM—垃圾回收GC算法

    1 GC算法简介 算法 特点 标记-清除 分为"标记"和"清除"两个阶段 复制 可以解决效率问题,将可用的内存按容量划分为大小相等的两块. 标记-整理 先标记. ...

  7. Python基础-两个乒乓球队进行比赛,各出三人。

    两个乒乓球队进行比赛,各出三人.甲队为a,b,c三人,乙队为x,y,z三人.已抽签决定比赛名单.有人向队员打听比赛的名单.a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单. L1 = [ ...

  8. 《一步步成为 Hacker_Day 01》

    环境搭建 传统运行模式 - 一台机器同时只能运行一个操作系统 |:----------|----------:| | 应用程序 | 应用程序 | |:----------|----------:| | ...

  9. 简单的节流函数throttle

    在实际项目中,总会遇到一些函数频繁调用的情况,比如window.resize,mouseover,上传进度类似的触发频率比较高的函数,造成很大的性能损耗,这里可以使用节流函数来进行性能优化,主要是限制 ...

  10. Await/Async

    Async其实就是Generator函数的语法糖. 啥是语法糖?就是一种更容易让人理解,代码可读性更高的另外一种语法. const asyncRead = async function(){ cons ...