Wow! Such City!(最短路)

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 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.
 

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.
 

Source
 

Recommend
liuyiding


这题wa了十多次,原来是没有将标记数组的下标改为j的缘故。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
#define CRL(a) memset(a,0,sizeof(a))
#define inf 0x3f3f3f3f3f3f3f3fLL
#define T 1005
ll x[T*T],y[T*T],map[T][T],bo[T];
int n,m;
ll Cx(int i)
{
	return (12345+x[i-1]*23456+x[i-2]*34567+x[i-1]*x[i-2]*45678)%5837501;
}
ll Cy(int i)
{
	return (56789 + y[i-1]*67890+y[i-2]*78901+y[i-1]*y[i-2]*89012)%9860381;
}
ll Cz(int i)
{
	return (x[i]*90123+y[i])%8475871+1;
}
void dijkstra()
{
	int i,j;
	for(i=0;i<n;++i){
		x[i] = map[0][i];
	}CRL(bo);bo[0] = 1;
	for(i=0;i<n-1;++i)
	{	int u=0;ll mi=inf;
		for(j=0;j<n;++j){
			if(!bo[j]&&mi>x[j]){
				mi = x[j];
				u = j;
			}
		}
		bo[u]=1;
		for(j=0;j<n;++j){
				if(!bo[j]&&x[j]>map[u][j]+mi){
					x[j] = map[u][j]+mi;
				}
		}
	}
}
int main()
{
	/*freopen("input.txt","r",stdin);*/
	int i,t;
	/*printf("%I64d",inf);*/
	while(~scanf("%d%d%I64d%I64d%I64d%I64d"
		  ,&n,&m,&x[0],&x[1],&y[0],&y[1]))
	{
		t = n*n;
		map[0][0]=inf,map[0][1]=Cz(1);
		for(i=2;i<t;++i){
			x[i] = Cx(i),y[i]=Cy(i);
			if(i/n!=i%n)
				map[i/n][i%n]=Cz(i);
			else
				map[i/n][i%n]=inf;
		}
		dijkstra();
		ll mi=0x3f3f3f3f;
		for(i=0;i<n;++i){
			if(x[i]!=inf)
			mi = min((ll)mi,x[i]%m);
		}
		printf("%I64d\n",mi);
	}
	return 0;
}

对spfa的head与next还有点没理解透,到时再写完它吧。
spfa做法:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
typedef long long ll;
#define CRL(a) memset(a,0,sizeof(a))
#define inf 0x3f3f3f3f3f3f3f3fLL
#define T 1005
ll x[T*T],y[T*T],bo[T],head[T];
int n,m;
ll Cx(int i)
{
	return (12345+x[i-1]*23456+x[i-2]*34567+x[i-1]*x[i-2]*45678)%5837501;
}
ll Cy(int i)
{
	return (56789 + y[i-1]*67890+y[i-2]*78901+y[i-1]*y[i-2]*89012)%9860381;
}
ll Cz(int i)
{
	return (x[i]*90123+y[i])%8475871+1;
}
int Count;
struct node
{
	int from,to,next,w;//所在地,目的地,?,权值
	void add(int i,int j,int k){
		from = i,to = j, w = k,next = head[i];
		head[i] = Count++;
	}
}e[T*T];
void spfa()
{
	queue<int> a;
	int i,j,k,u;
	for(i=0;i<=n;++i){//0到所有点初始化为不可达
		x[i] = inf; 
	}CRL(bo);
	a.push(0);x[0] = 0;//0-0可达要写
	while(!a.empty())
	{
		u = a.front();a.pop();
		bo[u] = 0;
		for(i=head[u];i!=-1;i=e[i].next){
			int v = e[i].to;
			if(x[v]>e[i].w+x[u]){
				x[v] = e[i].w+x[u];
				if(!bo[v]){
					bo[v] = 1;
					a.push(v);
				}
			}
		}
	}
}
int main()
{
	/*freopen("input.txt","r",stdin);*/
	int i,t;
	/*printf("%I64d",inf);*/
	while(~scanf("%d%d%I64d%I64d%I64d%I64d"
		  ,&n,&m,&x[0],&x[1],&y[0],&y[1]))
	{
		Count = 0;
		t = n*n;memset(head,-1,sizeof(head));
		e[Count].add(0,1,Cz(1));
		for(i=2;i<t;++i){
			x[i] = Cx(i),y[i]=Cy(i);
			if(i/n!=i%n)
				e[Count].add(i/n,i%n,Cz(i));
		}
		spfa();
		ll mi=inf;
		for(i=1;i<n;++i){
			if(x[i]!=inf)
			mi = min((ll)mi,x[i]%m);
		}
		printf("%I64d\n",mi);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值