链接:http://acm.hdu.edu.cn/showproblem.php?pid=4849
题目:
Wow! Such City!
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 253 Accepted Submission(s): 89
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 C i, j (a positive integer) for traveling from city i to city j. Please note that C i, j may not equal to C j, 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 ≤ 10 6) 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:
C i, j is generated in the following way:
Given integers X 0, X 1, Y 0, Y 1, (1 ≤ X 0, X 1, Y 0, Y 1≤ 1234567), for k ≥ 2 we have
Xk = (12345 + X k-1 * 23456 + X k-2 * 34567 + X k-1 * X k-2 * 45678) mod 5837501
Yk = (56789 + Y k-1 * 67890 + Y k-2 * 78901 + Y k-1 * Y k-2 * 89012) mod 9860381
The for k ≥ 0 we have
Z k = (X k * 90123 + Y k ) mod 8475871 + 1
Finally for 0 ≤ i, j ≤ N - 1 we have
C i, j = Z i*n+j for i ≠ j
C i, 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 C i, j (a positive integer) for traveling from city i to city j. Please note that C i, j may not equal to C j, 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 ≤ 10 6) 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:
C i, j is generated in the following way:
Given integers X 0, X 1, Y 0, Y 1, (1 ≤ X 0, X 1, Y 0, Y 1≤ 1234567), for k ≥ 2 we have
Xk = (12345 + X k-1 * 23456 + X k-2 * 34567 + X k-1 * X k-2 * 45678) mod 5837501
Yk = (56789 + Y k-1 * 67890 + Y k-2 * 78901 + Y k-1 * Y k-2 * 89012) mod 9860381
The for k ≥ 0 we have
Z k = (X k * 90123 + Y k ) mod 8475871 + 1
Finally for 0 ≤ i, j ≤ N - 1 we have
C i, j = Z i*n+j for i ≠ j
C i, 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,X 0,X 1,Y 0,Y 1.See the description for more details.
For each test case, there is only one line containing 6 integers N,M,X 0,X 1,Y 0,Y 1.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
题意:
让你求出一个n*n的矩阵,从0这个点出发,到其他点的最短距离。
解题思路:
单源最短路径,Dijkstra。ps:注意中间处理时x、y可能会超int。
代码:
#include <cstdio>
#include <cstring>
#include <limits.h>
using namespace std;
const int MAXN = 1010;
long long x[MAXN*MAXN], y[MAXN*MAXN], z[MAXN*MAXN];
int map[MAXN][MAXN], d[MAXN];
bool vis[MAXN];
int n, m, ans;
void Dijkstra(int s)
{
memset(vis, false, sizeof(vis));
vis[s] = true;
for(int i = 0; i < n; i++)
{
d[i] = map[s][i];
}
d[s] = 0;
for(int i = 0; i < n - 1; i++)
{
int min = INT_MAX, k = -1;
for(int j = 0; j < n; j++)
{
if(!vis[j] && min > d[j])
{
min = d[j];
k = j;
}
}
vis[k] = true;
for(int j = 0; j < n; j++)
{
if(!vis[j] && d[k] + map[k][j] < d[j])
{
d[j] = d[k] + map[k][j];
}
}
}
//printf("%d : \n", s);
for(int i = 0; i < n; i++)
{
int tmp = d[i] % m;
if(i != s && tmp < ans)
{
ans = tmp;
}
// printf("%d ", d[i]);
}
// puts("");
}
int main()
{
while(~scanf("%d%d%I64d%I64d%I64d%I64d",
&n, &m, &x[0], &x[1], &y[0], &y[1]))
{
int t = n * n;
for(int i = 2; i < t; 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;
}
for(int i = 0; i < t; i++)
{
z[i] = (x[i] * 90123 + y[i]) % 8475871 + 1;
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
map[i][j] = (i == j ? 0 : z[i*n+j]);
// printf("%d ", map[i][j]);
}
// puts("");
}
ans = INT_MAX;
Dijkstra(0);
printf("%d\n", ans);
}
return 0;
}