HDU 4849 Wow! Such City!陝西邀請賽C(最短路)

來源:互聯網
上載者:User

標籤:style   http   color   os   for   io   

HDU 4849 Wow! Such City!

題目連結

題意:按照題目中的公式構造出臨接矩陣後,求出1到2 - n最短路%M的最小值

思路:就根據題目中方法構造矩陣,然後寫一個dijkstra,利用d數組取求答案即可

代碼:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const long long INF = 100000000000000LL;const int N = 1005;typedef long long ll;long long n, m, X0, X1, Y0, Y1;long long a[N * N], b[N * N], c[N * N], g[N][N], d[N];int vis[N * N];void build() {    memset(vis, 0, sizeof(vis));    a[0] = X0; a[1] = X1;    b[0] = Y0; b[1] = Y1;    g[1][2] = (a[1] * 90123 % 8475871 + b[1]) % 8475871 + 1;    for (int i = 2; i < n * n; i++) {a[i] = (12345 + a[i - 1] * 23456 % 5837501 + a[i - 2] * 34567 % 5837501 + a[i - 1] * a[i - 2] % 5837501 * 45678 % 5837501) % 5837501;b[i] = (56789 + b[i - 1] * 67890 % 9860381 + b[i - 2] * 78901 % 9860381 + b[i - 1] * b[i - 2] % 9860381 * 89012 % 9860381) % 9860381;g[i / n + 1][i % n + 1] = (a[i] * 90123 % 8475871 + b[i]) % 8475871 + 1;    }    for (int i = 1; i <= n; i++)g[i][i] = 0;}void dijk() {    int v[N];    memset(v, 0, sizeof(v));    for (int i = 1; i <= n; i++)d[i] = INF;    d[1] = 0;    for (int i = 0; i < n; i++) {int x; long long m = INF;for (int y = 1; y <= n; y++) if (!v[y] && d[y] <= m) {m = d[y]; x = y;}v[x] = 1;for (int y = 1; y <= n; y++) d[y] = min(d[y], d[x] + g[x][y]);    }}int main() {    while (cin >> n >> m >> X0 >> X1 >> Y0 >> Y1) {build();dijk();for (int i = 2; i <= n; i++) {    vis[d[i] % m] = 1;}long long ans_v = INF;for (long long i = 0; i < m; i++) {    if (vis[i])ans_v = min(ans_v, i);}cout << ans_v << endl;    }    return 0;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.