标签:des style blog http color java os io
4 2 1 2 0 -2 -2 0 3 -1 -1 0 0 1 1 1 0 1 1 1 0 3 1 1 1 0 -1 -1 -1 0 -1 -1 -1 0 3 -1 -1 -1 0 -2 -2 -2 0 -2 -2 -2 0
2 1 1 0
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> using namespace std; typedef struct node { int a[20],ans,x,top;//a数组里面放参加party的朋友的编号 }; int ma[20][20]; int fri[20],Max,n; void bfs() { node t,v; queue <node> Q; t.top=0;t.ans=0;t.x=-1; Q.push(t); while(!Q.empty()) { v=Q.front();Q.pop(); if(v.x==n-1)//每选完一轮更新一下最大值 { if(Max<v.ans) Max=v.ans; continue; } //两个搜索方向,要么选此人,要么不选 t.x=v.x+1;//不选第i个人 t.ans=v.ans; t.top=v.top; for(int i=0;i<v.top;i++) t.a[i]=v.a[i]; Q.push(t);//选第i个人 t.top=v.top+1; t.a[v.top]=t.x; t.ans=v.ans+fri[t.x]; for(int i=0;i<v.top;i++) t.ans+=ma[t.a[i]][t.x]; Q.push(t); } } int main() { int T,i,j; cin>>T; while(T--) { cin>>n; for(i=0;i<n;i++) cin>>fri[i]; for(i=0;i<n;i++) for(j=0;j<n;j++) cin>>ma[i][j]; Max=-1; bfs(); if(Max<0) cout<<"0"<<endl; else cout<<Max<<endl; } return 0; }
SDUT 2860-生日Party--BFS,布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://blog.csdn.net/qq_16255321/article/details/38559515