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) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...
随机推荐
- php 截取字符串第一个字符,截取掉字符串最后一个字符的方法
php 截取字符串第一个字符,php截取掉字符串最后一个字符的方法: $frist = substr( $c_url, 0, 1 ); $delete_last = substr(base_url() ...
- MAC安装最新datagrip之后无法非官方激活,而且启动过慢
由于之前安装过,更新最新版本之后发现不能使用(http://xidea.online)激活??? 解决方法:使用CleanMyMac等相应软件删除之后,还要去相应的保存记录的路径(/Users/用户名 ...
- 负载均衡之-haproxy
老规矩,先介绍?复制一段? 1)HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案. 2)HAProxy特别适用于那些负载特大的 ...
- 根据wsdl文件,soupUI生成webservice客户端代码
根据wsdl文件,soupUI生成webservice客户端代码 功能介绍: 对于面向WebServie接口开发时,当我们已经获取到WSDL文件后,可以使用soapUI工具生成对应的客户端和服务端代码 ...
- 07: jquery.cookie操作cookie
1.1 jquery.cookie常用方法 定义:让网站服务器把少量数据储存到客户端的硬盘或内存,从客户端的硬盘读取数据的一种技术 1. 添加一个"会话cookie" $.cook ...
- 20145127《java程序设计》第八周学习总结
一.教材学习内容总结 第十四章 NIO与NIO2 NIO(New IO)-from JDK1.4 NIO2 -from Java SE 7 14.1 认识NIO Channel: 衔接数据节点(与IO ...
- 20145205武钰《网络对抗》web安全基础实践
实验后问题回答 (1)SQL注入攻击原理,如何防御 攻击原理:SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意SQL命令的目的 防御手 ...
- CodeForces 471D MUH and Cube Walls -KMP
Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of ...
- Python3基础 json.loads 解析json格式的数据,得到一个字典
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- linux下带有空格的文件怎么删除
如:hello world文件 第一种方式 先用 ls -i 得到 hello world 的inod(就是最前面的数字)假设这个数字是123,然后find . -inum -exec rm {} \ ...