HDU-4849 Wow! Such City! (单源最短路)
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
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.
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
题目分析:按题意将那个表示距离的矩阵处理出来,然后就是单源最短路了。
代码如下:
# 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; }