hdu 4849 Wow! Such City! 简单最短路

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4849

 

理解题意,然后根据公式求出距离

再套用最短路模板即可

队友写的~

 

#include <cstring>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;
typedef pair<ll, int> P;
#define INF 0x3f3f3f3f
const double eps = 1e-9;
const int maxn = 1000;
const int modx = 5837501;
const int mody = 9860381;
const int modz = 8475871;
ll x[maxn*maxn+10], y[maxn*maxn+10];
ll z[maxn*maxn+10];

void build(int n, ll x0, ll x1, ll y0, ll y1)
{
    int N = n*n;
    x[0] = x0; y[0] = y0;
    x[1] = x1; y[1] = y1;
    for(int i = 2; i < N; i++)
    {
        x[i] = (12345 + x[i-1]*23456 + x[i-2]*34567 + (x[i-1]*x[i-2]%modx)*45678)%modx;
        y[i] = (56789 + y[i-1]*67890 + y[i-2]*78901 + (y[i-1]*y[i-2]%mody)*89012)%mody;
    }
    for(int i = 0; i < N; i++)
        z[i] = (x[i]*90123 + y[i]) % modz + 1;
    //cout<<endl;
}
ll d[maxn+10];

void Dijkstra(int n)
{
    priority_queue<P, vector<P>, greater<P> > que;
    fill(d, d+n+1, INF);
    d[0] = 0;
    que.push(make_pair(d[0], 0));
    while(!que.empty())
    {
        P tp = que.top();que.pop();
        int u = tp.second;
        if(d[u] < tp.first)
            continue;
        for(int v = 0; v < n; v++)
        {
            if(v == u)    continue;
            ll dis = z[u*n+v];
            if(d[v] > d[u] + dis)
            {
                d[v] = d[u]+ dis;
                que.push(make_pair(d[v], v));
            }
        }
    }
}
int main()
{
    int n,m,x0,x1,y0,y1;
    //freopen("in", "r", stdin);
    while(scanf("%d%d%d%d%d%d", &n, &m, &x0, &x1, &y0, &y1) != EOF)
    {
        build(n,(ll)x0,(ll)x1,(ll)y0,(ll)y1);
        Dijkstra(n);
        ll ans = m;
        for(int i = 1; i < n; i++)
            ans = min(ans, d[i]%m);
        printf("%I64d\n", ans);
    }
}

 

posted @ 2015-05-20 19:31  地鼠地鼠  阅读(171)  评论(0编辑  收藏  举报