[CF] codeforces_1301F_Super Jaber_ shortest

Face questions

codeforces1301F

answer

  • This question is a simple question Road
  • Two kinds of ways to walk
    • Mang past, walking distance from Manhattan
    • With super powers. It is certainly three sections: first to a color from the color to another color, another color from the target. Wherein each segment may be degraded.
  • We have to count three ways: Manhattan from a point to a color, a color to color.
  • A first random count, a second scan four times, on the basis of the second third of the Floyd

  • This stuff will be out what the problem was? Anyway, I was an overflow.

Code

#include<bits/stdc++.h>
#define LL long long
#define MAXN 1010
#define MAXK 41
using namespace std;
template<typename T>void Read(T &cn)
{
    char c;int sig = 1;
    while(!isdigit(c = getchar()))if(c == '-')sig = -1; cn = c-48;
    while(isdigit(c = getchar()))cn = cn*10+c-48; cn*=sig;
}
template<typename T>void Write(T cn)
{
    if(cn < 0) {putchar('-'); cn = 0-cn; }
    int wei = 0; T cm = 0; int cx = cn%10; cn/=10;
    while(cn)cm = cm*10+cn%10,cn/=10,wei++;
    while(wei--)putchar(cm%10+48),cm/=10;
    putchar(cx+48);
}
template<typename T>void Min(T &cn, T cm) {cn = cn < cm ? cn : cm; }
template<typename T>void Max(T &cn, T cm) {cn = cn < cm ? cm : cn; }
int n,m,k,q;
int col[MAXN+1][MAXN+1];
int ju[MAXN+1][MAXN+1][MAXK+1];
int a[MAXK*2+1][MAXK*2+1];
void nong(int cn1,int cn2,int cm1,int cm2,int ci1,int ci2)
{
    for(int i = cn1;i!=cn2;i+=ci1) for(int j = cm1;j!=cm2;j+=ci2)
    {
        for(int ij = 1;ij<=k;ij++) Min(ju[i][j][ij], min(ju[i][j-ci2][ij], ju[i-ci1][j][ij])+1);
    }
}
int main()
{
    Read(n); Read(m); Read(k);
    for(int i = 0;i<=n+1;i++) for(int j = 0;j<=m+1;j++) for(int ij = 1;ij<=k;ij++) ju[i][j][ij] = 100000000;
    for(int i = 1;i<=n;i++) for(int j = 1;j<=m;j++) Read(col[i][j]), ju[i][j][col[i][j]] = 0; 
    nong(1,n+1,1,m+1,1,1); nong(1,n+1,m,0,1,-1);
    nong(n,0,1,m+1,-1,1); nong(n,0,m,0,-1,-1);
    for(int i = 1;i<=k*2;i++) for(int j = 1;j<=k*2;j++) a[i][j] = k*3+m+n+1;
    for(int i = 1;i<=n;i++) for(int j = 1;j<=m;j++) for(int ij = 1;ij<=k;ij++) Min(a[col[i][j]+k][ij], ju[i][j][ij]);
    for(int i = 1;i<=k;i++) a[i][i+k] = 1, a[i][i] = a[i+k][i+k] = 0;
    for(int ij = 1;ij<=k*2;ij++)
    for(int i = 1;i<=k*2;i++)
    for(int j = 1;j<=k*2;j++)
    Min(a[i][j], a[i][ij] + a[ij][j]);
    Read(q);
    for(int i = 1;i<=q;i++)
    {
        int bx1, by1, bx2, by2;
        Read(bx1); Read(by1); Read(bx2); Read(by2);
        int ans = abs(bx1-bx2) + abs(by1-by2);
        for(int j = 1;j<=k;j++) for(int ij = 1;ij<=k;ij++) Min(ans, ju[bx1][by1][j] + ju[bx2][by2][ij] + a[j][ij+k]);
        Write(ans); puts("");
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/czyarl/p/12397558.html