HDU-4849 Wow! Such City! (单源最短路)
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
For each test case, there is only one line containing 6 integers N,M,X0,X1,Y0,Y1.See the description for more details.
# 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! (单源最短路)的更多相关文章
- HDU 4849 Wow! Such City!陕西邀请赛C(最短路)
HDU 4849 Wow! Such City! 题目链接 题意:依照题目中的公式构造出临接矩阵后.求出1到2 - n最短路%M的最小值 思路:就依据题目中方法构造矩阵,然后写一个dijkstra,利 ...
- HDU 4849 - Wow! Such City!
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) Input There ar ...
- 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)
关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...
- [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)
Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...
- 用scheme语言实现SPFA算法(单源最短路)
最近自己陷入了很长时间的学习和思考之中,突然发现好久没有更新博文了,于是便想更新一篇. 这篇文章是我之前程序设计语言课作业中一段代码,用scheme语言实现单源最段路算法.当时的我,花了一整天时间,学 ...
- 单源最短路_SPFA_C++
当我们需要求一个点到其它所有点的最短路时,我们可以采用SPFA算法 代码特别好写,而且可以有环,但是不能有负权环,时间复杂度是O(α(n)n),n为边数,α(n)为n的反阿克曼函数,一般小于等于4 模 ...
- 【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条无向边的图,每条边权都为正.令 ...
- 【算法系列学习】Dijkstra单源最短路 [kuangbin带你飞]专题四 最短路练习 A - Til the Cows Come Home
https://vjudge.net/contest/66569#problem/A http://blog.csdn.net/wangjian8006/article/details/7871889 ...
- 模板C++ 03图论算法 1最短路之单源最短路(SPFA)
3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...
随机推荐
- TED #10# A rite of passage for late life
Bob Stein: A rite of passage for late life Collection I grew up white, secular and middle class in 1 ...
- SNMP学习笔记之SNMP TRAP简介、流程以及使用Python实现接受Trap信息
0x00 SNMP TRAP简介 SNMP(Simple Network Management Protocol) trap是一种很有用,但是也容易让人难以理解的协议. 虽然名字叫做简单网络管理协议, ...
- 关于微信分享到朋友圈(Thinkphp-tp3.2框架下实现)
PHP部分 扩展类代码部分: <?php namespace Think; class JsSdk { private $appId; private $appSecret; public $d ...
- linux django 知识点 安装mysql数据库 和 pycharm
django 命令及相关知识点 1. 启动 pycharm 命令:sh pycharm.sh 2. 创建 django 项目 : django-admin.py startproject Hello ...
- 探索Java8:Stream的使用
Java 8 API添加了一个新的抽象称为流Stream,可以让你以一种声明的方式处理数据. Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方式来提供一种对 Java 集合运算和表达 ...
- C#调用托管ocx、dll
前篇文章是调用非托管,比较复杂,这里是调用托管,很简单[所以在遇到非托管dll时可以通过二次封装成托管的方式,再通过这边文章来使用] 1.注意这是基于COM的ocx或者dll,所以用regsvr32先 ...
- vim的个性化配置- 再谈vim的折叠和展开 -- 彻底掌握vim 的展开和折叠!
http://www.wklken.me/posts/2016/02/03/some-vim-configs.html 一般把 设置成 逗号, 是比较好的, 因为逗号比默认的leader 要方便键入 ...
- How can I list all foreign keys referencing a given table in SQL Server?
How can I list all foreign keys referencing a given table in SQL Server? how to check if columns in ...
- 第十一章 非对称加密算法--DH
注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第8章“高等加密算法--非对称加密算法” 11.1.非对称加密算法 特点: 发送方和接收方均有一个密钥对(公钥+私钥),其中公 ...
- triggerHandler不执行事件默认值
<input type="text" /> $('input').triggerHandler('focus');