Submission #886251

#TimeUsernameProblemLanguageResultExecution timeMemory
886251tsumondaiJakarta Skyscrapers (APIO15_skyscraper)C++14
100 / 100
184 ms67388 KiB
#include <bits/stdc++.h>
using namespace std;

#define Task "skyscraper"
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME  (1.0 * clock() / CLOCKS_PER_SEC)

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
 
const int N = 3e4 + 5;
 
const int oo = 0x3f3f3f3f, mod = 1e9 + 7;
 
int n, m, k;
string s;
vector<int> arr;

vector<int> ed[N];
vector<pair<int, int>> ej[N];
 
bool find(int j, int d) {
    auto it = lower_bound(ed[j].begin(), ed[j].end(), d);
 
    return it != ed[j].end() && *it == d;
}
 
void process() {
    static int dd[N];
    int n, m, i, j, d, s, t;
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int,int>>> pq;
 
   	cin >> n >> m;
    s = t = -1;
    for (i = 0; i < m; i++) {
        cin >> j >> d;
        if (i == 0)
            s = j;
        else if (i == 1)
            t = j;
        ed[j].push_back(d);
    }
    for (i = 0; i < n; i++) {
        sort(ed[i].begin(), ed[i].end());
        ed[i].erase(unique(ed[i].begin(), ed[i].end()), ed[i].end());
    }
    for (i = 0; i < n; i++)
        for (int d : ed[i]) {
            for (j = i + d; j < n; j += d) {
                ej[i].push_back({j, (j - i) / d});
                if (find(j, d))
                    break;
            }
            for (j = i - d; j >= 0; j -= d) {
                ej[i].push_back({j, (i - j) / d});
                if (find(j, d))
                    break;  
            }
        }
    memset(dd, 0x3f, n * sizeof *dd);
    dd[s] = 0;
    pq.push({0, s});
    while (!pq.empty()) {
        i = pq.top().second;
        pq.pop();
        for (auto tmp : ej[i]) {
        	int j=tmp.fi, w=tmp.se;
            d = dd[i] + w;
            if (d < dd[j]) {
                dd[j] = d;
                pq.push({d, j});
            }
        }
    }
    cout << (dd[t] == oo ? -1 : dd[t]);
}

signed main() {
    cin.tie(0)->sync_with_stdio(false);
    if(fopen(Task".INP", "r")) {
        freopen(Task".INP", "r", stdin);
        freopen(Task".OUT", "w", stdout);
    }
    process();
    cerr << "Time elapsed: " << __TIME << " s.\n";
    return 0;
}

// dont stop

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:87:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |         freopen(Task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:88:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |         freopen(Task".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...