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
inputstandard input
outputstandard output
The main characters have been omitted to be short.
You are given a directed unweighted graph without loops with n vertexes and a path in it (that path is not necessary simple) given by a sequence p1,p2,…,pm of m vertexes; for each 1≤i<m there is an arc from pi 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 a is a subsequence of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements. It is obvious that the sequence p 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 n (2≤n≤100) — the number of vertexes in a graph.
The next n lines define the graph by an adjacency matrix: the j-th character in the i-st line is equal to 1 if there is an arc from vertex i to the vertex j else it is equal to 0. It is guaranteed that the graph doesn't contain loops.
The next line contains a single integer m (2≤m≤106) — the number of vertexes in the path.
The next line contains m 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 pi to pi+1.
Output
In the first line output a single integer k (2≤k≤m) — the length of the shortest good subsequence. In the second line output k 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
inputCopy
4
0110
0010
0001
1000
4
1 2 3 4
outputCopy
3
1 2 4
inputCopy
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
outputCopy
11
1 2 4 2 4 2 4 2 4 2 4
inputCopy
3
011
101
110
7
1 2 3 1 3 2 1
outputCopy
7
1 2 3 1 3 2 1
inputCopy
4
0110
0001
0001
1000
3
1 2 4
outputCopy
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个节点的有向图,
和一个序列p,
让你找一个最小的序列v,使其v[1]=p[1] ,v[end]=p[end] ,并且 v 中节点再遍历的过程中,p序列是最短路序列之一。
思路:
用Floyd 算法,nnn 算出任意两个的最短路径。
然后处理p序列,
以一个开始位st 向后 找节点now 是否满足 now -st 满足 p[st] 到 p[now] 的最短路径距离。
如果满足就把now加入一个deque中待用,同时删除掉当前deque中已有的数,
否则就用deque中的数代替st,重复此操作。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
char s[105][105];
int n;
int m;
int a[maxn];
int cnt[105][105];
int dis[105][105];
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
scanf("%d", &n);
repd(i, 1, n)
{
scanf("%s", s[i] + 1);
}
scanf("%d", &m);
repd(i, 1, m)
{
scanf("%d", &a[i]);
}
repd(i, 1, n)
{
repd(j, 1, n)
{
dis[i][j] = inf;
}
dis[i][i] = 0;
}
repd(i, 1, n)
{
repd(j, 1, n)
{
// cout<<s[i][j]<<" ";
if (s[i][j] == '1')
{
dis[i][j] = 1;
}
}
// cout<<endl;
}
repd(k, 1, n)
{
repd(i, 1, n)
{
repd(j, 1, n)
{
if (dis[i][k] + dis[k][j] < dis[i][j])
{
dis[i][j] = dis[i][k] + dis[k][j];
}
}
}
}
// repd(i,1,n)
// {
// repd(j,1,n)
// {
// cout<<dis[i][j]<<" ";
// }
// cout<<endl;
// }
deque<int> q;
while (!q.empty())
{
q.pop_back();
}
std::vector<int> ans;
ans.clear();
int now = 2;
int start = 1;
while (now <= m)
{
int dist = now - start;
if (dist == dis[a[start]][a[now]])
{
if (!q.empty())
{
q.pop_front();
}
q.push_back(now);
now++;
} else
{
ans.push_back(a[start]);
if (!q.empty())
{
start = q.front();
q.pop_front();
}
}
// cout << sz(q) << endl;
}
ans.push_back(a[start]);
if (ans[sz(ans) - 1] != a[m])
{
ans.push_back(a[m]);
}
cout << sz(ans) << endl;
for (auto x : ans)
{
cout << x << " ";
}
cout << endl;
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
Codeforces Round #581 (Div. 2) C. Anna, Svyatoslav and Maps (Floyd 算法,最短路)的更多相关文章
- Codeforces Round #581 (Div. 2)-E. Natasha, Sasha and the Prefix Sums-动态规划+组合数学
Codeforces Round #581 (Div. 2)-E. Natasha, Sasha and the Prefix Sums-动态规划+组合数学 [Problem Description] ...
- Codeforces Round #581 (Div. 2)
A:暴力. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm& ...
- 01串LIS(固定串思维)--Kirk and a Binary String (hard version)---Codeforces Round #581 (Div. 2)
题意:https://codeforc.es/problemset/problem/1204/D2 给你一个01串,如:0111001100111011101000,让你改这个串(使0尽可能多,任意 ...
- D2. Kirk and a Binary String (hard version) D1 Kirk and a Binary String (easy version) Codeforces Round #581 (Div. 2) (实现,构造)
D2. Kirk and a Binary String (hard version) time limit per test1 second memory limit per test256 meg ...
- Codeforces Round #581 (Div. 2) B. Mislove Has Lost an Array (贪心)
B. Mislove Has Lost an Array time limit per test1 second memory limit per test256 megabytes inputsta ...
- Codeforces Round #581 (Div. 2)A BowWow and the Timetable (思维)
A. BowWow and the Timetable time limit per test1 second memory limit per test256 megabytes inputstan ...
- Codeforces Round #581 (Div. 2)D(思维,构造,最长非递减01串)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;char s[100007];int main ...
- Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- codeforces 1204C Anna, Svyatoslav and Maps(floyd+dp)
题目链接:http://codeforces.com/problemset/problem/1204/C 给定一组序列,P1,P2,P3...Pm,这是一组合法路径的序列,即任意的Pi和Pi+1之间有 ...
随机推荐
- 2、puppet资源详解
定义puppet资源 puppet资源抽象 资源定义 每一个资源有一个type.一个title和一个属性集合(attribute) type {'title': //type表示资源类型, ti ...
- 麦香牛肉(dp 、数论)
麦香牛肉 时间限制: 1 Sec 内存限制: 128 MB 题目描述 农夫约翰的奶牛几乎要武装暴动,因为他们听说麦当劳要推出新产品麦香牛肉.奶牛们要尽力阻止这种产品的上市.他们研究了一种“劣等包装” ...
- [笔记] 如何在Windows上同时打开多个钉钉?
钉钉防多开原理 常规程序防止多开,会使用Mutex. 钉钉是常规程序,所以也是使用Mutex. 查找钉钉使用的Mutex 工具:ProcessExplorer.exe 启动钉钉,然后使用Process ...
- Delphi组件编辑器
看到Dev中的cxGrid组件的编辑器很强大,于是很想探究一下,跟踪cxGrid的代码比较麻烦,但原理大概知道一二.首先来研究一下设计器双击cxGrid弹出一个编辑窗体,选择窗体中的一个内容后,属性编 ...
- iOS服务器数据请求"汉字编码"问题
下面记录一下数据请求问题: 1.不知道大家有木有遇到过,当数据请求的URL带有汉字的时候,请求数据肯定会报404错误,也就是参数或者是接口URL错误<虽然说404,500等错误一般都是服务器问题 ...
- 应用安全 - 工具 - Jmeter - 漏洞 - 汇总
CVE-2018-1297 Date 类型 rmi 反序列化导致远程代码执行 影响范围
- [Python3] 033 异常
目录 异常 1. 简介 2. 异常的分类 3. 出现异常小例子 例子 4. 异常处理 5. 解决异常小例子 5.1 例子1 5.2 例子2 5.3 例子3 5.4 例子4 6. 手动引发异常 6.1 ...
- 非旋(fhq)Treap小记
前置知识:二叉搜索树 以下摘自 ↑: 二叉搜索树每次操作访问O(深度)个节点. 在刻意构造的数据中,树的形态会被卡成一条链,于是复杂度爆炸 它的复杂度与直接暴力删除类似. 但二叉搜索树扩展性强.更复杂 ...
- # 深圳杯D题爬取电视收视率排行榜
目录 深圳杯D题爬取电视收视率排行榜 站点分析 代码实现 深圳杯D题爬取电视收视率排行榜 站点分析 http://www.tvtv.hk/archives/category/tv 每天的排行版通过静态 ...
- php aes加解密,mcrypt_encrypt 和openssl_encrypt
php7.1以下版本使用 /* * mcrypt_encrypt 加密 * php7.1开始被丢弃 可以使用openssl_encrypt * */ function aes_encrypt($con ...