1204C - Anna, Svyatoslav and Maps

1204C - Anna, Svyatoslav and Maps

看别人题解才知道题目是怎么回事。。。

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(ll  i=a;i<=b;i++)
#define dec(i,a,b)      for(ll  i=a;i>=b;i--)
#define forn(i, n)      for(ll i = 0; i < int(n); i++)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846;
const double eps = 1e-6;
const int mod = 998244353;
const int N = 1e6 + 5;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
ll lcm(ll m, ll n)
{
    return m * n / gcd(m, n);
}
bool prime(int x) {
    if (x < 2) return false;
    for (int i = 2; i * i <= x; ++i) {
        if (x % i == 0) return false;
    }
    return true;
}
inline int qpow(int x, ll n) {
    int r = 1;
    while (n > 0) {
        if (n & 1) r = 1ll * r * x % mod;
        n >>= 1; x = 1ll * x * x % mod;
    }
    return r;
}
inline int add(int x, int y) {
    return ((x%mod)+(y%mod))%mod;
}
inline int sub(int x, int y) {
    x -= y;
    return x < 0 ? x += mod : x;
}
inline int mul(int x, int y) {
    return (1ll * (x %mod) * (y % mod))%mod;
}
inline int Inv(int x) {
    return qpow(x, mod - 2);
}

int main()
{
    int n;
    cin >> n;
    vector<vector<int>> g(n+1,vector<int>(n+1,inf));
    rep(i, 1, n)
    {
        string s;
        cin >> s;
        forn(j, n)
        {
            if (s[j] == '1')
            {
                g[i][j + 1] = 1;
            }
        }
    }
    rep(i, 0, n)
        g[i][i] = 0;
    int len;
    cin >> len;
    vector<int> a(len + 1);
    rep(i, 1, len)
        cin >> a[i];
    rep(i, 1, n)
    {
        rep(j, 1, n)
        {
            rep(k, 1, n)
            {
                g[j][k] = min(g[j][i] + g[i][k], g[j][k]);
            }
        }
    }
    vector<int> res;
    res.push_back(a[1]);
    int dis = 0;
    rep(i, 2, len)
    {
        dis += g[a[i - 1]][a[i]];
        if (dis > g[res.back()][a[i]])
        {
            res.push_back(a[i - 1]);
            dis = g[res.back()][a[i]];
        }
    }
    res.push_back(a[len]);
    cout << res.size() << endl;
    rep(i, 0, res.size()-1)
        cout << res[i] << " ";
    return 0;
}

 

posted @ 2020-07-10 23:44  DeaL57  阅读(148)  评论(0编辑  收藏  举报