uva 140 宽带
2015-09-07 22:30
309 查看
给你一个以邻接表展现的图的节点以及其相邻的节点,这时你将所有出现过的节点随机排列,
找出此排序中相邻节点之间距离最长的值代表此排列的值,而这只是其中一种排列,
你要找出所有排列中这种代表的值得最小值,并将有最小值的串的排列情况输出
// Created by Chenhongwei in 2015.
// Copyright (c) 2015 Chenhongwei. All rights reserved.
#include"iostream"
#include"cstdio"
#include"cstdlib"
#include"cstring"
#include"climits"
#include"queue"
#include"cmath"
#include"map"
#include"set"
#include"stack"
#include"vector"
#include"sstream"
#include"algorithm"
using namespace std;
typedef long long ll;
vector<int>g[30];
set<int >node;
int ans[20],a[20];
int main()
{
//ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
string s;
while(getline(cin,s)&&s[0]!='#')
{
for(int i=0;i<=26;i++)
g[i].clear();
node.clear();
int p=0;
while(p<s.size())
{
int x=s[p]-'A';
node.insert(x);
p+=2;
while(s[p]!=';'&&p<s.size())
{
int y=s[p]-'A';
node.insert(y);
g[x].push_back(y);
g[y].push_back(x);
p++;
}
p++;
}
p=0;
set<int>::iterator it=node.begin();
for(;it!=node.end();it++)
{
a[p]=*it;
p++;
}
int answer=INT_MAX/2;
do
{
int b[30];
memset(b,0,sizeof b);
for(int i=0;i<p;i++)
{
for(int j=0;j<g[a[i]].size();j++)
{
for(int k=0;k<p;k++)
if(g[a[i]][j]==a[k])
{
b[a[i]]=max(b[a[i]],abs(k-i));
b[a[k]]=max(b[a[k]],abs(k-i));
break;
}
}
}
int maxv=0;
for(int i=0;i<p;i++)
maxv=max(maxv,b[a[i]]);
if(maxv<answer)
{
answer=maxv;
for(int i=0;i<p;i++)
ans[i]=a[i];
}
}while(next_permutation(a,a+p));
for(int i=0;i<p;i++)
printf("%c ",ans[i]+'A');
printf("-> %d\n",answer);
}
return 0;
}
找出此排序中相邻节点之间距离最长的值代表此排列的值,而这只是其中一种排列,
你要找出所有排列中这种代表的值得最小值,并将有最小值的串的排列情况输出
// Created by Chenhongwei in 2015.
// Copyright (c) 2015 Chenhongwei. All rights reserved.
#include"iostream"
#include"cstdio"
#include"cstdlib"
#include"cstring"
#include"climits"
#include"queue"
#include"cmath"
#include"map"
#include"set"
#include"stack"
#include"vector"
#include"sstream"
#include"algorithm"
using namespace std;
typedef long long ll;
vector<int>g[30];
set<int >node;
int ans[20],a[20];
int main()
{
//ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
string s;
while(getline(cin,s)&&s[0]!='#')
{
for(int i=0;i<=26;i++)
g[i].clear();
node.clear();
int p=0;
while(p<s.size())
{
int x=s[p]-'A';
node.insert(x);
p+=2;
while(s[p]!=';'&&p<s.size())
{
int y=s[p]-'A';
node.insert(y);
g[x].push_back(y);
g[y].push_back(x);
p++;
}
p++;
}
p=0;
set<int>::iterator it=node.begin();
for(;it!=node.end();it++)
{
a[p]=*it;
p++;
}
int answer=INT_MAX/2;
do
{
int b[30];
memset(b,0,sizeof b);
for(int i=0;i<p;i++)
{
for(int j=0;j<g[a[i]].size();j++)
{
for(int k=0;k<p;k++)
if(g[a[i]][j]==a[k])
{
b[a[i]]=max(b[a[i]],abs(k-i));
b[a[k]]=max(b[a[k]],abs(k-i));
break;
}
}
}
int maxv=0;
for(int i=0;i<p;i++)
maxv=max(maxv,b[a[i]]);
if(maxv<answer)
{
answer=maxv;
for(int i=0;i<p;i++)
ans[i]=a[i];
}
}while(next_permutation(a,a+p));
for(int i=0;i<p;i++)
printf("%c ",ans[i]+'A');
printf("-> %d\n",answer);
}
return 0;
}
相关文章推荐
- Centos7 firewall常用命令总结
- 【JavaSE】day15_TCP之聊天室
- HDOJ 1864 最大报销额
- next数组介绍
- ARM MMU页表框架
- 实现主成分分析和白化
- Linux下磁盘常见错误分析与检测工具使用
- hdu5419 Victor and Toys
- Java Web学习(一)
- 《资源革命:如何抓住一百年来最大的商机》:资源紧缺是一个巨大的商机,有许多创新的解决办法。三星推荐
- Linux内存管理之mmap详解
- jdk 环境变量配置
- 1072 Nightmare
- LeetCode Sum Root to Leaf Numbers
- 腾讯16算法 杂谈
- Android五大存储---内部存储(缓存)
- JavaScript之prototype
- LeetCode Sum Root to Leaf Numbers
- 学习 《一步步搭建自己的博客》 第一版 之异常 边理解边记录
- Objective-C( Category 分类,非正式协议,分类延展)