#include <bits/stdc++.h>
#define F first
#define S second
#define LL long long
#define rep(i, x) for(auto &i : (x))
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define left(x) ((x)<<1)
#define right(x) ((x)>>1)
#define LSOne(x) (x & -(x))
using namespace std;
using T = pair<char, pair<int, int>>;
const int SZ = 2e5 + 1;
const LL MOD = 1e9 + 7;
const LL INF = 1e18 + 1;
LL bp (LL b, LL e, LL m = MOD) {
if ( e == 0 ) return 1;
LL T = bp(b, e / 2);
T *= T; T %= m;
if ( e & 1 ) T *= b;
return T %= m;
}
LL nv_i (LL a) { return bp(a, MOD - 2); };
LL gcd (LL a, LL b) { return (b == 0 ? a : gcd(b, a % b)); }
LL lcm(LL a, LL b) { return (a * (b / gcd(a, b))); }
LL ceil (LL a, LL b) { return ((a + b - 1) / b); }
vector<vector<char>> G;
vector<vector<bool>> VIS;
vector<vector<pair<int, int>>> FATHER;
int n, m;
void dijkstra() {
priority_queue<T, vector<T>, greater<T>> PQ;
PQ.push({G[1][1], {1, 1}});
while(!PQ.empty()) {
int i = PQ.top().S.F, j = PQ.top().S.S;
char letter = PQ.top().F;
PQ.pop();
pair<int, int> a = {0, 0};
if(i + 1 <= n) {
PQ.push({G[i + 1][j], {i + 1, j}});
if(FATHER[i + 1][j] != a) {
a = FATHER[i + 1][j];
if(letter < G[a.F][a.S]) {
FATHER[i + 1][j] = {i, j};
}
}
else {
FATHER[i + 1][j] = {i, j};
}
a = {0, 0};
}
if(j + 1 <= m) {
PQ.push({G[i][j + 1], {i, j + 1}});
if(FATHER[i][j + 1] != a) {
a = FATHER[i][j + 1];
if(letter < G[a.F][a.S]) {
FATHER[i][j + 1] = {i, j};
}
}
else {
FATHER[i][j + 1] = {i, j};
}
a = {0, 0};
}
}
}
void word(int i, int j) {
if(i == 1 && j == 1) {
cout << char(G[i][j]);
return;
}
word(FATHER[i][j].F, FATHER[i][j].S);
cout << char(G[i][j]);
}
void solve() {
cin >> n >> m;
G.resize(n + 1, vector<char> (m + 1));
VIS.resize(n + 1, vector<bool> (m + 1));
FATHER.resize(n + 1, vector<pair<int, int>> (m + 1));
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= m; j ++) {
cin >> G[i][j];
}
}
dijkstra();
word(n, m);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Execution timed out |
1081 ms |
27048 KB |
Time limit exceeded |
4 |
Runtime error |
683 ms |
65536 KB |
Execution killed with signal 9 |
5 |
Execution timed out |
1064 ms |
51912 KB |
Time limit exceeded |
6 |
Runtime error |
728 ms |
65536 KB |
Execution killed with signal 9 |
7 |
Runtime error |
722 ms |
65536 KB |
Execution killed with signal 9 |
8 |
Runtime error |
416 ms |
65536 KB |
Execution killed with signal 9 |
9 |
Runtime error |
689 ms |
65536 KB |
Execution killed with signal 9 |
10 |
Runtime error |
701 ms |
65536 KB |
Execution killed with signal 9 |
11 |
Runtime error |
732 ms |
65536 KB |
Execution killed with signal 9 |
12 |
Runtime error |
711 ms |
65536 KB |
Execution killed with signal 9 |
13 |
Runtime error |
662 ms |
65536 KB |
Execution killed with signal 9 |
14 |
Runtime error |
408 ms |
65536 KB |
Execution killed with signal 9 |
15 |
Runtime error |
705 ms |
65536 KB |
Execution killed with signal 9 |
16 |
Runtime error |
318 ms |
65536 KB |
Execution killed with signal 9 |