답안 #395149

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
395149 2021-04-27T22:12:12 Z duality Wall (CEOI14_wall) C++11
100 / 100
320 ms 21916 KB
#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------

int grid[401][401],ver[401][401],hor[401][401];
LLI dist[802][802],ver2[802][802],hor2[802][802];
int visited[401][401],parent[401][401];
priority_queue<pair<LLI,pii> > H;
int main() {
    int i,j;
    int N,M;
    scanf("%d %d",&N,&M);
    for (i = 0; i < N; i++) {
        for (j = 0; j < M; j++) scanf("%d",&grid[i][j]);
    }
    for (i = 0; i < N; i++) {
        for (j = 0; j <= M; j++) scanf("%d",&ver[i][j]);
    }
    for (i = 0; i <= N; i++) {
        for (j = 0; j < M; j++) scanf("%d",&hor[i][j]);
    }

    for (i = 0; i <= N; i++) {
        for (j = 0; j <= M; j++) dist[i][j] = 1e18;
    }
    dist[0][0] = 0,H.push(mp(0,mp(0,0)));
    while (!H.empty()) {
        LLI d = -H.top().first;
        int x = H.top().second.first;
        int y = H.top().second.second;
        H.pop();

        if ((x > 0) && (d+hor[y][x-1] < dist[y][x-1])) {
            dist[y][x-1] = d+hor[y][x-1],parent[y][x-1] = 2;
            H.push(mp(-dist[y][x-1],mp(x-1,y)));
        }
        if ((x < M) && (d+hor[y][x] < dist[y][x+1])) {
            dist[y][x+1] = d+hor[y][x],parent[y][x+1] = 0;
            H.push(mp(-dist[y][x+1],mp(x+1,y)));
        }
        if ((y > 0) && (d+ver[y-1][x] < dist[y-1][x])) {
            dist[y-1][x] = d+ver[y-1][x],parent[y-1][x] = 3;
            H.push(mp(-dist[y-1][x],mp(x,y-1)));
        }
        if ((y < N) && (d+ver[y][x] < dist[y+1][x])) {
            dist[y+1][x] = d+ver[y][x],parent[y+1][x] = 1;
            H.push(mp(-dist[y+1][x],mp(x,y+1)));
        }
    }
    for (i = 0; i < 2*(N+1); i++) {
        for (j = 0; j < 2*(M+1); j++) {
            if ((i & 1) && (j & 1)) ver2[i][j] = ver[i/2][j/2],hor2[i][j] = hor[i/2][j/2];
            else if (i & 1) ver2[i][j] = ver[i/2][j/2];
            else if (j & 1) hor2[i][j] = hor[i/2][j/2];
        }
    }
    hor2[0][0] = ver2[0][0] = 1e18;
    for (i = 0; i < N; i++) {
        for (j = 0; j < M; j++) {
            if (grid[i][j]) {
                ver2[2*i][2*j+1] = ver2[2*i][2*j+2] = ver2[2*i+2][2*j+1] = ver2[2*i+2][2*j+2] = 1e18;
                hor2[2*i+1][2*j] = hor2[2*i+2][2*j] = hor2[2*i+1][2*j+2] = hor2[2*i+2][2*j+2] = 1e18;
                int x = j,y = i;
                while (((x > 0) || (y > 0)) && !visited[y][x]) {
                    visited[y][x] = 1;
                    if (parent[y][x] == 0) {
                        ver2[2*y][2*x] = ver2[2*y][2*x-1] = 1e18;
                        x--;
                    }
                    else if (parent[y][x] == 1) {
                        hor2[2*y][2*x] = hor2[2*y-1][2*x] = 1e18;
                        y--;
                    }
                    else if (parent[y][x] == 2) {
                        ver2[2*y][2*x+1] = ver2[2*y][2*x+2] = 1e18;
                        x++;
                    }
                    else {
                        hor2[2*y+1][2*x] = hor2[2*y+2][2*x] = 1e18;
                        y++;
                    }
                }
            }
        }
    }
    for (i = 0; i < 2*(N+1); i++) {
        for (j = 0; j < 2*(M+1); j++) dist[i][j] = 1e18;
    }
    dist[0][1] = 0,H.push(mp(0,mp(1,0)));
    while (!H.empty()) {
        LLI d = -H.top().first;
        int x = H.top().second.first;
        int y = H.top().second.second;
        H.pop();

        if ((x > 0) && (d+hor2[y][x-1] < dist[y][x-1])) {
            dist[y][x-1] = d+hor2[y][x-1];
            H.push(mp(-dist[y][x-1],mp(x-1,y)));
        }
        if ((x < 2*M+1) && (d+hor2[y][x] < dist[y][x+1])) {
            dist[y][x+1] = d+hor2[y][x];
            H.push(mp(-dist[y][x+1],mp(x+1,y)));
        }
        if ((y > 0) && (d+ver2[y-1][x] < dist[y-1][x])) {
            dist[y-1][x] = d+ver2[y-1][x];
            H.push(mp(-dist[y-1][x],mp(x,y-1)));
        }
        if ((y < 2*N+1) && (d+ver2[y][x] < dist[y+1][x])) {
            dist[y+1][x] = d+ver2[y][x];
            H.push(mp(-dist[y+1][x],mp(x,y+1)));
        }
    }
    printf("%lld\n",dist[1][0]);

    return 0;
}

