标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4849
3 10 1 2 3 4 4 20 2 3 4 5
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 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.
特别注意: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;
}hdu 4849 Wow! Such City! (最短路spfa)
标签:
原文地址:http://blog.csdn.net/qiqi_skystar/article/details/51354211