-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDP_dag.cpp
100 lines (85 loc) · 1.69 KB
/
DP_dag.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <cmath>
#include<sstream>
#include<list>
#include<iomanip>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <bitset>
#include <cassert>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
const int INF = 1000000007;
const long long LINF = 3e18 + 7;
const int MAX_N = 1200010;
const int MAX_W = 600010;
const int MAX_ARRAYK = 100000;
double PI = 3.14159265358979323846;
//using ll = long long;
int H, W;
int a[1010][1010];
vector<int> G[1010000];
int dy[4] = { 0, -1, 0, 1 };
int dx[4] = { -1, 0, 1, 0 };
int deg[1010000];
long long dp[1010000];
long long mod = 1000000007;
int main() {
cin >> H >> W;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> a[i][j];
}
}
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
for (int i = 0; i < 4; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
if (nx >= 0 && nx < W && ny >= 0 && ny < H && a[y][x] < a[ny][nx]) {
G[y * W + x].push_back(ny * W + nx);
deg[ny * W + nx]++;
}
}
}
}
queue<int> que;
for (int i = 0; i < H * W; i++) {
if (deg[i] == 0) {
que.push(i);
dp[i] = 0LL;
}
}
while (!que.empty()) {
int v = que.front();
que.pop();
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i];
dp[u] += (dp[v] + 1LL);
dp[u] %= mod;
deg[u]--;
if (deg[u] == 0) {
que.push(u);
}
}
}
long long ans = H * W;
for (int i = 0; i < H * W; i++) {
ans += dp[i];
ans %= mod;
}
cout << ans << endl;
return 0;
}