Submission #106879

#TimeUsernameProblemLanguageResultExecution timeMemory
106879wilwxkPortals (BOI14_portals)C++11
0 / 100
65 ms47608 KiB
#include <bits/stdc++.h> using namespace std; const int MAXN=1e6+5; const int MAXNN=1e3+3; vector<int> g[MAXN], pes[MAXN]; set<pair<int, int> > s; int ori[MAXNN][MAXNN]; int parede[5][MAXNN][MAXNN]; int dist[MAXN]; int n, nn, mm, star, chegada; int quem(int i, int j) { return (i-1)*mm+j; } void liga(int a, int b, int c, int k) { if(a==b) return; // printf("%d liga %d c/ %d\n", a, b, c); g[a].push_back(b); if(k) g[b].push_back(a); pes[a].push_back(c); if(k) pes[b].push_back(c); } void dijkstra(int star) { for(int i=1; i<=n; i++) { dist[i]=1e9+9; if(i==star) dist[i]=0; s.insert({dist[i], i}); } while(!s.empty()) { int cur=s.begin()->second; if(cur==chegada) return; // printf("cur %d // %d\n", cur, dist[cur]); s.erase(s.begin()); for(int i=0; i<g[cur].size(); i++) { int viz=g[cur][i]; int peso=pes[cur][i]; // printf(" // %d > %d\n", viz, peso); if(dist[viz]>dist[cur]+peso) { s.erase({dist[viz], viz}); dist[viz]=dist[cur]+peso; s.insert({dist[viz], viz}); } } } } int main() { scanf("%d %d", &nn, &mm); for(int i=1; i<=nn; i++) { char cc[MAXNN]; scanf(" %s", &cc[1]); for(int j=1; j<=mm; j++) { char c=cc[j]; if(c=='#') ori[i][j]=1; else ori[i][j]=0; if(c=='S') star=quem(i, j); if(c=='C') chegada=quem(i, j); } } for(int i=1; i<=nn; i++) { int ult=1; for(int j=1; j<=mm; j++) { if(ori[i][j]!=0) { ult=j+1; continue; } parede[1][i][j]=ult; if(i-1>=1&&ori[i-1][j]==0) liga(quem(i-1, j), quem(i, j), 1, 1); if(i+1<=nn&&ori[i+1][j]==0) liga(quem(i+1, j), quem(i, j), 1, 1); if(j-1>=1&&ori[i][j-1]==0) liga(quem(i, j-1), quem(i, j), 1, 1); if(j+1<=mm&&ori[i][j+1]==0) liga(quem(i, j+1), quem(i, j), 1, 1); } ult=mm; for(int j=mm; j>=1; j--) { if(ori[i][j]!=0) { ult=j-1; continue; } parede[2][i][j]=ult; } } for(int j=1; j<=mm; j++) { int ult=1; for(int i=1; i<=mm; i++) { if(ori[i][j]!=0) { ult=i+1; continue; } parede[3][i][j]=ult; } ult=nn; for(int i=nn; i>=1; i--) { if(ori[i][j]!=0) { ult=i-1; continue; } parede[4][i][j]=ult; } } for(int i=1; i<=nn; i++) { for(int j=1; j<=mm; j++) { if(ori[i][j]!=0) continue; int aa=abs(parede[1][i][j]-j); int bb=abs(parede[2][i][j]-j); int cc=abs(parede[3][i][j]-i); int dd=abs(parede[4][i][j]-i); int melhor=min(min(aa, bb), min(cc, dd))+1; int cur=quem(i, j); liga(cur, quem(i, parede[1][i][j]), min(melhor, aa), 0); liga(cur, quem(i, parede[2][i][j]), min(melhor, bb), 0); liga(cur, quem(parede[3][i][j], j), min(melhor, cc), 0); liga(cur, quem(parede[4][i][j], j), min(melhor, dd), 0); } } n=nn*mm; dijkstra(star); // for(int i=1; i<=n; i++) printf("%d > %d\n", i, dist[i]); printf("%d\n", dist[chegada]); }

Compilation message (stderr)

portals.cpp: In function 'void dijkstra(int)':
portals.cpp:33:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0; i<g[cur].size(); i++) {
                ~^~~~~~~~~~~~~~
portals.cpp: In function 'int main()':
portals.cpp:46:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &nn, &mm);
  ~~~~~^~~~~~~~~~~~~~~~~~~
portals.cpp:48:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   char cc[MAXNN]; scanf(" %s", &cc[1]);
                   ~~~~~^~~~~~~~~~~~~~~
#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...