您的位置:首页 > 理论基础 > 计算机网络

Minimum Distance in a Star Graph(南宁网络赛)

2017-09-24 19:35 423 查看
In this problem, we will define a graph called star graph, and the question is to find the minimum distance between two given nodes in the star graph.

Given an integer nnn,
an n−dimensionaln-dimensionaln−dimensional
star graph, also referred to as SnS_{n}S​n​​,
is an undirected graph consisting of n!n!n!
nodes (or vertices) and ((n−1) ∗ n!)/2((n-1)\ *\ n!)/2((n−1) ∗ n!)/2
edges. Each node is uniquely assigned a label x1 x2 ... xnx_{1}\ x_{2}\ ...\ x_{n}x​1​​ x​2​​ ... x​n​​
which is any permutation of the n digits 1,2,3,...,n{1, 2, 3, ..., n}1,2,3,...,n.
For instance, an S4S_{4}S​4​​
has the following 24 nodes 1234,1243,1324,1342,1423,1432,2134,2143,2314,2341,2413,2431,3124,3142,3214,3241,3412,3421,4123,4132,4213,4231,4312,4321{1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413,
2431, 3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321}1234,1243,1324,1342,1423,1432,2134,2143,2314,2341,2413,2431,3124,3142,3214,3241,3412,3421,4123,4132,4213,4231,4312,4321.
For each node with label x1 x2x3 x4 ... xnx_{1}\ x_{2} x_{3}\ x_{4}\ ...\ x_{n}x​1​​ x​2​​x​3​​ x​4​​ ... x​n​​,
it has n−1n-1n−1
edges connecting to nodes x2 x1 x3 x4 ... xnx_{2}\ x_{1}\ x_{3}\ x_{4}\ ...\ x_{n}x​2​​ x​1​​ x​3​​ x​4​​ ... x<
1594f
span style="font-size:0em;">​n​​,
x3 x2 x1 x4 ... xnx_{3}\ x_{2}\ x_{1}\ x_{4}\ ...\ x_{n}x​3​​ x​2​​ x​1​​ x​4​​ ... x​n​​,
x4 x2 x3 x1 ... xnx_{4}\ x_{2}\ x_{3}\ x_{1}\ ...\ x_{n}x​4​​ x​2​​ x​3​​ x​1​​ ... x​n​​,
..., and xn x2 x3 x4 ... x1x_{n}\ x_{2}\ x_{3}\ x_{4}\ ...\ x_{1}x​n​​ x​2​​ x​3​​ x​4​​ ... x​1​​.
That is, the n−1n-1n−1
adjacent nodes are obtained by swapping the first symbol and the
d−thd-thd−th
symbol of x1 x2 x3 x4 ... xnx_{1}\ x_{2}\ x_{3}\ x_{4}\ ...\ x_{n}x​1​​ x​2​​ x​3​​ x​4​​ ... x​n​​,
for d=2,...,nd = 2, ..., nd=2,...,n.
For instance, in S4S_{4}S​4​​,
node 123412341234
has 333
edges connecting to nodes 213421342134,
321432143214,
and 423142314231.
The following figure shows how S4S_{4}S​4​​
looks (note that the symbols aaa,
bbb,
ccc,
and ddd
are not nodes; we only use them to show the connectivity between nodes; this is for the clarity of the figure).





In this problem, you are given the following inputs:

nnn:
the dimension of the star graph. We assume that nnn
ranges from 444
to 999.
Two nodes x1x_{1}x​1​​
x2x_{2}x​2​​
x3x_{3}x​3​​
... xnx_{n}x​n​​
and y1y_{1}y​1​​
y2y_{2}y​2​​
y3 ... yny_{3}\ ...\ y_{n}y​3​​ ... y​n​​
in SnS_{n}S​n​​.
You have to calculate the distance between these two nodes (which is an integer).

Input Format

nnn
(dimension of the star graph)

A list of 555
pairs of nodes.

Output Format

A list of 555
values, each representing the distance of a pair of nodes.

样例输入

4
1234 4231
1234 3124
2341 1324
3214 4213
3214 2143


样例输出

1
2
2
1
3


//题意:给定一个n,然后有 n! 个顶点,顶点的真实值分别为123...n,123...(n-1)n,... 举个例子:n=4,顶点真实值分别为

1234,1243,1324,1342,1423,1432,2134,2143,2314,2341,2413,2431,3124,3142,3214,3241,3412,3421,4123,4132,4213,4231,4312,4321

