Submission #958640

#TimeUsernameProblemLanguageResultExecution timeMemory
958640yeediotPortals (BOI14_portals)C++17
100 / 100
651 ms111828 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...