Submission #958640

# Submission time Handle Problem Language Result Execution time Memory
958640 2024-04-06T09:24:13 Z yeediot Portals (BOI14_portals) C++17
100 / 100
651 ms 111828 KB
#include<bits/stdc++.h>
using namespace std;
#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=1e9;
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:41:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28252 KB Output is correct
2 Correct 13 ms 28252 KB Output is correct
3 Correct 13 ms 28248 KB Output is correct
4 Correct 14 ms 28252 KB Output is correct
5 Correct 13 ms 28252 KB Output is correct
6 Correct 14 ms 28248 KB Output is correct
7 Correct 16 ms 28252 KB Output is correct
8 Correct 13 ms 28248 KB Output is correct
9 Correct 18 ms 28276 KB Output is correct
10 Correct 17 ms 28252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28248 KB Output is correct
2 Correct 13 ms 28252 KB Output is correct
3 Correct 14 ms 28252 KB Output is correct
4 Correct 14 ms 28504 KB Output is correct
5 Correct 14 ms 28200 KB Output is correct
6 Correct 17 ms 28252 KB Output is correct
7 Correct 14 ms 28252 KB Output is correct
8 Correct 14 ms 28248 KB Output is correct
9 Correct 14 ms 30552 KB Output is correct
10 Correct 14 ms 30552 KB Output is correct
11 Correct 14 ms 30612 KB Output is correct
12 Correct 14 ms 30556 KB Output is correct
13 Correct 14 ms 30556 KB Output is correct
14 Correct 13 ms 28252 KB Output is correct
15 Correct 14 ms 30596 KB Output is correct
16 Correct 13 ms 28332 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 13 ms 28360 KB Output is correct
4 Correct 15 ms 28376 KB Output is correct
5 Correct 23 ms 34652 KB Output is correct
6 Correct 29 ms 34900 KB Output is correct
7 Correct 23 ms 34908 KB Output is correct
8 Correct 21 ms 34904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28248 KB Output is correct
2 Correct 13 ms 28248 KB Output is correct
3 Correct 14 ms 28252 KB Output is correct
4 Correct 13 ms 28220 KB Output is correct
5 Correct 16 ms 28252 KB Output is correct
6 Correct 14 ms 28252 KB Output is correct
7 Correct 14 ms 28500 KB Output is correct
8 Correct 13 ms 28252 KB Output is correct
9 Correct 15 ms 30556 KB Output is correct
10 Correct 14 ms 30552 KB Output is correct
11 Correct 14 ms 30676 KB Output is correct
12 Correct 20 ms 30556 KB Output is correct
13 Correct 16 ms 30556 KB Output is correct
14 Correct 22 ms 34496 KB Output is correct
15 Correct 24 ms 34652 KB Output is correct
16 Correct 23 ms 34728 KB Output is correct
17 Correct 28 ms 34652 KB Output is correct
18 Correct 25 ms 35160 KB Output is correct
19 Correct 22 ms 35592 KB Output is correct
20 Correct 24 ms 35676 KB Output is correct
21 Correct 22 ms 34652 KB Output is correct
22 Correct 23 ms 34652 KB Output is correct
23 Correct 24 ms 34652 KB Output is correct
24 Correct 26 ms 35544 KB Output is correct
25 Correct 14 ms 28252 KB Output is correct
26 Correct 15 ms 30616 KB Output is correct
27 Correct 14 ms 28344 KB Output is correct
28 Correct 21 ms 34908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28248 KB Output is correct
2 Correct 17 ms 28248 KB Output is correct
3 Correct 16 ms 28252 KB Output is correct
4 Correct 14 ms 28252 KB Output is correct
5 Correct 14 ms 28252 KB Output is correct
6 Correct 15 ms 28376 KB Output is correct
7 Correct 14 ms 28376 KB Output is correct
8 Correct 14 ms 28252 KB Output is correct
9 Correct 15 ms 30620 KB Output is correct
10 Correct 15 ms 30812 KB Output is correct
11 Correct 14 ms 30688 KB Output is correct
12 Correct 14 ms 30552 KB Output is correct
13 Correct 15 ms 30700 KB Output is correct
14 Correct 24 ms 34652 KB Output is correct
15 Correct 28 ms 34652 KB Output is correct
16 Correct 23 ms 34908 KB Output is correct
17 Correct 24 ms 34724 KB Output is correct
18 Correct 25 ms 35160 KB Output is correct
19 Correct 22 ms 35668 KB Output is correct
20 Correct 33 ms 35752 KB Output is correct
21 Correct 23 ms 34648 KB Output is correct
22 Correct 23 ms 34652 KB Output is correct
23 Correct 23 ms 34652 KB Output is correct
24 Correct 375 ms 93008 KB Output is correct
25 Correct 651 ms 111828 KB Output is correct
26 Correct 334 ms 111736 KB Output is correct
27 Correct 405 ms 111468 KB Output is correct
28 Correct 325 ms 85588 KB Output is correct
29 Correct 336 ms 86112 KB Output is correct
30 Correct 316 ms 87148 KB Output is correct
31 Correct 26 ms 35496 KB Output is correct
32 Correct 544 ms 111188 KB Output is correct
33 Correct 16 ms 28248 KB Output is correct
34 Correct 14 ms 30552 KB Output is correct
35 Correct 392 ms 93700 KB Output is correct
36 Correct 13 ms 28252 KB Output is correct
37 Correct 21 ms 34896 KB Output is correct
38 Correct 241 ms 90716 KB Output is correct
39 Correct 274 ms 78052 KB Output is correct