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;
}

  

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

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

    HDU 4849 Wow! Such City! 题目链接 题意:依照题目中的公式构造出临接矩阵后.求出1到2 - n最短路%M的最小值 思路:就依据题目中方法构造矩阵,然后写一个dijkstra,利 ...

  2. HDU 4849 - Wow! Such City!

    Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)   Input There ar ...

  3. 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)

    关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...

  4. [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

  5. 用scheme语言实现SPFA算法(单源最短路)

    最近自己陷入了很长时间的学习和思考之中,突然发现好久没有更新博文了,于是便想更新一篇. 这篇文章是我之前程序设计语言课作业中一段代码,用scheme语言实现单源最段路算法.当时的我,花了一整天时间,学 ...

  6. 单源最短路_SPFA_C++

    当我们需要求一个点到其它所有点的最短路时,我们可以采用SPFA算法 代码特别好写,而且可以有环,但是不能有负权环,时间复杂度是O(α(n)n),n为边数,α(n)为n的反阿克曼函数,一般小于等于4 模 ...

  7. 【UVA1416】(LA4080) Warfare And Logistics (单源最短路)

    题目: Sample Input4 6 10001 3 21 4 42 1 32 3 33 4 14 2 2Sample Output28 38 题意: 给出n个节点m条无向边的图,每条边权都为正.令 ...

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

    https://vjudge.net/contest/66569#problem/A http://blog.csdn.net/wangjian8006/article/details/7871889 ...

  9. 模板C++ 03图论算法 1最短路之单源最短路(SPFA)

    3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...

随机推荐

  1. VMware前路难测,多个厂家群雄逐鹿

    以VMware为例,虚拟机巨头公布了第二财季报告所示,它第二财季收入同比增长13%,达到了21.7亿美元,而且该公司收入和每股收益均超出预期. 在人们高谈Salesforce.亚马逊等新兴云计算厂商取 ...

  2. Python入门之python装饰器的4种类型:函数装饰函数、函数装饰类、类装饰函数、类装饰类

    一:函数装饰函数 def wrapFun(func): def inner(a, b): print('function name:', func.__name__) r = func(a, b) r ...

  3. Android 基础知识点(一)

  4. 使用requireJS加载不符合AMD规范的js文件:shim的使用方式和实现原理

    原文链接: http://www.bubuko.com/infodetail-671521.html

  5. Fast特征点的寻找和提取

    一.基础 最初由Rosten和Drummond [Rosten06]提出的FAST(加速段测试的特征)特征检测算法是基于将点P与其包围圆内的点集的直接比较的思想. 基本思想是,如果附近的几个点与P类似 ...

  6. 在一个activity中销毁指定activity

    通过静态变量的方法: 1.在Aactivity中设置一个Activity静态变量 static Activity activity; 2.在onCreate中: activity=this: 3.在B ...

  7. jQuery 源码分析:当 selector 传来一个函数时,怎么进行处理?

    本文章为 0.9 版本,将会在稍后润色更新.本文使用的 jQuery 版本为 3.4.0 我们知道使用 $ 操作符时,可以往里面塞很多类型的参数,字符串,对象,函数...,jQuery 会根据不同的参 ...

  8. luogu 1004 方格取数

    题目描述 设有 $N \times N$ 的方格图 $(N \le 9)$ ,我们将其中的某些方格中填入正整数,而其他的方格中则放入数字 $0$ .如下图所示(见样例): A 0 0 0 0 0 0 ...

  9. HDU 6171 Admiral(双向BFS+队列)题解

    思路: 最大步骤有20,直接BFS会超时. 因为知道开始情况和结果所以可以用双向BFS,每个BFS规定最大步骤为10,这样相加肯定小于20.这里要保存每个状态搜索到的最小步骤,用Hash储存.当发现现 ...

  10. HDU 5887 Herbs Gathering(搜索求01背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=5887 题意: 容量很大的01背包. 思路: 因为这道题目背包容量比较大,所以用dp是行不通的.所以得用搜索来做, ...