模拟 + 最短路 之 hdu 4849 Wow! Such City!

//  [7/26/2014 Sjm]
/*
此题看懂题意,就可以了。。。模拟+最短路。。。
*/
 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <algorithm>
 5 using namespace std;
 6 typedef __int64 int64;
 7 const int MAX = 1005;
 8 const int INF = 0x3f3f3f3f;
 9 int64 N, M, X0, X1, Y0, Y1;
10 int64 X[MAX*MAX], Y[MAX*MAX], Z[MAX*MAX], C[MAX][MAX];
11 int64 d[MAX];
12 bool used[MAX];
13 
14 void Dijkstra(int64 s) {
15     fill(d, d + N, INF);
16     fill(used, used + N, false);
17 
18     d[s] = 0;
19 
20     while (true) {
21         int64 v = -1;
22         for (int64 u = 0; u < N; ++u) {
23             if (!used[u] && (-1 == v || d[u] < d[v])) {
24                 v = u;
25             }
26         }
27 
28         if (-1 == v) break;
29         used[v] = true;
30 
31         for (int u = 0; u < N; ++u) {
32             d[u] = min(d[u], d[v] + C[v][u]);
33         }
34     }
35 }
36 
37 int main() {
38     //freopen("input.txt", "r", stdin);
39     while (~scanf("%I64d %I64d %I64d %I64d %I64d %I64d", &N, &M, &X[0], &X[1], &Y[0], &Y[1])) {
40         Z[0] = ((X[0] * 90123) % 8475871 + Y[0] % 8475871) % 8475871 + 1;
41         Z[1] = ((X[1] * 90123) % 8475871 + Y[1] % 8475871) % 8475871 + 1;
42         for (int i = 2; i < N*N; ++i) {
43             X[i] = (12345 + (X[i - 1] * 23456) % 5837501 + (X[i - 2] * 34567) % 5837501 + (X[i - 1] * X[i - 2] * 45678) % 5837501) % 5837501;
44             Y[i] = (56789 + (Y[i - 1] * 67890) % 9860381 + (Y[i - 2] * 78901) % 9860381 + (Y[i - 1] * Y[i - 2] * 89012) % 9860381) % 9860381;
45             Z[i] = ((X[i] * 90123) % 8475871 + Y[i] % 8475871) % 8475871 + 1;
46         }
47         for (int i = 0; i < N; ++i) {
48             for (int j = 0; j < N; ++j) {
49                 if (i == j) C[i][j] = 0;
50                 else C[i][j] = Z[i*N + j];
51             }
52         }
53         /*
54         for (int i = 0; i < N*N; ++i) { cout << X[i] << "  "; }
55         cout << endl;
56         for (int i = 0; i < N*N; ++i) { cout << Y[i] << "  "; }
57         cout << endl;
58         for (int i = 0; i < N; ++i) {
59             for (int j = 0; j < N; ++j) {
60                 cout << C[i][j] << "  ";
61             }
62             cout << endl;
63         }
64         cout << endl;
65         */
66         Dijkstra(0);
67 
68         int64 ans = INF;
69         for (int i = 1; i < N; ++i) {
70             ans = min(ans, d[i] % M);
71         }
72 
73         printf("%d
", ans);
74     }
75     return 0;
76 }
原文地址:https://www.cnblogs.com/shijianming/p/4140815.html