hdu 4849 Wow! Such City! (最短路spfa)

题目链接: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): 1486    Accepted Submission(s): 511

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.

Source

2014西安全国邀请赛

题目大意:  给你一个计算公式,要你根据所给的公式算出N个顶点的图的任意两点间的距离(即权值),然后求出0号点到所有其他点的最短距离,最后输出这些最短距离中%m最小的值即可。

特别注意:1、取模

2、权值注意超出int范围要用lld

3、数组开到n*n+n

详见代码。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
ll inf=0x3f3f3f3f;
ll x[1010*1010+1010],y[1010*1010+1010],z[1010*1010+1010];
int Map[1010][1010];
int ans[1010],vis[1010];
int n,m;

void init()
{
    //memset(vis,0,sizeof(vis));
    for(int i=2; i<(n-1)*n+n; i++)
    {
        x[i]=(12345+x[i-1]*23456%5837501+x[i-2]*34567%5837501+(x[i-1]*x[i-2]%5837501)*45678%5837501)%5837501;
        y[i]=(56789+y[i-1]*67890%9860381+y[i-2]*78901%9860381+(y[i-1]*y[i-2]%9860381)*89012%9860381)%9860381;
    }
    for(int k=0; k<(n-1)*n+n; k++)
    {
        z[k]= (x[k]*90123%8475871+y[k])%8475871+1;
    }
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            if(i==j)
            {
                Map[i][j]=0;
            }
            else
            {
                Map[i][j]= z[i*n+j];
            }
        }
    }

}

void spfa()
{
    for (int i=0; i<n; i++)
    {
        ans[i]=inf;
        vis[i]=0;
    }
    queue<int>q;
    vis[0]=1;
    ans[0]=0;
    q.push(0);
    while (!q.empty())
    {
        int s=q.front();
        q.pop();
        vis[s]=0;
        for (int i=0; i<n; i++)
        {
            if (ans[i]>Map[s][i]+ans[s])
            {
                ans[i]=Map[s][i]+ans[s];
                if (!vis[i])
                {
                    q.push(i);
                    vis[i]=1;
                }
            }
        }
    }
}

int main()
{
    while (~scanf("%d%d%lld%lld%lld%lld",&n,&m,&x[0],&x[1],&y[0],&y[1]))
    {
        init();
        spfa();
        int Min=ans[1]%m;
        for (int i=2; i<n; i++)
        {
            if (Min>ans[i]%m)
                Min=ans[i]%m;
        }
        printf ("%d\n",Min);
    }
    return 0;
}
时间: 2024-10-12 20:36:30

hdu 4849 Wow! Such City! (最短路spfa)的相关文章

HDU 4849 Wow! Such City!陕西邀请赛C(最短路)

HDU 4849 Wow! Such City! 题目链接 题意:按照题目中的公式构造出临接矩阵后,求出1到2 - n最短路%M的最小值 思路:就根据题目中方法构造矩阵,然后写一个dijkstra,利用d数组取求答案即可 代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const long long I

hdu 4849 Wow! Such City!(dijstra)

题目链接:hdu 4849 Wow! Such City! 题目大意:有N个城市,给定计算两两城市距离的公式,然后求0到1~N-1的城市中,最短路径模掉M的最小值. 解题思路:先根据公式求出距离C矩阵,注意中间连乘3次的可能爆long long,然后用裸的dijstra算法求最短路. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using name

HDU 4849 - Wow! Such City!

怎么说吧,就是一个又臭又长又水的单源最短路径外加算算MOD的题. 1 #include<cstdio> 2 #include<queue> 3 #include<cstring> 4 #define MAXN 1003 5 #define MAXK 1000*1000+1000 6 #define INF 0x3f3f3f3f 7 using namespace std; 8 typedef long long ll; 9 int n,m; 10 ll x[MAXK],

hdu4849 Wow! Such City!(最短路dijkstra)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4849 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 co

hdu 4849 最短路 西安邀请赛 Wow! Such City!

http://acm.hdu.edu.cn/showproblem.php?pid=4849 会有很多奇怪的Wa的题,当初在西安就不知道为什么wa,昨晚做了,因为一些Sb错误也wa了很久,这会儿怎么写都会AC.... 收获: 1.还是基本都构思好在去敲代码,因为当时没过,昨晚心里有阴影,敲得很慢,而且最开始各种取模以防止漏掉,太保守了......以后一定先估算是不是需要取模防止TLE,当然时间够的话还是适当多取个模防止莫名其妙的错误.. 2.如果出错,注意参数是不是对的,最开始写好之后,因为m和

hdu 2962 Trucking (二分+最短路Spfa)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1763    Accepted Submission(s): 618 Problem Description A certain local trucking co

hdu 4849

简单题,公式计算+最短路.注意点:注意1 取模,2 数组开到n*n+n. #include<iostream> #include<queue> using namespace std; long long x[1234567];long long y[1234567];long long z[1234567]; int c[1001][1001]; int d[1002];int inq[1002]; const int inf=0x3f3f3f3f; int n,m; void

hdu 1690 Bus System(Dijkstra最短路)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690 Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6569    Accepted Submission(s): 1692 Problem Description Because of the huge popula

HDU 5352 MZL&#39;s City(2015 多校 第5场,最小费用最大流)

  MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 291    Accepted Submission(s): 94 Problem Description MZL is an active girl who has her own country. Her big country has N cities nu