Submission #106895

# Submission time Handle Problem Language Result Execution time Memory
106895 2019-04-20T20:34:29 Z wilwxk Portals (BOI14_portals) C++11
70 / 100
563 ms 263168 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN=1e6+5;
const int MAXNN=1e3+3;
set<pair<int, int> > g[MAXN];
set< pair<int, int> > s;
short ori[MAXNN][MAXNN];
short 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;
	g[a].insert({b, c});
	if(k) g[b].insert({a, 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;
		s.erase(s.begin());
		for(auto aresta : g[cur]) {
			int viz=aresta.first; int peso=aresta.second;
			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<=nn; 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);
		}
	}
	// for(int i=1; i<=nn; i++) { for(int j=1; j<=mm; j++) printf("%d ", parede[3][i][j]); printf("\n"); }

	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

portals.cpp: In function 'int main()':
portals.cpp:43: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:45: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 time Memory Grader output
1 Correct 44 ms 47352 KB Output is correct
2 Correct 43 ms 47480 KB Output is correct
3 Correct 45 ms 47352 KB Output is correct
4 Correct 42 ms 47352 KB Output is correct
5 Correct 43 ms 47448 KB Output is correct
6 Correct 46 ms 47480 KB Output is correct
7 Correct 43 ms 47444 KB Output is correct
8 Correct 46 ms 47388 KB Output is correct
9 Correct 46 ms 47352 KB Output is correct
10 Correct 43 ms 47360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 47360 KB Output is correct
2 Correct 43 ms 47480 KB Output is correct
3 Correct 43 ms 47492 KB Output is correct
4 Correct 42 ms 47352 KB Output is correct
5 Correct 49 ms 47448 KB Output is correct
6 Correct 43 ms 47480 KB Output is correct
7 Correct 42 ms 47488 KB Output is correct
8 Correct 44 ms 47408 KB Output is correct
9 Correct 48 ms 48888 KB Output is correct
10 Correct 46 ms 48760 KB Output is correct
11 Correct 44 ms 48120 KB Output is correct
12 Correct 45 ms 48248 KB Output is correct
13 Correct 45 ms 48248 KB Output is correct
14 Correct 42 ms 47352 KB Output is correct
15 Correct 45 ms 48520 KB Output is correct
16 Correct 45 ms 47368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 47356 KB Output is correct
2 Correct 41 ms 47352 KB Output is correct
3 Correct 42 ms 47376 KB Output is correct
4 Correct 46 ms 47488 KB Output is correct
5 Correct 83 ms 54392 KB Output is correct
6 Correct 82 ms 54880 KB Output is correct
7 Correct 81 ms 55928 KB Output is correct
8 Correct 94 ms 53880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 47352 KB Output is correct
2 Correct 42 ms 47480 KB Output is correct
3 Correct 43 ms 47456 KB Output is correct
4 Correct 47 ms 47368 KB Output is correct
5 Correct 51 ms 47352 KB Output is correct
6 Correct 45 ms 47480 KB Output is correct
7 Correct 45 ms 47480 KB Output is correct
8 Correct 47 ms 47480 KB Output is correct
9 Correct 47 ms 48964 KB Output is correct
10 Correct 47 ms 48760 KB Output is correct
11 Correct 45 ms 48120 KB Output is correct
12 Correct 51 ms 48248 KB Output is correct
13 Correct 50 ms 48248 KB Output is correct
14 Correct 95 ms 54460 KB Output is correct
15 Correct 80 ms 54904 KB Output is correct
16 Correct 84 ms 55976 KB Output is correct
17 Correct 81 ms 56952 KB Output is correct
18 Correct 92 ms 59388 KB Output is correct
19 Correct 128 ms 66168 KB Output is correct
20 Correct 104 ms 66120 KB Output is correct
21 Correct 76 ms 54520 KB Output is correct
22 Correct 79 ms 54520 KB Output is correct
23 Correct 83 ms 55160 KB Output is correct
24 Correct 127 ms 66168 KB Output is correct
25 Correct 42 ms 47352 KB Output is correct
26 Correct 49 ms 48504 KB Output is correct
27 Correct 43 ms 47332 KB Output is correct
28 Correct 70 ms 53880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 47372 KB Output is correct
2 Correct 43 ms 47360 KB Output is correct
3 Correct 45 ms 47472 KB Output is correct
4 Correct 44 ms 47352 KB Output is correct
5 Correct 52 ms 47352 KB Output is correct
6 Correct 49 ms 47428 KB Output is correct
7 Correct 43 ms 47480 KB Output is correct
8 Correct 42 ms 47480 KB Output is correct
9 Correct 51 ms 48796 KB Output is correct
10 Correct 49 ms 48888 KB Output is correct
11 Correct 46 ms 48152 KB Output is correct
12 Correct 50 ms 48248 KB Output is correct
13 Correct 45 ms 48248 KB Output is correct
14 Correct 77 ms 54392 KB Output is correct
15 Correct 88 ms 54936 KB Output is correct
16 Correct 94 ms 55928 KB Output is correct
17 Correct 83 ms 56960 KB Output is correct
18 Correct 90 ms 59384 KB Output is correct
19 Correct 83 ms 66272 KB Output is correct
20 Correct 123 ms 66040 KB Output is correct
21 Correct 76 ms 54468 KB Output is correct
22 Correct 77 ms 54520 KB Output is correct
23 Correct 114 ms 55160 KB Output is correct
24 Runtime error 563 ms 263168 KB Execution killed with signal 9 (could be triggered by violating memory limits)
25 Halted 0 ms 0 KB -