题意:给出一个n个结点的图G和一个结点的排列,定义结点的带宽为i和相邻结点在排列中的最远距离,求出让带宽最小的结点排列。
思路:用STL的next_permutation来做确实是很方便,适当剪枝一下就可以了,不过我不明白的是为什么我用string字符串会超时...
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std; int a[], c[];
int ans[];
int map[][];
char str[]; int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int l, k, maxn,n;
while (gets(str) )
{
if (str[] == '#') break;
memset(map, , sizeof(map));
memset(a, , sizeof(a));
memset(ans, , sizeof(ans));
memset(c, , sizeof(c));
l = strlen(str); for (int i = ; i < l; i++)
{
if (str[i] == ':')
{
int t = str[i - ] - 'A';
a[t] = ;
for (k = i + ; str[k] != ';' && k < l; k++)
{
int p = str[k] - 'A';
a[p] = ;
map[t][p] = map[p][t] = ;
}
i = k;
}
} n = ;
for (int i = ; i < ; i++)
{
if (a[i])
c[n++] = i;
}
maxn = ;
do
{
int sum = ;
int ok = ;
for (int i = ; i < n; i++)
{
for (int j = i + ; j < n; j++)
{
if (map[c[i]][c[j]])
if (j - i>sum)
sum = j - i;
if (sum>=maxn) { ok = ; break; }
}
if (!ok) break;
} if (sum < maxn)
{
maxn = sum;
memcpy(ans, c, sizeof(c));
}
} while (next_permutation(c, c + n));
for (int i = ; i < n; i++)
{
char s = 'A' + ans[i];
cout << s << " ";
}
cout << "-> " << maxn << endl;
}
return ;
}