点击打开链接

Wow! Such City!

Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 2366    Accepted Submission(s): 808

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 For the first test case, we have 0 1 2 3 4 5 6 7 8 X 1 2 185180 788997 1483212 4659423 4123738 2178800 219267 Y 3 4 1633196 7845564 2071599 4562697 3523912 317737 1167849 Z 90127 180251 1620338 2064506 625135 5664774 5647950 8282552 4912390 the cost matrix C is 0 180251 1620338 2064506 0 5664774 5647950 8282552 0

Hint

So the minimal cost from city 0 to city 1 is 180251, while the distance to city 2 is 1620338. Given M = 10, city 1 and city 2 belong to category 1 and 8 respectively. Since only category 1 and 8 contain at least one city, the minimal one of them, category 1, is the desired answer to Doge’s question.

Source
2014西安全国邀请赛
Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6275 6274 6273 6272 6271 

题解:

根据给出的计算公式,计算任意两点之间的距离,然后求出起点0到其他n-1个点的最短距离,

输出最短距离对m取余之后的最小值

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
ll inf=0x3f3f3f3f;
ll x[1010*1010+1010],y[1010*1010+1010],z[1010*1010+1010];
int Map[1010][1010];
int ans[1010];//ans[i]表示起点到i的最短距离
int vis[1010];//vis[i]表示i是否在队列中
int n,m;void init()
{   //根据MAP数组的计算公式,i的最大值为n*nfor(int i=2; i<(n-1)*n+n; i++){   //这里和题目给定的公式不同,实际计算需要边计算,边取模x[i]=(12345+x[i-1]*23456%5837501+x[i-2]*34567%5837501+(x[i-1]*x[i-2]%5837501)*45678%5837501)%5837501;y[i]=(56789+y[i-1]*67890%9860381+y[i-2]*78901%9860381+(y[i-1]*y[i-2]%9860381)*89012%9860381)%9860381;}for(int k=0; k<(n-1)*n+n; k++){z[k]= (x[k]*90123%8475871+y[k])%8475871+1;}for(int i=0; i<n; i++){for(int j=0; j<n; j++){if(i==j){Map[i][j]=0;}else{Map[i][j]= z[i*n+j];}}}}void spfa()
{for (int i=0; i<n; i++){ans[i]=inf;vis[i]=0;}queue<int>q;vis[0]=1;ans[0]=0;q.push(0);while (!q.empty()){int s=q.front();q.pop();vis[s]=0;for (int i=0; i<n; i++){if (ans[i]>Map[s][i]+ans[s]){ans[i]=Map[s][i]+ans[s];if (!vis[i]){q.push(i);vis[i]=1;}}}}
}int main()
{while (~scanf("%d%d%lld%lld%lld%lld",&n,&m,&x[0],&x[1],&y[0],&y[1])){init();spfa();int Min=ans[1]%m;for (int i=2; i<n; i++){if (Min>ans[i]%m)Min=ans[i]%m;}printf ("%d\n",Min);/*for(int i=0;i<n;i++)printf("%d %d\n",ans[i],ans[i]%m);*/}return 0;
}

HDU4809 Wow! Such City! Dijkstra算法相关推荐

  1. 基于Dijkstra算法的武汉地铁路径规划!

    ↑↑↑关注后"星标"Datawhale 每日干货 & 每月组队学习,不错过 Datawhale干货 作者:牧小熊,华中农业大学,Datawhale原创作者 前言 最近爬取了 ...

  2. 基于Dijkstra算法的武汉地铁路径规划!(附下载)

    来源:Datawhale 本文约3300字,建议阅读10分钟 本文为你详解路径规划项目,附源码链接. 前言 最近爬取了武汉地铁线路的信息,通过调用高德地图的api 获得各个站点的进度和纬度信息,使用D ...

  3. 【爬虫、算法】基于Dijkstra算法的武汉地铁路径规划!

    作者:牧小熊,华中农业大学,Datawhale原创作者 前言 最近爬取了武汉地铁线路的信息,通过调用高德地图的api 获得各个站点的进度和纬度信息,使用Dijkstra算法对路径进行规划. 1.数据爬 ...

  4. pta7-7旅游规划(dijkstra算法)

    题目链接:https://pintia.cn/problem-sets/1101307589335527424/problems/1101314114762387456 题意:给n给城市,m条公路,公 ...

  5. Python、C/C++混编实现最短路径可视化—Dijkstra算法

    摘 要   本文讲述的是主要是运用C/C++语言Dijkstra算法来完成交通图的存储.图中任一顶点到其余任意一顶点间的最短路径问题,并利用Python中的复杂网络分析库Networkx来绘制有向图以 ...

  6. 合肥市出行地铁路径规划——基于Dijkstra算法

    合肥市出行地铁路径规划--基于Dijkstra算法 1. 引言 2. 导入相应的模块 3. 申请高德地图的API 4. 获取合肥地铁数据 5. 计算合肥各地铁站点之间的距离 6.寻找最近的地铁站 7. ...

  7. PAT甲级1003 Emergency Dijkstra算法(堆优化版/朴素版)

    前言   最近花了很多的时间在写JAVA项目上面,疏忽了算法和数据结构的学习.最近突然醒悟基础更为重要,打算从今天开始每天抽出一些时间做下PAT甲级的题目.现有题库的前两题很简单,从第三题开始吧. 题 ...

  8. [C] Dijkstra算法——通过边实现松弛

    Dijkstra算法--通过边实现松弛 本算法学习指定一个点(源点)到其余各个顶点的最短路径,也叫做单源最短路径例如求下图1号顶点到2,3,4,5,6号顶点的最短路径 这个时候你可能就要问了,为什么不 ...

  9. 经典算法研究系列:二、Dijkstra 算法初探

    经典算法研究系列:二.Dijkstra 算法初探  July   二零一一年一月 ====================== 本文主要参考:算法导论 第二版.维基百科. 写的不好之处,还望见谅. 本 ...

最新文章

  1. codevs1032
  2. rabbitmq——镜像队列
  3. C语言enum关键字
  4. ffmpeg实现摄像头拉流_干货 | 速看!乐橙K32Famp;K36F摄像头全彩夜视功能的不同点全在这了!...
  5. 都市男女的32声叹息
  6. PHP set_error_handler()函数的使用【转载】
  7. 接口测试--ApiPost组成介绍
  8. JAVA程序设计(11)-----面对对象0基础设计 麻将 创建麻将牌 然后洗牌 发牌~ 恩 就这样...
  9. ❤️区块链Hyperledger Fabric 老版本 1.1.0 快速部署安装 教程合集❤️
  10. 代码分析系列 数3退1
  11. 谷歌无法加载印象笔记剪辑插件
  12. 【漏洞学习——XSS】TOM邮箱存储型XSS一枚
  13. pycharm安装及添加桌面图标
  14. Spring Boot 定制横幅banner与关闭banner
  15. 每日写题分享--包含min函数的栈/双栈实现
  16. Cadence OrCAD Capture 如何批量替换元器件
  17. Aspose.PDF 23.1.0 for .NET Crack
  18. Python - openpyxl Excel 操作示例与实践
  19. PC:各大主板开机启动项快捷键
  20. Unity变换矩阵之如何构建变换矩阵

热门文章

  1. Direct3D 11 Tutorial 4: 3D Spaces_Direct3D 11 教程4:3D空间
  2. 95. Unique Binary Search Trees II
  3. 微信公众号开发 回复事件(测试账号)
  4. NOI模拟题4 Problem C: 填格子(board)
  5. 【学习笔记 】sass教程巩固学习
  6. javascript之构造器
  7. HMM学习(3)-Patterns generated by a hidden process
  8. 学习 TTreeView [8] - AlphaSort、CustomSort、SortType
  9. ssh项目放到服务器上出现404,项目运行一段时间,后台程序无法启动,404错误
  10. 苹果手机夜间模式怎么设置_微信夜间模式终于来了,苹果和安卓都适用!