UVa 140 - BandWidth (暴力)

F - Bandwidth
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit  Status

Description


 Bandwidth 

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

题意:看图,在数轴上,求一序列使图中连线两点最大距离尽可能小。
分析:数据较小,枚举序列,暴力
AC代码:(提前说,写挫了,有不好的地方不要多看,思路看去就行,存档之用)
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
const int MAXM=100;
const int INF=0x3f3f3f3f;
int en,end1,minans;string ans;
int u[MAXM],v[MAXM],p[10],inq[30],list[13];
int done[13];
int dis1(int a,int b){	return a>b?a-b:b-a;}
void dfs(int place)
{
	int i,j;
	if(place==en+1)
	{
		int mm=0;
		for(i=0;i<end1;i++)
			if(mm<dis1(done[u[i]],done[v[i]]))
			{
				mm=dis1(done[u[i]],done[v[i]]);
			}	

		if(mm<minans)
		{
			minans=mm;
			ans=string();
			for(i=1;i<=en;i++)
			{
				ans+=(char)(list[p[i]]+'A');
				ans+=" ";
			}
		}
		return ;
	}
	
	for(i=0;i<en;i++)
		if(!done[i])
		{
			done[i]=place;p[place]=i;//将done【】数据类型设为int,存该数字的位置
			dfs(place+1);
			done[i]=0;
		}
	return ;
}
int main()
{
	char a[100];char s,t,xx;
	int i,cas=0;
	while(gets(a)!=NULL)
	{
		if(!strcmp(a,"#"))break;
		memset(inq,0,sizeof inq);
		int len=strlen(a);en=0;end1=0;
		for(i=0;i<len;)
		{
			sscanf(a+i,"%c",&s);i+=1;	
			if(!inq[s-'A'])inq[s-'A']=1;
			if(i<len)sscanf(a+i,"%c",&xx);i+=1;
			if(xx==':')
			while(i<len)
			{
				sscanf(a+i,"%c",&t);i++;
				if(t==';')break;
				if(!inq[t-'A'])inq[t-'A']=1;
				u[end1]=s-'A';
				v[end1++]=t-'A';
			}
		}
		for(i=0;i<26;i++)
			if(inq[i])list[en++]=i;
		for(i=0;i<end1;i++)
		{
			int num=en-1;
			while(num>=0)
			{
				if(list[num]==u[i])break;
					num--;
			}
			u[i]=num;
			num=en-1;
			while(num>=0)
			{
				if(list[num]==v[i])break;
					num--;
			}
			v[i]=num;
		}
		memset(done,0,sizeof done);
		minans=INF;dfs(1);
		cout<<ans<<"-> "<<minans<<endl;
	}
	return 0;
}


你可能感兴趣的:(UVa 140 - BandWidth (暴力))