HDU-4849 Wow! Such City! (单源最短路)

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 country there are N (2 ≤N≤ 1000) cities labeled 0 . . . N - 1.
He is currently in city 0. Meanwhile, for each pair of cities, there
exists a road connecting them, costing Ci,j (a positive integer) for traveling from city i to city j. Please note that Ci,j may not equal to Cj,i for any given i ≠ j.
Doge is carefully examining the cities: in fact he will divide cities (his current city 0 is NOT included) into M (2 ≤ M ≤ 106)
categories as follow: If the minimal cost from his current city
(labeled 0) to the city i is Di, city i belongs to category numbered Di mod M.Doge wants to know the “minimal” category (a category with minimal number) which contains at least one city.
For example, for a country with 4 cities (labeled 0 . . . 3, note
that city 0 is not considered), Doge wants to divide them into 3
categories. Suppose category 0 contains no city, category 1 contains
city 2 and 3, while category 2 contains city 1, Doge consider category 1
as the minimal one.
Could you please help Doge solve this problem?

Note:

Ci,j is generated in the following way:
Given integers X0, X1, Y0, Y1, (1 ≤ X0, X1, Y0, Y1≤ 1234567), for k ≥ 2 we have
Xk  = (12345 + Xk-1 * 23456 + Xk-2 * 34567 + Xk-1 * Xk-2 * 45678)  mod  5837501
Yk  = (56789 + Yk-1 * 67890 + Yk-2 * 78901 + Yk-1 * Yk-2 * 89012)  mod  9860381
The for k ≥ 0 we have

Zk = (Xk * 90123 + Yk ) mod 8475871 + 1

Finally for 0 ≤ i, j ≤ N - 1 we have

Ci,j = Zi*n+j for i ≠ j
Ci,j = 0   for i = j

Input

There are several test cases. Please process till EOF.
For each test case, there is only one line containing 6 integers N,M,X0,X1,Y0,Y1.See the description for more details.

Output

For each test case, output a single line containing a single integer: the number of minimal category.

Sample Input

3 10 1 2 3 4

4 20 2 3 4 5

Sample Output

1

10

题目分析:按题意将那个表示距离的矩阵处理出来,然后就是单源最短路了。

代码如下:

# include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;
const int INF=1<<30;
int n,m;
long long x[1001005],y[1001005],z[1001005];
int a[1005][1005],dis[1005];
void init()
{
    for(int i=2;i<n*n;++i)
        x[i]=(12345+((x[i-1]%5837501)*23456)%5837501+((x[i-2]%5837501)*34567)%5837501+(((x[i-1]%5837501)*(x[i-2]%5837501))%5837501)*45678)%5837501;
    for(int i=2;i<n*n;++i)
        y[i]=(56789+((y[i-1]%9860381)*67890)%9860381+((y[i-2]%9860381)*78901)%9860381+(((y[i-1]%9860381)*(y[i-2]%9860381))%9860381)*89012)%9860381;
    for(int i=0;i<n*n;++i)
        z[i]=(((x[i]%8475871)*90123+y[i])%8475871+1)%8475871;
    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j)
            a[i][j]=(i==j)?0:z[i*n+j];
    }
}
void spfa()
{
    fill(dis,dis+n,INF);
    dis[0]=0;
    queue<int>q;
    q.push(0);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=1;i<n;++i){
            if(dis[i]>dis[u]+a[u][i]){
                dis[i]=dis[u]+a[u][i];
                q.push(i);
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d%lld%lld%lld%lld",&n,&m,&x[0],&x[1],&y[0],&y[1]))
    {
        init();
        spfa();
        int ans=m;
        for(int i=1;i<n;++i)
            ans=min(ans,dis[i]%m);
        printf("%d\n",ans);
    }
    return 0;
}

  

时间: 2024-12-16 02:23:08

HDU-4849 Wow! Such City! (单源最短路)的相关文章

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 name

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],

【裸单源最短路:Dijkstra算法两种版本】hdu 1874 畅通工程续

Source : hdu 1874 畅通工程续 http://acm.hdu.edu.cn/showproblem.php?pid=1874 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让行人很困扰. 现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离. Input 本题目包含多组数据,请处理到文件结束.

【算法系列学习】Dijkstra单源最短路 [kuangbin带你飞]专题四 最短路练习 A - Til the Cows Come Home

https://vjudge.net/contest/66569#problem/A http://blog.csdn.net/wangjian8006/article/details/7871889 邻接矩阵实现的单源最短路 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<algorithm> 6 #include

Dijkstra算法 --- 单源最短路

Dijkstra算法适用于边权值为正的情况,可用于计算正权图上的单元最短路. 其伪代码如下: 设d[v0] = 0, 其他d[i] = INF 循环n次{ 在所有未标号的结点中,选取d值最小的结点x 给结点x加上永久标号 对于从x出发的所有边,执行松弛操作. } //松弛操作的伪代码如下: RELAX(u,v,w) if(u.d + w(u,v) < v.d){ v.d = w.d + w(u,v); pre[v] = u; } Dijkstra算法代码: /* Dijkstra 单源最短路算法

常见模板(欧拉筛素数,最小生成树,快排,并查集,单源最短路)

欧拉筛素数: #include<cstdio> #define maxn 10000000+10 using namespace std; int n,prime[5000001],num_prime=0,m; bool if_prime[maxn]; void euler(int limit) { for(int i=2;i<=limit;i++) { if(!if_prime[i]) prime[++num_prime]=i; for(int j=1;prime[j]*i<=l

利用分支限界法求解单源最短路(Dijkstra)问题

分支限界法定义:采用BFS算法,并使用剪枝函数的算法称为分支界限法. 分支限界法解释:按广度优先的原则,有选择的在其child中进行扩展,从而舍弃不含有最优解的分支,不断重复这一过程,直到找到答案或者判定无解. 分支界限法常常用到优先队列来选择最佳扩展节点,有时也会用到普通队列,以先进先出为原则来进行筛选. 单源最短路问题定义:给定有向图和起点,寻找到达所有点的最短路径. 单源最短路的分支限界法概述:首先把节点加入优先队列,之后不断地从队列中取出最优扩展点,观察其可抵达的所有目标节点,若当前路径