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
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 4
4 20 2 3 4 5
Sample Output
1
10
For the first test case, we have
0 1 2 3 4 5 6 7 8
X 1 2 185180 788997 1483212 4659423 4123738 2178800 219267
Y 3 4 1633196 7845564 2071599 4562697 3523912 317737 1167849
Z 90127 180251 1620338 2064506 625135 5664774 5647950 8282552 4912390
the cost matrix C is
0 180251 1620338
2064506 0 5664774
5647950 8282552 0
Hint
So 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.
就是让求最小的dis[i]%m的,饿哦理解错了,伤心
代码
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
#include<utility>
#include<iomanip>
#include<time.h>
#include<iostream>
#define lowbit(x) (x&-x)
#define abs(x) ((x)>0?(x):-(x))
using namespace std;
typedef long long ll;
const double Pi = acos(-1.0);
const int N = 1e6+10, M = 1e3+20, mod = 1e9+7, inf = 2e9+10;
const double e=2.718281828459 ;
const double esp=1e-9;
struct node
{
ll x,y,z;
} a[N];
int c[M][M];
ll dis[M];
int book[M];
int main()
{
ll n,m,x0,y0,x1,y1;
while(~scanf("%I64d%I64d",&n,&m))
{
memset(book,0,sizeof(book));
scanf("%I64d%I64d%I64d%I64d",&x0,&x1,&y0,&y1);
a[0].x=x0;
a[0].y=y0;
a[1].x=x1;
a[1].y=y1;
for(ll i=0; i<=n*n+n; i++)
{
if(i==1||i==0)
{
a[i].z=(a[i].x*90123+a[i].y)%8475871+1;
}
else
{
a[i].x=(12345 + a[i-1].x * 23456 + a[i-2].x * 34567 + a[i-1].x * a[i-2].x * 45678)%5837501;
a[i].y= (56789 + a[i-1].y * 67890 +a[i-2].y* 78901 + a[i-1].y *a[i-2].y* 89012)%9860381;
a[i].z=(a[i].x*90123+a[i].y)%8475871+1;
}
}
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(i==j) c[i][j]=0;
else
{
c[i][j]=a[i*n+j].z;
}
}
}
ll minn=inf,u;
for(int i=0; i<n; i++)
{
dis[i]=c[0][i];
}
dis[0]=0;
for(int i=0; i<n-1; i++)
{
minn=inf;
for(int j=0; j<n; j++)
{
if(book[j]==0&&dis[j]<minn)
{
minn=dis[j];
u=j;
}
}
book[u]=1;
for(int v=0; v<n; v++)
{
if(c[u][v]<inf)
{
if(dis[v]>dis[u]+c[u][v])
dis[v]=dis[u]+c[u][v];
}
}
}
minn=inf;
for(int i=1;i<n;i++)
{
minn=min(minn,dis[i]%m);
}
printf("%I64d\n",minn);
}
return 0;
}