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

2017ACM-ICPC南宁网络赛Frequent Subsets Problem(康托展开+bfs)

2017-09-26 09:50 731 查看
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​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


题目来源

2017 ACM-ICPC 亚洲区(南宁赛区)网络赛

题意:给定两个序列,只能用第一个和后面的交换,能把第一个序列换成第二个序列,求最小的交换次数.

我们队没有看出来只能第一个和最后一个交换,然后就感觉这题没法做,赛后他们说只能第一个和后面的交换,*********这*****不就是康托展开+bfs么

不会康托展开请戳这里







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

using namespace std;

struct node
{
int ct;
char s[99];
int step;
};

char s1[99], s2[99];
int a[99];
int v[363880];

int f(int n)
{
if(n == 0)
return 1;
int i = 1;
int x = 1;
for(i = 1;i <= n;i++)
x *= i;
return x;
}

int cantor(char s[], int n)
{
int i, j, sum;
for(i = 0;i < n;i++)
{
sum = 0;
for(j = i + 1;j < n;j++)
{
if(s[j] < s[i])
sum++;
}
a[n-i-1] = sum;
}
int nx = 0;
for(i = 0;i < n;i++)
{
nx = nx + a[i] * f(i);
}
return nx;
}

void bfs(int n, int nx)
{
memset(v,0,sizeof(v));
node a, b;
queue<node>q;
strcpy(a.s,s1);
a.ct = cantor(a.s,n);
a.step = 0;
v[a.ct] = 1;
q.push(a);
while(!q.empty())
{
a = q.front();
q.pop();
if(a.ct == nx)
{
cout<<a.step<<endl;
return;
}
b.step = a.step + 1;
for(int i = 1;i < n;i++)
{
strcpy(b.s,a.s);
swap(b.s[0],b.s[i]);
b.ct = cantor(b.s,n);
if(!v[b.ct])
{
q.push(b);
v[b.ct] = 1;
}
}
}
}

int main()
{
int n, sum;
int nx;
cin>>n;
for(int k = 0;k < 5;k++)
{
cin>>s1>>s2;
nx = cantor(s2,n);
bfs(n,nx);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