hdu4849 Wow! Such City!

来源:互联网 发布:经传软件跟风统计 编辑:程序博客网 时间:2024/05/17 13:43

hdu4849 Wow! Such City!(最短路dijkstra)





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.

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


dijkstra代码如下:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #include <cstdio>  
  2. #include <iostream>  
  3. #include <algorithm>  
  4. #define MAX 1017  
  5. #define INF 1000000000  
  6. using namespace std;  
  7. __int64 c[MAX][MAX];  
  8. __int64 x[MAX*MAX], y[MAX*MAX], z[MAX*MAX];  
  9.   
  10. void dijkstra (__int64 mat[][MAX],int n, int s,int f)  
  11. {//s为起点, f:为终点  
  12.     __int64 dis[MAX];//记录到任何点的最短距离  
  13.     __int64 mark[MAX];//记录被选中的结点   
  14.     int i,j,k = 0;  
  15.     for(i = 0 ; i < n ; i++)//初始化所有结点,每个结点都没有被选中   
  16.         mark[i] = 0;  
  17.     for(i = 0 ; i < n ; i++)//将每个结点到start结点weight记录为当前distance   
  18.     {  
  19.         dis[i] = mat[s][i];  
  20.         //path[i] = s;  
  21.     }  
  22.     mark[s] = 1;//start结点被选中   
  23.     //path[s] = 0;  
  24.     dis[s] = 0;//将start结点的的距离设置为0   
  25.     __int64 min ;//设置最短的距离。   
  26.     for(i = 1 ; i < n; i++)  
  27.     {  
  28.         min = INF;  
  29.         for(j = 0 ; j < n;j++)  
  30.         {  
  31.             if(mark[j] == 0  && dis[j] < min)//未被选中的结点中,距离最短的被选中   
  32.             {  
  33.                 min = dis[j] ;  
  34.                 k = j;  
  35.             }  
  36.         }  
  37.         mark[k] = 1;//标记为被选中   
  38.         for(j = 0 ; j < n ; j++)  
  39.         {  
  40.             if( mark[j] == 0  && (dis[j] > (dis[k] + mat[k][j])))//修改剩余结点的最短距离   
  41.             {  
  42.                 dis[j] = dis[k] + mat[k][j];  
  43.             }  
  44.         }  
  45.     }   
  46. }   
  47. int main()  
  48. {  
  49.     int n, m;  
  50.     int i, j;  
  51.       
  52.     while(~scanf("%d%d",&n,&m))  
  53.     {  
  54.         scanf("%I64d%I64d%I64d%I64d",&x[0],&x[1],&y[0],&y[1]);  
  55.         for(i = 0; i < 2; i++)  
  56.         {  
  57.             z[i] = (x[i]*90123+y[i])%8475871+1;  
  58.         }  
  59.         for(i = 2; i <= n*n; i++)  
  60.         {  
  61.             x[i]=(12345+x[i-1]*23456+x[i-2]*34567+x[i-1]*x[i-2]*45678)%5837501;  
  62.             y[i]=(56789+y[i-1]*67890+y[i-2]*78901+y[i-1]*y[i-2]*89012)%9860381;  
  63.             z[i]=(x[i]*90123+y[i])%8475871+1;  
  64.         }  
  65.         for(i = 0; i < n; i++)  
  66.         {  
  67.             for(j = 0; j < n; j++)  
  68.             {  
  69.                 if(i == j)  
  70.                     c[i][j] = 0;  
  71.                 else  
  72.                     c[i][j] = z[i*n+j];  
  73.             }  
  74.         }  
  75.         __int64 mmax = INF, ans;  
  76.         dijkstra(c,n,0,n-1);  
  77.         for(i = 1; i < n; i++)  
  78.         {  
  79.             ans = dis[i];  
  80.             if(ans == 0)  
  81.                 continue;  
  82.             ans%=m;  
  83.             if(mmax > ans)  
  84.             {  
  85.                 mmax = ans;  
  86.             }  
  87.         }  
  88.         printf("%I64d\n",mmax);  
  89.     }  
  90.     return 0;  
  91. }  
0 0