HDU 4849
来源:互联网 发布:淘宝卖家发货货到付款 编辑:程序博客网 时间:2024/06/07 22:13
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<set>#include<queue>#include<vector>using namespace std;#define N 1500#define LL long long #define inf 0x3f3f3f3fLL X[N*N], Y[N*N], Z[N*N], ans, m, n;struct edge{int form, to, next;LL w;}eg[N*N];int head[N*N], vis[N*N], t;LL dist[N*N];void add(int i, int j, LL w){eg[t].form = i;eg[t].to = j;eg[t].w = w;eg[t].next = head[i];head[i] = t++;}void spfa(int s){queue<int>q;memset(dist, inf, sizeof(dist));memset(vis, 0, sizeof(vis));q.push(s);dist[s] = 0;while (!q.empty()){int u = q.front();q.pop();vis[u] = 0;for (int i = head[u]; ~i; i = eg[i].next){int v = eg[i].to;if (dist[v] > dist[u] + eg[i].w){dist[v] = dist[u] + eg[i].w;if (!vis[v]){vis[v] = 1;q.push(v);}}}}}int main(){#ifdef CDZSCfreopen("i.txt", "r", stdin);#endifwhile (~scanf("%d%d%lld%lld%lld%lld", &n, &m, &X[0], &X[1], &Y[0], &Y[1])){memset(head, -1, sizeof(head));t = 0;ans =inf;for (int k = 2; k<n*n; k++){X[k] = (12345 + X[k - 1] * 23456 + X[k - 2] * 34567 + X[k - 1] * X[k - 2] * 45678) % 5837501;Y[k] = (56789 + Y[k - 1] * 67890 + Y[k - 2] * 78901 + Y[k - 1] * Y[k - 2] * 89012) % 9860381;}for (int k = 0; k<n*n; k++){Z[k] = (X[k] * 90123 + Y[k]) % 8475871 + 1;}for (int i = 0; i<n; i++){for (int j = 0; j<n; j++){if (i != j){add(i + 1, j + 1, Z[i*n + j]);}}}spfa(1);for (int i = 2; i <= n; i++){if (ans>dist[i] % m){ans = dist[i] % m;}}printf("%lld\n", ans);}return 0;}
哦呵呵哈。。。。。。。。。。。。。。。。。。。。。。。。。数组开小了,无限超时。。。。题意从0点到其他点的最短路,然后对所有的最短路对m求余,输出最小的那个值。数组开小了,开始没注意,后来注意到这里的一维数组的大小应该是N*N呀!!!
Wow! Such City!
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 1412 Accepted Submission(s): 481
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 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 0HintSo 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.
Source
2014西安全国邀请赛
0 0
- hdu 4849
- HDU 4849
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- [54]Spiral Matrix
- Mac常用操作和设置
- Io及多线程
- Cisco交换机常用命令
- java学习资料
- HDU 4849
- hdoj2669Romantic【扩展欧几里得】
- WordPress代码高亮插件CodeColorer正确使用方法
- 【Java Web】在Eclipse下配置Tomcat服务器
- 请不要创新,打好基础,一切从模仿开始,加油
- ZooKeeper数据模型
- 安全参透之旅第2章 fping工具使用
- java基本教程之join方法详解 java多线程教程
- iOS经典讲解之Swift枚举(Enum)、协议(protocol)、扩展(Extension)