Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an ordering on the elements in V, then the bandwidth of a node v is defined as the maximum distance in the ordering between v and any node to which it is connected in the graph. The bandwidth of the ordering is then defined as the maximum of the individual bandwidths. For example, consider the following graph:
This can be ordered in many ways, two of which are illustrated below:
For these orderings, the bandwidths of the nodes (in order) are 6, 6, 1, 4, 1, 1, 6, 6 giving an ordering bandwidth of 6, and 5, 3, 1, 4, 3, 5, 1, 4 giving an ordering bandwidth of 5.
Write a program that will find the ordering of a graph that minimises the bandwidth.
Input
Input will consist of a series of graphs. Each graph will appear on a line by itself. The entire file will be terminated by a line consisting of a single #. For each graph, the input will consist of a series of records separated by `;'. Each record will consist of a node name (a single upper case character in the the range `A' to `Z'), followed by a `:' and at least one of its neighbours. The graph will contain no more than 8 nodes.
Output
Output will consist of one line for each graph, listing the ordering of the nodes followed by an arrow (->) and the bandwidth for that ordering. All items must be separated from their neighbours by exactly one space. If more than one ordering produces the same bandwidth, then choose the smallest in lexicographic ordering, that is the one that would appear first in an alphabetic listing.
Sample input
A:FB;B:GC;D:GC;F:AGH;E:HD #
Sample output
A B C F G D H E -> 3
这题其实不难 一直TLE 以为字母都是按顺序来的 后来发现可能不按顺序 出现过的才有 各种剪枝优化以后跑了15ms 用的数组有点多比较麻烦 还可以更优化的
#include <bits/stdc++.h>
//A:FB;B:GC;D:GC;F:AGH;E:HD
using namespace std;
vector<int > vel[10];
string s;
int minn,cur[10],have[27],ans[10],temp[10],num,g[27];//minn:已找到的最小值 have:是否用过 temp:当前序列 ans:已找到的最优序列 num:出现字母总个数 g:是否出现过 cur:当前序列每个位置的带宽
int bandwidth(int n,int len) {
int wide=0,sum=0;
for(int i=1;i<len;i++)
for(int j=0;j<vel[n].size();j++)
if(temp[i]==vel[n][j]&&(len-i)>wide)
wide=len-i,sum++;
else if(temp[i]==vel[n][j]&&(len-i)<=wide)
sum++;
if(vel[n].size()-sum>minn)
return -1; //如果剩余节点个数大于当前最小值,剪枝
return wide;
}
void solve(int len) {
if(len==num) {
int sum=0;
for(int i=1;i<=num;i++)
if(cur[i]>sum)
sum=cur[i];
if(sum<minn) {
minn=sum;
memcpy(ans,temp,sizeof(temp));
}
}
else {
for(int i=1;i<=26;i++) {
if(!g[i])
continue;
if(!have[i]) {
if(!vel[i].empty()) {
int o=bandwidth(i,len);
if(o>=minn||o==-1)
continue;
cur[len]=o;
}
temp[len]=i;
have[i]=1;
solve(len+1);
have[i]=0;
}
}
}
}
int main() {
// freopen("in.txt","r",stdin);
// freopen("mine.txt","w",stdout);
while(cin>>s&&s!="#") {
for(int i=0;i<10;i++)
vel[i].clear();
memset(have,0,sizeof(have));
memset(cur,0,sizeof(cur));
memset(g,0,sizeof(g));
int flag=0,pos=0;
num=0;
minn=100;
for(int i=0;i<s.length();i++) {
if(s[i]==':')
pos=s[i-1]-64,flag=1;
else if(isalpha(s[i])) {
if(!g[s[i]-64])
num++;
g[s[i]-64]=1;
if(flag) {
vel[pos].push_back(s[i]-64);
vel[s[i]-64].push_back(pos);
}
}
else if(s[i]==';')
flag=0;
}
num++;
solve(1);
for(int i=1;i<num;i++)
printf("%c ",ans[i]+64);
printf("-> %d\n",minn);
}
return 0;
}