hdu 4849 Wow! Such City!(dijstra)

题目链接: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;
}

hdu 4849 Wow! Such City!(dijstra)

时间: 2024-10-29 10:46:33

hdu 4849 Wow! Such City!(dijstra)的相关文章

HDU 4849 Wow! Such City!陕西邀请赛C(最短路)

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 I

hdu 4849 Wow! Such City! (最短路spfa)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4849 Wow! Such City! Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 1486    Accepted Submission(s): 511 Problem Description Doge, tired of bei

HDU 4849 - Wow! Such City!

怎么说吧,就是一个又臭又长又水的单源最短路径外加算算MOD的题. 1 #include<cstdio> 2 #include<queue> 3 #include<cstring> 4 #define MAXN 1003 5 #define MAXK 1000*1000+1000 6 #define INF 0x3f3f3f3f 7 using namespace std; 8 typedef long long ll; 9 int n,m; 10 ll x[MAXK],

hdu 4849 最短路 西安邀请赛 Wow! Such City!

http://acm.hdu.edu.cn/showproblem.php?pid=4849 会有很多奇怪的Wa的题,当初在西安就不知道为什么wa,昨晚做了,因为一些Sb错误也wa了很久,这会儿怎么写都会AC.... 收获: 1.还是基本都构思好在去敲代码,因为当时没过,昨晚心里有阴影,敲得很慢,而且最开始各种取模以防止漏掉,太保守了......以后一定先估算是不是需要取模防止TLE,当然时间够的话还是适当多取个模防止莫名其妙的错误.. 2.如果出错,注意参数是不是对的,最开始写好之后,因为m和

hdu4849 Wow! Such City!(最短路dijkstra)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4849 Problem Description Doge, tired of being a popular image on internet, is considering moving to another city for a new way of life. In his co

hdu 4893 Wow! Such Sequence!(线段树)

题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 修改k的为值增加d 2 l r, 查询l到r的区间和 3 l r, 间l到r区间上的所以数变成最近的斐波那契数,相等的话取向下取. 解题思路:线段树,对于每个节点新增一个bool表示该节点以下的位置是否都是斐波那契数. #include <cstdio> #include <cstring> #include <cstdlib> #include <algor

HDU 4893 Wow! Such Sequence! 水线段树

思路: 线段树走起.. 写完这题就退役T^T 单点更新的时候直接找到这个点的最近fib,然后维护当前和 和 fib的和 #include<stdio.h> #include<string.h> #include<iostream> #include<math.h> #include<algorithm> #include<queue> #include<map> #include<set> #include&l

hdu 4893 Wow! Such Sequence!(线段树功能:单点更新,区间更新相邻较小斐波那契数)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4893 --------------------------------------------------------------------------------------------------------------------------------------------

hdu 4848 Wow! Such Conquering!

Wow! Such Conquering! Problem Description There are n Doge Planets in the Doge Space. The conqueror of Doge Space is Super Doge, who is going to inspect his Doge Army on all Doge Planets. The inspection starts from Doge Planet 1 where DOS (Doge Olymp