답안 #651134

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
651134 2022-10-17T10:34:51 Z lukameladze Soccer (JOI17_soccer) C++14
5 / 100
430 ms 34272 KB
# include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define int long long
#define pii pair <int, int>
#define pb push_back
const int N = 1000, inf = 1e18;
int t,n,a,b,c,h,w,x[N],y[N],dx[] = {0,0,1,-1}, dy[] = {1,-1,0,0};
int dp[N][N][10],dist[N][N];
int check(int x, int y) {
    return (!(x <= 0 || y <= 0 || x > h || y > w));
}
void bfs() {
    queue <pii> q;
    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            dist[i][j] = inf;
        }
    }
    for (int i = 1; i <= n; i++) {
        dist[x[i]][y[i]] = 0;
        q.push({x[i],y[i]});
    }
    while (!q.empty()) {
        int x = q.front().f;
        int y = q.front().s;
        q.pop();
        for (int i = 0; i <= 3; i++) {
            int nx = x + dx[i]; int ny = y + dy[i];
            if (check(nx,ny) && dist[nx][ny] == inf) {
                dist[nx][ny] = dist[x][y] + 1;
                q.push({nx,ny});
            }
        }
    }
}
void dijkstra() {
    priority_queue < pair <pii, pii > > q;
    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++ ){
            for (int k = 0; k <= 4; k++) {
                dp[i][j][k] = inf;
            }
        }
    }
    q.push({{0,4},{x[1],y[1]}});
    dp[x[1]][y[1]][4] = 0;
    while (!q.empty()) {
        int dis = -q.top().f.f;
        int dir = q.top().f.s;
        int x = q.top().s.f;
        int y = q.top().s.s;
        q.pop();
        if (dp[x][y][dir] < dis) continue;
        
        if (dir == 4) {
            for (int i = 0; i <= 3; i++) {
                int nx = x + dx[i]; int ny = y + dy[i];
                if (dp[x][y][i] > dp[x][y][4] + b) {
                    dp[x][y][i] = dp[x][y][4] + b;
                    q.push({{-dp[x][y][i], i},{x,y}});
                }
                
                if (check(nx,ny) && dp[nx][ny][i] > dp[x][y][dir] + b + c){
                    dp[nx][ny][i] = dp[x][y][dir] + b + c;
                    q.push({{-dp[nx][ny][i], i},{nx,ny}});
                }
            }
            for (int i = 0; i <= 3; i++) {
                int nx = x + dx[i]; int ny = y + dy[i];
                if (check(nx,ny) && dp[nx][ny][4] > dp[x][y][dir] + c) {
                    dp[nx][ny][4] = dp[x][y][4] + c;
                    q.push({{-dp[nx][ny][4], 4},{nx,ny}});
                }
            }
        }
        else {
            if (dp[x][y][4] > dp[x][y][dir] + c*dist[x][y]) {
                dp[x][y][4] = dp[x][y][dir] + c*dist[x][y];
                q.push({{-dp[x][y][4], 4},{x,y}});
            }
            int nx = x + dx[dir];
            int ny = y + dy[dir];
            //if (x == 4 && y == 6 && dir == 2) cout<<nx<<" "<<ny<<"\n";
            if (dp[nx][ny][dir] > dp[x][y][dir] + a) {
                dp[nx][ny][dir] = dp[x][y][dir] + a;
                q.push({{-dp[nx][ny][dir], dir},{nx,ny}});
            }
        }
    }
    int ans = inf;
    cout<<dp[x[n]][y[n]][4]<<"\n";
}
main() {
    std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>h>>w; h++;w++;
    cin>>a>>b>>c;
    cin>>n;
    
    for (int i = 1; i <= n; i++) {
        cin>>x[i]>>y[i];
        x[i]++;
        y[i]++;
    }
    bfs();
    dijkstra();
}

Compilation message

soccer.cpp: In function 'void dijkstra()':
soccer.cpp:92:9: warning: unused variable 'ans' [-Wunused-variable]
   92 |     int ans = inf;
      |         ^~~
soccer.cpp: At global scope:
soccer.cpp:95:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   95 | main() {
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 10192 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 326 ms 34188 KB Output is correct
4 Correct 338 ms 34232 KB Output is correct
5 Correct 80 ms 11364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 416 ms 34272 KB Output is correct
2 Correct 430 ms 34256 KB Output is correct
3 Correct 282 ms 29160 KB Output is correct
4 Correct 291 ms 34172 KB Output is correct
5 Correct 284 ms 30036 KB Output is correct
6 Correct 287 ms 34212 KB Output is correct
7 Incorrect 3 ms 5972 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 10192 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 326 ms 34188 KB Output is correct
4 Correct 338 ms 34232 KB Output is correct
5 Correct 80 ms 11364 KB Output is correct
6 Correct 416 ms 34272 KB Output is correct
7 Correct 430 ms 34256 KB Output is correct
8 Correct 282 ms 29160 KB Output is correct
9 Correct 291 ms 34172 KB Output is correct
10 Correct 284 ms 30036 KB Output is correct
11 Correct 287 ms 34212 KB Output is correct
12 Incorrect 3 ms 5972 KB Output isn't correct
13 Halted 0 ms 0 KB -