This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
const int MAXN = 3e4+5;
vector< vector < pair<int, int> > > adj(MAXN);
vector< vector < int > > doges(MAXN);
int dp[MAXN];
//map< pair<int, int>, bool > done;
map< pair<int, int>, int> max_road;
bool visited[MAXN];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
int goal, begin;
for(int i = 0; i < m; i++)
{
pair<int, int> temp;
cin >> temp.first >> temp.second;
if(i == 1)
goal = temp.first;
else if(i == 0)
begin = temp.first;
doges[temp.first].push_back(temp.second);
}
for(int i = 0; i <= n; i++) {
dp[i] = 1e9;
//visited[i] = false;
}
//visited[begin] = false;
dp[begin] = 0;
//set< pair<int, int> > curr;
//curr.insert({0, begin});
int ans = 1e9;
for(int i = 0; i < n; i++)
{
int v = -1;
for (int j = 0; j < n; j++) {
if (!visited[j] && (v == -1 || dp[j] < dp[v]))
v = j;
}
if (dp[v] == 1e9)
break;
visited[v] = true;
for(int temp : doges[v]) {
int currNode = v + temp;
int cnt = 1;
while(currNode < n)
{
int val = max_road[{v, currNode}];
if(val == 0 || val > cnt)
{
//adj[v].erase({val, currNode});
max_road[{v, currNode}] = cnt;
//adj[v].insert({cnt, currNode});
}
//if(curr == 4)
//cout << temp.first << "\n";
cnt++;
currNode += temp;
}
currNode = v - temp;
cnt = 1;
while(currNode >= 0)
{
int val = max_road[{v, currNode}];
if(val == 0 || val > cnt)
{
//adj[v].erase({val, currNode});
max_road[{v, currNode}] = cnt;
//adj[v].insert({cnt, currNode});
}
//if(curr == 4)
//cout << temp.first << "\n";
cnt++;
currNode -= temp;
}
}
for(int to = 0; to < n; to++)
{
int len = max_road[{v, to}];
if(len != 0)
{
if (dp[v] + len < dp[to]) {
dp[to] = dp[v] + len;
}
}
}
for (auto edge : adj[v]) {
int to = edge.second;
int len = edge.first;
if (dp[v] + len < dp[to]) {
dp[to] = dp[v] + len;
}
}
adj[v].clear();
}
ans = dp[goal];
if(ans >= 1e9)
ans = -1;
cout << ans << "\n";
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:37:15: warning: 'begin' may be used uninitialized in this function [-Wmaybe-uninitialized]
37 | dp[begin] = 0;
| ~~~~~~~~~~^~~
skyscraper.cpp:117:9: warning: 'goal' may be used uninitialized in this function [-Wmaybe-uninitialized]
117 | ans = dp[goal];
| ~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |