Submission #958639

# Submission time Handle Problem Language Result Execution time Memory
958639 2024-04-06T09:23:30 Z yeediot Portals (BOI14_portals) C++17
100 / 100
628 ms 182404 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
#define __lg(x) 63-__builtin_clzll(x)
#define pow2(x) (1LL<<x)
void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef local
void setio(){freopen("/Users/iantsai/Library/Mobile Documents/com~apple~CloudDocs/cpp/Empty.md","r",stdin);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}
struct line{
    int a,b;
    int operator()(const int x)const{
        return a*x+b;
    }
};
bool check(line l1,line l2,line l3){
    return (l3.b-l2.b)*(l1.a-l2.a)<=(l2.b-l1.b)*(l2.a-l3.a);
}
const int mxn=1005;
const int inf=1e18;
char g[mxn][mxn];
int pdis[mxn][mxn];
int dis[mxn][mxn];
int n,m;
vector<vector<vector<pii>>>adj(mxn,vector<vector<pii>>(mxn));
bool wall(int i,int j){
    return (i==0 or i==n+1 or j==0 or j==m+1 or g[i][j]=='#');
}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
pii st,ed;
void pre(){
    for(int i=1;i<=n;i++){
        int last=0;
        for(int j=1;j<=m;j++){
            if(g[i][j]=='#'){
                last=j;
                continue;
            }
            adj[i][j].pb({i,last+1});
            for(int k=0;k<4;k++){
                adj[i][j].pb({i+dx[k],j+dy[k]});
            }
        }
        last=m+1;
        for(int j=m;j>=1;j--){
            if(g[i][j]=='#'){
                last=j;
                continue;
            }
            adj[i][j].pb({i,last-1});
        }
    }
    for(int j=1;j<=m;j++){
        int last=0;
        for(int i=1;i<=n;i++){
            if(g[i][j]=='#'){
                last=i;
                continue;
            }
            adj[i][j].pb({last+1,j});
        }
        last=n+1;
        for(int i=n;i>=1;i--){
            if(g[i][j]=='#'){
                last=i;
                continue;
            }
            adj[i][j].pb({last-1,j});
        }
    }
}
void calc_near_portal(){
    queue<pii>q;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            pdis[i][j]=inf;
            if(wall(i-1,j) or wall(i+1,j) or wall(i,j-1) or wall(i,j+1)){
                pdis[i][j]=0;
                q.push({i,j});
            }
        }
    }
    while(sz(q)){
        auto [x,y]=q.front();
        q.pop();
        for(auto [nx,ny]:adj[x][y]){
            if(pdis[nx][ny]==inf){
                pdis[nx][ny]=pdis[x][y]+1;
                q.push({nx,ny});
            }
        }
    }
}
bool adjacent(int x,int y,int nx,int ny){
    return abs(nx-x)==1 or abs(ny-y)==1;
}
void dij(){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            dis[i][j]=inf;
        }
    }
    dis[st.F][st.S]=0;
    priority_queue<tuple<int,int,int>,vector<tuple<int,int,int>>,greater<tuple<int,int,int>>>pq;
    pq.push({0,st.F,st.S});
    while(sz(pq)){
        auto [d,x,y]=pq.top();
        pq.pop();
        if(x==ed.F and y==ed.S){
            cout<<d<<'\n';
            break;
        }
        if(d>dis[x][y])continue;
        for(auto [nx,ny]:adj[x][y]){
            int cost=d;
            if(adjacent(x,y,nx,ny)){
                cost+=1;
            }
            else{
                cost+=pdis[x][y]+1;
            }
            if(cost<dis[nx][ny]){
                dis[nx][ny]=cost;
                pq.push({cost,nx,ny});
            }
        }
    }
}
signed main(){
    setio();
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>g[i][j];
            pdis[i][j]=inf;
            if(g[i][j]=='S'){
                st={i,j};
            }
            if(g[i][j]=='C'){
                ed={i,j};
            }
        }
    }
    pre();
    calc_near_portal();
    dij();
}
 /*
 input:
 
 */















 

Compilation message

