Hdu4849 wow! Such city! (Dijkstra)

Source: Internet
Author: User

Reprint please indicate the source: http://blog.csdn.net/u012860063? Viewmode = Contents

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4849


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  9860381The for k ≥ 0 we have                  Zk = (Xk * 90123 + Yk ) mod 8475871 + 1Finally 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 44 20 2 3 4 5

Sample output
110For the first test case, we have   0   1   2   3   4   5   6   7   8X   1   2 185180 7889971483212465942341237382178800 219267Y   3   4163319678455642071599456269735239123177371167849Z 90127 18025116203382064506 6251355664774564795082825524912390the cost matrix C is            0 1802511620338                2064506   05664774                56479508282552   0So 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.

Question: Find the shortest distance from start 0 to n-1! Note that you need to modulo m first and then find the shortest path. That is to say, the original path is not the shortest path, but after modulo m, it is the shortest distance!


The Dijkstra code is as follows:

# 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 is the start point, and F: Is the end point _ int64 dis [Max]; // record the shortest distance to any vertex _ int64 mark [Max]; // record the selected node int I, j, k = 0; for (I = 0; I <n; I ++) // Initialize all nodes. Each node is not selected. MARK [I] = 0; for (I = 0; I <N; I ++ )// Record weight from each node to the Start Node as the current distance {dis [I] = mat [s] [I]; // path [I] = s ;} mark [s] = 1; // Start Node Selected // path [s] = 0; DIS [s] = 0; // set the Start Node distance to 0 _ int64 min; // set the shortest distance. For (I = 1; I <n; I ++) {min = inf; For (j = 0; j <n; j ++) {If (MARK [J] = 0 & dis [J] <min) // unselected node with the shortest distance selected {min = dis [J]; k = J ;}} mark [k] = 1; // mark as selected for (j = 0; j <n; j ++) {If (MARK [J] = 0 & (DIS [J]> (DIS [k] + mat [k] [J]) // modify the shortest distance of the remaining node {dis [J] = dis [k] + mat [k] [J] ;}}} int main () {int N, m; int I, j; while (~ Scanf ("% 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; elsec [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 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.