例题7-6 带宽 UVa140
刚开始已知看不懂题。。醉了
题目中的相邻结点是针对图说的
得到的排列不一定是按照八个结点画一条线依次经过的那种
#include <bits/stdc++.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
/*
A:FB;B:GC;D:GC;F:AGH;E:HD
*/
int V[26],G[26][26];
int n;
int pre_best,best;
int ans[10],point[10],bestans[10];
char letters[27]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
bool init(){
best=8;
char *s;
scanf("%s",s);
//cout<<s<<endl;
for(int i=0;i<strlen(s);i++)
cout<<s[i];
cout<<endl;
if(s[0]=='#'){
//cout<<"!"<<endl;
return false;
}
memset(V,0,sizeof(V));
memset(G,0,sizeof(G));
memset(ans,0,sizeof(ans));
memset(bestans,0,sizeof(bestans));
memset(point,0,sizeof(point));
for(int i=0;i<strlen(s);i++){
cout<<endl<<s[i];
if(s[i]>='A'&&s[i]<='Z'){
int m,n;
m=s[i]-'A';
int j;
for(j=i+1;s[j]!=';'&&s[j]!='\n'&&s[j]!='\0';j++){
//cout<<j<<" ";
//if(s[j]=':')
// continue;
if(s[j]>='A'&&s[j]<='Z'){
cout<<" "<<s[j];
n=s[j]-'A';
G[m][n]=1;G[n][m]=1;
}
}
i=j;
}
}
cout<<endl;
for(int i=0;i<26;i++){
for(int j=0;j<26;j++){
if(G[i][j]==1)
V[i]=1;//V[j]=1;
}
}
n=0;
pre_best=0;
for(int i=0;i<26;i++)
if(V[i]){
point[n++]=i;
if(V[i]>pre_best)
pre_best=V[i];
}
for(int i=0;i<26;i++){
for(int j=0;j<26;j++){
cout<<G[i][j]<<" ";
}
cout<<endl;
}
return true;
}
void dfs(int cur,int f_dstc){
if(cur==n&&f_dstc<best){
best=f_dstc;
for(int i=0;i<n;i++){
bestans[i]=ans[i];
}
return;
}
for(int i=0;i<n;i++){
int ok=1;
for(int j=0;j<cur;j++){
if(ans[j]==point[i]){
ok=0;
break;
}
}
if(ok){ //point[i]在前边的序列没有出现过
int dstc;
for(int j=0;j<cur;j++){
if(G[point[i]][ans[j]]){ //前边的序列中有point[i]的相邻结点
dstc=cur-j;
if(dstc>f_dstc)
f_dstc=dstc;
}
}
if(f_dstc<best){
ans[cur]=point[i];
dfs(cur+1,f_dstc);
}
}
}
return;
}
int main(int argc, char *argv[]) {
while(init()){
cout<<endl;
for(int x=0;x<n;x++){
cout<<point[x]<<" ";
}
dfs(0,1);
for(int i=0;i<n;i++){
cout<<letters[bestans[i]]<<' ';
}
cout<<"-> "<<best;
}
return 0;
}
例题7-7 天平难题 UVa1354
例题7-11 宝箱 UVa12325
#include <bits/stdc++.h>
using namespace std;
/*UVa12325*/
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
/*
2
100 1 1 2 2
100 5 3 34 34
*/
long long n,s1,s2,v1,v2,r;
int main(int argc, char *argv[]) {
int T,t=0;
cin>>T;
while(T--){
scanf("%d%d%d%d%d",&n,&s1,&v1,&s2,&v2);
if(s1<s2){
swap(s1,s2);
swap(v1,v2);
}
cout<<n<<s1<<v1<<s2<<v2<<endl;
r=ceil(sqrt(1.0*n));
long long v,value=0;
if(s1>r){
for(long long i=0;i<=n/s1;i++){
v=v1*i+(n-s1*i)/s2*v2;
if(v>value)
value=v;
}
}
else{
if(s2*v1<s1*v2){
swap(s1,s2);
swap(v1,v2);
}
for(long long i=0;i<s2;i++){
v=s2*i+(n-s2*i)/s1*v1;
if(v>value)
value=v;
}
}
cout<<"Case #"<<t++<<": "<<value<<endl;
}
return 0;
}
例题7-12 旋转游戏 UVa1343