[暴力搜索]Bandwidth UVA140
来源:互联网 发布:手机频谱分析软件 编辑:程序博客网 时间:2024/06/05 18:18
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
#include<iostream>#include<algorithm>#include<cstring>using namespace std;int grid[30][30];int appear[30];int width(const string &str){ int maxlen=0; int i,j,k; for(i=0;i<str.size();i++) { for(j=0;j<str.size();j++) { if(grid[str[i]-'A'+1][str[j]-'A'+1]) { int len=j-i; if(len<0) len=len*-1; if(len>maxlen) maxlen=len; } } } return maxlen;}int main(){ string str; while(getline(cin,str)&&str!="#") { memset(grid,0,sizeof(grid)); memset(appear,0,sizeof(appear)); int i=0,j,k; while(i<str.size()) { k=str[i]-'A'+1; appear[k]=1; i=i+2; while(str[i]!=';'&&i!=str.size()) { j=str[i]-'A'+1; appear[j]=1; grid[k][j]=grid[j][k]=1; i++; } if(str[i]==';') i++; } string st=""; for(i=1;i<=26;i++) { if(appear[i]) { char ch='A'+i-1; st=st+ch; } } string st1; sort(st.begin(),st.end()); //cout<<st<<endl; st1=st; int minwidth=width(st); while(next_permutation(st.begin(),st.end())) { int lenth=width(st); if(lenth<minwidth) { st1=st; minwidth=lenth; } } for(i=0;i<st1.size();i++) cout<<st1[i]<<" "; cout<<"-> "<<minwidth<<endl; //cout<<st1<<endl<<minwidth<<endl; } return 0;}
- [暴力搜索]Bandwidth UVA140
- UVA140 - Bandwidth (暴力dfs+排列+剪枝)
- UVA140 ——bandwidth(搜索)
- UVa140 Bandwidth
- uva140 - Bandwidth
- UVa140 - Bandwidth
- UVA140 - Bandwidth
- uva140-Bandwidth
- uva140 - Bandwidth
- UVa140- Bandwidth
- uva140 Bandwidth(排列树+剪枝)
- UVa140 - Bandwidth DFS最优性剪枝题
- 例题7-6 UVa140 Bandwidth(枚举+剪枝)
- Uva140 Bandwidth 【dfs回溯+剪枝】【例题7-6】
- UVa140
- UVA140
- uva140
- UVA140
- java学习笔记——开篇
- SPI总线之CPOL CPHA(转)
- 寻找直方图中面积最大的矩形(C语言版)
- Android 动画框架详解,第 2 部分
- Linux--Socket Buffer--Netowrk Devices--Network Drivers
- [暴力搜索]Bandwidth UVA140
- play framework学习(数据库建表)
- indexOf(String.indexOf 方法)
- MySQL性能优化的最佳20+条经验
- netlink sample
- C# 配置文件读取与修改
- qemu kvm libvirt virsh之间的关系!
- VP6806高清视频处理模块
- play framework学习(html中用java,获取和拼凑url,css样式引用)