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}Sn,
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}x1 x2 ... xn
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}S4
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}x1 x2x3 x4 ... xn,
it has n−1n-1n−1
edges connecting to nodes x2 x1 x3 x4 ... xnx_{2}\ x_{1}\ x_{3}\ x_{4}\ ...\ x_{n}x2 x1 x3 x4 ... x<
1594f
span style="font-size:0em;">n,
x3 x2 x1 x4 ... xnx_{3}\ x_{2}\ x_{1}\ x_{4}\ ...\ x_{n}x3 x2 x1 x4 ... xn,
x4 x2 x3 x1 ... xnx_{4}\ x_{2}\ x_{3}\ x_{1}\ ...\ x_{n}x4 x2 x3 x1 ... xn,
..., and xn x2 x3 x4 ... x1x_{n}\ x_{2}\ x_{3}\ x_{4}\ ...\ x_{1}xn x2 x3 x4 ... x1.
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}x1 x2 x3 x4 ... xn,
for d=2,...,nd = 2, ..., nd=2,...,n.
For instance, in S4S_{4}S4,
node 123412341234
has 333
edges connecting to nodes 213421342134,
321432143214,
and 423142314231.
The following figure shows how S4S_{4}S4
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).
![](https://res.jisuanke.com/img/upload/20170920/b9510b5515aa0293915340ea604adac52bc73f30.png)
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}x1
x2x_{2}x2
x3x_{3}x3
... xnx_{n}xn
and y1y_{1}y1
y2y_{2}y2
y3 ... yny_{3}\ ...\ y_{n}y3 ... yn
in SnS_{n}Sn.
You have to calculate the distance between these two nodes (which is an integer).
(dimension of the star graph)
A list of 555
pairs of nodes.
values, each representing the distance of a pair of nodes.
//题意:给定一个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;
}
Given an integer nnn,
an n−dimensionaln-dimensionaln−dimensional
star graph, also referred to as SnS_{n}Sn,
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}x1 x2 ... xn
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}S4
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}x1 x2x3 x4 ... xn,
it has n−1n-1n−1
edges connecting to nodes x2 x1 x3 x4 ... xnx_{2}\ x_{1}\ x_{3}\ x_{4}\ ...\ x_{n}x2 x1 x3 x4 ... x<
1594f
span style="font-size:0em;">n,
x3 x2 x1 x4 ... xnx_{3}\ x_{2}\ x_{1}\ x_{4}\ ...\ x_{n}x3 x2 x1 x4 ... xn,
x4 x2 x3 x1 ... xnx_{4}\ x_{2}\ x_{3}\ x_{1}\ ...\ x_{n}x4 x2 x3 x1 ... xn,
..., and xn x2 x3 x4 ... x1x_{n}\ x_{2}\ x_{3}\ x_{4}\ ...\ x_{1}xn x2 x3 x4 ... x1.
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}x1 x2 x3 x4 ... xn,
for d=2,...,nd = 2, ..., nd=2,...,n.
For instance, in S4S_{4}S4,
node 123412341234
has 333
edges connecting to nodes 213421342134,
321432143214,
and 423142314231.
The following figure shows how S4S_{4}S4
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).
![](https://res.jisuanke.com/img/upload/20170920/b9510b5515aa0293915340ea604adac52bc73f30.png)
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}x1
x2x_{2}x2
x3x_{3}x3
... xnx_{n}xn
and y1y_{1}y1
y2y_{2}y2
y3 ... yny_{3}\ ...\ y_{n}y3 ... yn
in SnS_{n}Sn.
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 555values, 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;
}
相关文章推荐
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J.Minimum Distance in a Star Graph
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Minimum Distance in a Star Graph
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛(J.Minimum Distance in a Star Graph)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛- J. Minimum Distance in a Star Graph
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J. Minimum Distance in a Star Graph
- 2017ICPC南宁赛区网络赛 Minimum Distance in a Star Graph (bfs)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J Minimum Distance in a Star Graph 广度优先搜索
- 2017 ACM-ICPC南宁网络赛: J. Minimum Distance in a Star Graph(BFS)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J. Minimum Distance in a Star Graph(bfs+状态保存)
- 2017 icpc 南宁赛区 J. Minimum Distance in a Star Graph(bfs)
- Minimum Distance in a Star Graph BFS/康托展开 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- 计蒜客 Minimum Distance in a Star Graph 2017icpc南宁赛区 字符串bfs
- 2017南宁网络赛J.Minimum Distance in a Star Graph(bfs求最短路)
- Minimum Distance in a Star Graph
- ???Minimum Distance in a Star Graph
- 计蒜客 Minimum Distance in a Star Graph 思维水题
- HDU 4725-J - The Shortest Path in Nya Graph-增点建图-层次网络-最短路
- hdu-4725-The Shortest Path in Nya Graph-层次网络
- hdu-4725-The Shortest Path in Nya Graph-层次网络
- 153. Find Minimum in Rotated Sorted Array