每个顶点仅与另外n-1个点之间有边,每个点与和它相连的点是有规律的(后面解释),且边权值都为 1 。仍举n=4的例子,1234与2134,3214,4231相连,2134与1234,3124,4132相连,abcd与bacd,cbad,dbca相连,就是这个顶点真实值的第一位数分别与它后面的各位数交换。

现在给你2个顶点的真实值,让你计算这两点间的最短路径。(仅5组)

//思路:写两个函数,一个是把顶点的真实值转换为序号,一个是把序号转换为顶点的真实值。比如:1234 -> 1,2134 -> 7;5 -> 1423, 8 -> 2143 ... 顶点的真实值越小,它的序号就越小,分别为1、2、3、... n! 。然后就可以把题目中描述的关系建一张图(9!
=362880  每个点最多连8条边,二维数组、vector都可以存),然后跑SPFA即可。

本人水平有限,这两个函数写的巨长...,写的自己都有点晕了...,还好RP够高,1A了,hhh

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

const int MAX=362880+10;
const int INF=1e9;

int n;
int num;
vector<int>map[MAX];

//把真实值转换为序号
int change(int x)
{
int temp=x;
int res=num;
int arr[100];
int ans[100];
int tot=0;
for(int i=0;;i++)
{
if(temp==0)
break;
arr[i]=temp%10;
temp=temp/10;
tot++;
}
for(int i=1;i<tot;i++)
ans[i]=i;
int sum=0;
for(int i=n;i>0;i--)
{
res=res/i;
int tmp=n-i+1;
int w=1;
for(int j=1;j<tot;j++)
{
if(ans[j]==-1)
continue;
if(ans[j]==arr[tot-tmp])
break;
w++;
}
sum+=res*(w-1);
ans[arr[tot-tmp]]=-1;
}
return sum+1;
}

//把序号转换为真实值
int trans(int x)
{
int temp=x;
int arr[100];
int ans[100];
int cnt=1;
int res=num;
for(int i=1;i<=n;i++)
ans[i]=i;
for(int i=n;i>0;i--)
{
res=res/i;
int w=1;
int tt=temp/res;
if(temp%res!=0)
tt++;
for(int j=1;j<=n;j++)
{
if(ans[j]==-1)
{
continue;
}
if(w==tt)
{
w=j;
break;
}
w++;
}
arr[cnt]=ans[w];
ans[w]=-1;
cnt++;
temp=temp-res*(temp/res);
if(temp==1||temp==0)
break;
}
if(temp==1)
{
for(int i=1;i<=n;i++)
{
if(ans[i]==-1)
continue;
arr[cnt++]=i;
}
}
else if(temp==0)
{
for(int i=n;i>=1;i--)
{
if(ans[i]==-1)
continue;
arr[cnt++]=i;
}
}
int sum=0;
for(int i=1;i<cnt;i++)
{
sum+=arr[i]*pow(10,n-i);
}
return sum;
}

//最短路径算法SPFA
int dis[MAX];
void SPFA(int x)
{
for(int i=1;i<=num;i++)
dis[i]=INF;
queue<int>q;
q.push(x);
dis[x]=0;
while(!q.empty())
{
int now=q.front();
q.pop();
for(int i=0;i<map[now].size();i++)
{
int v=map[now][i];
if(dis[v]>dis[now]+1)
{
dis[v]=dis[now]+1;
q.push(v);
}
}
}
}

int main()
{
scanf("%d",&n);
num=1;
for(int i=2;i<=n;i++)
num*=i;

//建图
for(int i=1;i<=num;i++)
{
int xnum=trans(i);
int temp=xnum;
int arr[100];
int cnt=1;
int sum;
for(int j=1;;j++)
{
arr[cnt++]=temp%10;
temp=temp/10;
if(temp==0)
break;
}
temp=arr
;
for(int j=1;j<n;j++)
{
sum=0;
arr
=arr[j];
arr[j]=temp;
for(int k=1;k<=n;k++)
{
sum+=arr[k]*pow(10,k-1);
}
sum=change(sum);
//无向边
map[i].push_back(sum);
map[sum].push_back(i);
arr[j]=arr
;
arr
=temp;
}
}

int T=5;
while(T--)
{
int x,y;
scanf("%d%d",&x,&y);
x=change(x);
y=change(y);
SPFA(x);
printf("%d\n",dis[y]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: