码迷,mamicode.com
首页 > 其他好文 > 详细

[最短路,floyd] Codeforces 1204C Anna, Svyatoslav and Maps

时间:2019-08-27 13:03:07      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:property   several   cin   alt   main   any   limit   not   tin   

题目: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 1i<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 (2n1002≤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 (2m1062≤m≤106) — the number of vertexes in the path.

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

Output

In the first line output a single integer kk (2km2≤k≤m) — the length of the shortest good subsequence. In the second line output kk integers v1v1, …, vkvk (1vin1≤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 1241−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 12341−2−3−4. Note that subsequences 141−4 and 1341−3−4 aren‘t good because in both cases the shortest path passing through the vertexes of these sequences is 1341−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 1241−2−4 and 1341−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节点才能保证最短路仍是原来的序列,否则最短路就会变短

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

 

[最短路,floyd] Codeforces 1204C Anna, Svyatoslav and Maps

标签:property   several   cin   alt   main   any   limit   not   tin   

原文地址:https://www.cnblogs.com/Railgun000/p/11417427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!