portals.cpp: In function 'void setIO(std::string)':
portals.cpp:42:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
portals.cpp:43:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28248 KB Output is correct
2 Correct 14 ms 28252 KB Output is correct
3 Correct 13 ms 28252 KB Output is correct
4 Correct 13 ms 28252 KB Output is correct
5 Correct 15 ms 28252 KB Output is correct
6 Correct 13 ms 28252 KB Output is correct
7 Correct 14 ms 28252 KB Output is correct
8 Correct 14 ms 28252 KB Output is correct
9 Correct 14 ms 28108 KB Output is correct
10 Correct 13 ms 28252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28248 KB Output is correct
2 Correct 15 ms 28252 KB Output is correct
3 Correct 14 ms 28252 KB Output is correct
4 Correct 13 ms 28252 KB Output is correct
5 Correct 13 ms 28288 KB Output is correct
6 Correct 13 ms 28252 KB Output is correct
7 Correct 14 ms 28248 KB Output is correct
8 Correct 14 ms 28252 KB Output is correct
9 Correct 14 ms 31064 KB Output is correct
10 Correct 15 ms 30808 KB Output is correct
11 Correct 15 ms 30688 KB Output is correct
12 Correct 14 ms 30556 KB Output is correct
13 Correct 15 ms 30556 KB Output is correct
14 Correct 13 ms 28356 KB Output is correct
15 Correct 14 ms 30556 KB Output is correct
16 Correct 13 ms 28252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28252 KB Output is correct
2 Correct 13 ms 28364 KB Output is correct
3 Correct 14 ms 28248 KB Output is correct
4 Correct 13 ms 28248 KB Output is correct
5 Correct 25 ms 36188 KB Output is correct
6 Correct 25 ms 36540 KB Output is correct
7 Correct 26 ms 36768 KB Output is correct
8 Correct 21 ms 36696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 28252 KB Output is correct
2 Correct 13 ms 28252 KB Output is correct
3 Correct 15 ms 28508 KB Output is correct
4 Correct 13 ms 28252 KB Output is correct
5 Correct 14 ms 28252 KB Output is correct
6 Correct 13 ms 28252 KB Output is correct
7 Correct 14 ms 28252 KB Output is correct
8 Correct 13 ms 28248 KB Output is correct
9 Correct 15 ms 30812 KB Output is correct
10 Correct 15 ms 30812 KB Output is correct
11 Correct 17 ms 30556 KB Output is correct
12 Correct 15 ms 30808 KB Output is correct
13 Correct 14 ms 30556 KB Output is correct
14 Correct 23 ms 36184 KB Output is correct
15 Correct 29 ms 36516 KB Output is correct
16 Correct 24 ms 36700 KB Output is correct
17 Correct 25 ms 36700 KB Output is correct
18 Correct 26 ms 37208 KB Output is correct
19 Correct 22 ms 37980 KB Output is correct
20 Correct 28 ms 38236 KB Output is correct
21 Correct 24 ms 36444 KB Output is correct
22 Correct 27 ms 36560 KB Output is correct
23 Correct 25 ms 36444 KB Output is correct
24 Correct 28 ms 37976 KB Output is correct
25 Correct 14 ms 28252 KB Output is correct
26 Correct 14 ms 30556 KB Output is correct
27 Correct 13 ms 28248 KB Output is correct
28 Correct 20 ms 36800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28328 KB Output is correct
2 Correct 13 ms 28252 KB Output is correct
3 Correct 14 ms 28252 KB Output is correct
4 Correct 13 ms 28248 KB Output is correct
5 Correct 13 ms 28252 KB Output is correct
6 Correct 14 ms 28172 KB Output is correct
7 Correct 15 ms 28248 KB Output is correct
8 Correct 14 ms 28252 KB Output is correct
9 Correct 15 ms 30596 KB Output is correct
10 Correct 15 ms 30812 KB Output is correct
11 Correct 18 ms 30556 KB Output is correct
12 Correct 14 ms 30552 KB Output is correct
13 Correct 14 ms 30628 KB Output is correct
14 Correct 25 ms 36264 KB Output is correct
15 Correct 26 ms 36324 KB Output is correct
16 Correct 23 ms 36696 KB Output is correct
17 Correct 25 ms 36696 KB Output is correct
18 Correct 25 ms 37212 KB Output is correct
19 Correct 22 ms 38196 KB Output is correct
20 Correct 25 ms 38236 KB Output is correct
21 Correct 24 ms 36696 KB Output is correct
22 Correct 23 ms 36444 KB Output is correct
23 Correct 24 ms 36444 KB Output is correct
24 Correct 394 ms 150544 KB Output is correct
25 Correct 628 ms 182404 KB Output is correct
26 Correct 323 ms 182076 KB Output is correct
27 Correct 455 ms 182092 KB Output is correct
28 Correct 319 ms 136440 KB Output is correct
29 Correct 346 ms 138248 KB Output is correct
30 Correct 335 ms 139780 KB Output is correct
31 Correct 26 ms 37980 KB Output is correct
32 Correct 601 ms 181704 KB Output is correct
33 Correct 14 ms 28248 KB Output is correct
34 Correct 15 ms 30552 KB Output is correct
35 Correct 431 ms 151852 KB Output is correct
36 Correct 15 ms 28252 KB Output is correct
37 Correct 23 ms 36700 KB Output is correct
38 Correct 249 ms 145716 KB Output is correct
39 Correct 297 ms 121608 KB Output is correct