hdu 4849 Wow! Such City!(dijstra)

來源:互聯網
上載者:User

標籤:style   http   color   os   for   io   

題目連結:hdu 4849 Wow! Such City!

題目大意:有N個城市,給定計算兩兩城市距離的公式,然後求0到1~N-1的城市中,最短路徑模掉M的最小值。

解題思路:先根據公式求出距離C矩陣,注意中間連乘3次的可能爆long long,然後用裸的dijstra演算法求最短路。

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;const int maxd = 1005;const int maxn = maxd * maxd;const ll INF = 0x3f3f3f3f3f3f3f3f;int N, M;ll X[maxn], Y[maxn], Z[maxn];ll C[maxd][maxd], d[maxd];void init_distant () {    for (int i = 1; i < N * N; i++) {        if (i >= 2) {            X[i] = (12345 + X[i-1] * 23456 + X[i-2] * 34567 + (X[i-1] * X[i-2] % 5837501) * 45678)  % 5837501;            Y[i] = (56789 + Y[i-1] * 67890 + Y[i-2] * 78901 + (Y[i-1] * Y[i-2] % 9860381) * 89012)  % 9860381;        }        Z[i] = (X[i] * 90123 + Y[i]) % 8475871 + 1;    }    for (int i = 0; i < N; i++) {        for (int j = 0; j < N; j++) {            if (i == j)                C[i][j] = 0;            else                C[i][j] = Z[i*N+j];        }    }}ll dijstra () {    int v[maxd];    memset(v, 0, sizeof(v));    for (int i = 0; i < N; i++)        d[i] = INF;    d[0] = 0;    ll ans = INF;    for (int i = 0; i < N; i++) {        int u = 0;        ll dis = INF;        for (int j = 0; j < N; j++) {            if (d[j] < dis && v[j] == 0) {                u = j;                dis = d[j];            }        }        v[u] = 1;        if (d[u] % M < ans && u)            ans = d[u] % M;        for (int j = 0; j < N; j++) {            if (d[u] + C[u][j] < d[j])                d[j] = d[u] + C[u][j];        }    }    return ans;}int main () {    while (scanf("%d%d%lld%lld%lld%lld", &N, &M, &X[0], &X[1], &Y[0], &Y[1]) == 6) {        init_distant();        printf("%lld\n", dijstra());    }    /*    while (cin >> N >> M >> X[0] >> X[1] >> Y[0] >> Y[1]) {        init_distant();        cout << dijstra() << 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.