Compilation message

wall.cpp: In function 'int main()':
wall.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   65 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
wall.cpp:67:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   67 |         for (j = 0; j < M; j++) scanf("%d",&grid[i][j]);
      |                                 ~~~~~^~~~~~~~~~~~~~~~~~
wall.cpp:70:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   70 |         for (j = 0; j <= M; j++) scanf("%d",&ver[i][j]);
      |                                  ~~~~~^~~~~~~~~~~~~~~~~
wall.cpp:73:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   73 |         for (j = 0; j < M; j++) scanf("%d",&hor[i][j]);
      |                                 ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 588 KB Output is correct
2 Correct 1 ms 556 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 2 ms 1228 KB Output is correct
7 Correct 2 ms 1208 KB Output is correct
8 Correct 2 ms 1212 KB Output is correct
9 Correct 2 ms 1100 KB Output is correct
10 Correct 2 ms 1228 KB Output is correct
11 Correct 3 ms 1484 KB Output is correct
12 Correct 3 ms 1484 KB Output is correct
13 Correct 3 ms 1596 KB Output is correct
14 Correct 3 ms 1612 KB Output is correct
15 Correct 3 ms 1356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1612 KB Output is correct
2 Correct 3 ms 1740 KB Output is correct
3 Correct 3 ms 1612 KB Output is correct
4 Correct 4 ms 1616 KB Output is correct
5 Correct 3 ms 1732 KB Output is correct
6 Correct 3 ms 1612 KB Output is correct
7 Correct 3 ms 1720 KB Output is correct
8 Correct 3 ms 1612 KB Output is correct
9 Correct 3 ms 1604 KB Output is correct
10 Correct 4 ms 1592 KB Output is correct
11 Correct 3 ms 1740 KB Output is correct
12 Correct 3 ms 1612 KB Output is correct
13 Correct 3 ms 1740 KB Output is correct
14 Correct 3 ms 1612 KB Output is correct
15 Correct 3 ms 1612 KB Output is correct
16 Correct 3 ms 1612 KB Output is correct
17 Correct 3 ms 1740 KB Output is correct
18 Correct 3 ms 1612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 10028 KB Output is correct
2 Correct 51 ms 8468 KB Output is correct
3 Correct 46 ms 8408 KB Output is correct
4 Correct 56 ms 9448 KB Output is correct
5 Correct 70 ms 15008 KB Output is correct
6 Correct 58 ms 9712 KB Output is correct
7 Correct 150 ms 15748 KB Output is correct
8 Correct 141 ms 14600 KB Output is correct
9 Correct 116 ms 14160 KB Output is correct
10 Correct 160 ms 14932 KB Output is correct
11 Correct 168 ms 16564 KB Output is correct
12 Correct 37 ms 9968 KB Output is correct
13 Correct 130 ms 14484 KB Output is correct
14 Correct 157 ms 19132 KB Output is correct
15 Correct 243 ms 19072 KB Output is correct
16 Correct 220 ms 18708 KB Output is correct
17 Correct 282 ms 19896 KB Output is correct
18 Correct 320 ms 21916 KB Output is correct
19 Correct 303 ms 19020 KB Output is correct
20 Correct 227 ms 18368 KB Output is correct