转载请注明出处:http://blog.csdn.net/u012860063?

viewmode=contents

题目链接:

pid=4849">http://acm.hdu.edu.cn/showproblem.php?pid=4849

----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋http://user.qzone.qq.com/593830943/main

----------------------------------------------------------------------------------------------------------------------------------------------------------

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 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.

题意:找从起点0開始到n-1各点的最短距离!注意要先模m后再找最短距离,也就是说存在原路径并非最短距离,可是模上m后就是最短的距离的情况!

dijkstra代码例如以下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#define MAX 1017
#define INF 1000000000
using namespace std;
__int64 c[MAX][MAX];
__int64 x[MAX*MAX], y[MAX*MAX], z[MAX*MAX]; void dijkstra (__int64 mat[][MAX],int n, int s,int f)
{//s为起点。 f:为终点
__int64 dis[MAX];//记录到不论什么点的最短距离
__int64 mark[MAX];//记录被选中的结点
int i,j,k = 0;
for(i = 0 ; i < n ; i++)//初始化全部结点。每一个结点都没有被选中
mark[i] = 0;
for(i = 0 ; i < n ; i++)//将每一个结点到start结点weight记录为当前distance
{
dis[i] = mat[s][i];
//path[i] = s;
}
mark[s] = 1;//start结点被选中
//path[s] = 0;
dis[s] = 0;//将start结点的的距离设置为0
__int64 min ;//设置最短的距离。
for(i = 1 ; i < n; i++)
{
min = INF;
for(j = 0 ; j < n;j++)
{
if(mark[j] == 0 && dis[j] < min)//未被选中的结点中。距离最短的被选中
{
min = dis[j] ;
k = j;
}
}
mark[k] = 1;//标记为被选中
for(j = 0 ; j < n ; j++)
{
if( mark[j] == 0 && (dis[j] > (dis[k] + mat[k][j])))//改动剩余结点的最短距离
{
dis[j] = dis[k] + mat[k][j];
}
}
}
}
int main()
{
int n, m;
int i, j; while(~scanf("%d%d",&n,&m))
{
scanf("%I64d%I64d%I64d%I64d",&x[0],&x[1],&y[0],&y[1]);
for(i = 0; i < 2; i++)
{
z[i] = (x[i]*90123+y[i])%8475871+1;
}
for(i = 2; i <= n*n; i++)
{
x[i]=(12345+x[i-1]*23456+x[i-2]*34567+x[i-1]*x[i-2]*45678)%5837501;
y[i]=(56789+y[i-1]*67890+y[i-2]*78901+y[i-1]*y[i-2]*89012)%9860381;
z[i]=(x[i]*90123+y[i])%8475871+1;
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
if(i == j)
c[i][j] = 0;
else
c[i][j] = z[i*n+j];
}
}
__int64 mmax = INF, ans;
dijkstra(c,n,0,n-1);
for(i = 1; i < n; i++)
{
ans = dis[i];
if(ans == 0)
continue;
ans%=m;
if(mmax > ans)
{
mmax = ans;
}
}
printf("%I64d\n",mmax);
}
return 0;
}

hdu4849 Wow! Such City!(最短路dijkstra)的更多相关文章

  1. HDU-4849 Wow! Such City!,最短路!

    Wow! Such City!    题意:题面很难理解,幸亏给出了提示,敲了一发板子过了.给出x数组y数组和z数组的求法,并给出x.y的前几项,然后直接利用所给条件构造出z数组再构造出C数组即可,C ...

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

    Problem Description Doge, tired of being a popular image on internet, is considering moving to anoth ...

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

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

  4. hdu 2544 最短路 Dijkstra

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...

  5. 算法学习笔记(三) 最短路 Dijkstra 和 Floyd 算法

    图论中一个经典问题就是求最短路.最为基础和最为经典的算法莫过于 Dijkstra 和 Floyd 算法,一个是贪心算法,一个是动态规划.这也是算法中的两大经典代表.用一个简单图在纸上一步一步演算,也是 ...

  6. 单源最短路dijkstra算法&&优化史

    一下午都在学最短路dijkstra算法,总算是优化到了我能达到的水平的最快水准,然后列举一下我的优化历史,顺便总结总结 最朴素算法: 邻接矩阵存边+贪心||dp思想,几乎纯暴力,luoguTLE+ML ...

  7. HUD.2544 最短路 (Dijkstra)

    HUD.2544 最短路 (Dijkstra) 题意分析 1表示起点,n表示起点(或者颠倒过来也可以) 建立无向图 从n或者1跑dij即可. 代码总览 #include <bits/stdc++ ...

  8. 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树)

    layout: post title: 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树) author: "luowentaoaa" ca ...

  9. 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)

    layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...

随机推荐

  1. Oracle_exp/expdp备份

    目录索引 1.exp和expdp的区别 2.expdp导出数据库流程 一.↓↓exp和expdp的区别↓↓ 1.exp和expdp最明显的区别就是导出速度的不同.expdp导出是并行导出(如果把exp ...

  2. MAC软连接

    在mac上不设置环境变量有的时候也可以直接就访问到了某些文件.这个是为什么呢?答案是用了软连接. 1 查看加载文件 可以使用cat命令查看paths文件 cat etc/paths /usr/loca ...

  3. Eclipse 添加本地 SVN插件以及运行项目的流程

    去网上下载SVN插件包.里面包含文件如图: 把features和plugins文件夹里面的东西全部复制粘贴到eclipse安装目录下的features和plugins文件夹中就行.然后重启eclips ...

  4. [转]深入javascript——原型链和继承

    在上一篇post中,介绍了原型的概念,了解到在javascript中构造函数.原型对象.实例三个好基友之间的关系:每一个构造函数都有一个“守护神”——原型对象,原型对象心里面也存着一个构造函数的“位置 ...

  5. 分割字符串 ExtractStrings

    //分割字符串 ExtractStrings var s: String; List: TStringList; begin s := 'about: #delphi; #pascal, progra ...

  6. 团体程序设计天梯赛-练习集-L1-038. 新世界

    L1-038. 新世界 这道超级简单的题目没有任何输入. 你只需要在第一行中输出程序员钦定名言“Hello World”,并且在第二行中输出更新版的“Hello New World”就可以了. #in ...

  7. drf03 drf视图中提供的请求类和响应类

    drf除了在数据序列化部分简写代码以外,还在视图中提供了简写操作.所以在django原有的django.views.View类基础上,drf封装了多个子类出来提供给我们使用. Django REST ...

  8. Html 页面刷新后出现闪动

    Html 页面刷新后,或跳转后,出现闪动,抖动问题 1.查看有没有用到新字体,新字体链接位置是否存在 如: @font-face { font-family: "AvantGarde-Dem ...

  9. HDU 4405 Aeroplane chess(概率dp,数学期望)

    题目 http://kicd.blog.163.com/blog/static/126961911200910168335852/ 根据里面的例子,就可以很简单的写出来了,虽然我现在还是不是很理解为什 ...

  10. 纯CSS 3D翻转一个面(翻转导航菜单 立方体)

    在做练习的时候学到css的翻转导航菜单,原代码有点让人头疼,通过对其css的参数一点点研究了其实现过程. 这里推荐大家研究这个3D翻转动画的代码. 我的github:swarz,欢迎给老弟我++星星 ...