20分求调
查看原帖
20分求调
1348824
yzq120806楼主2024/6/10 10:31

用暴力回溯做的,按理讲只会TLE,可是基本上都是WA,有一些TLE

#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
int v[2700];
list<int> l[2700];
bool book[2700];
int n,m,k;
long long score;
struct node{
	int num;
	int value;
};
node make_node(int v,int w){
	node rn;
	rn.num=v;
	rn.value=w;
	return rn;
}
bool l_find(list<int> & nl,int & x){
	for(auto i:nl){
		if(i==x)return true;
	}
	return false;
}
inline int to_nodes(int a,int b){
//	cout<<"in function now:\na="<<a<<" b="<<b<<endl;
	if(a==b){
//		cout<<"a==b!\n\n";
		return 0;
	}
	int r=inf;
	int is[2700]={inf};
	memset(is,inf,sizeof(is));
	queue<node> q;
	q.push(make_node(a,-1));
	is[a]=0;
	while(!q.empty()){
//		cout<<"queue head:"<<q.front().num<<" queue head num:"<<q.front().value<<endl;
		if(q.front().num==b){
//			cout<<"Yes!to b!\nlast r:"<<r;
			r=min(r,q.front().value);
//			cout<<" new r:"<<r<<endl;
			q.pop();
			continue;
		}
//		cout<<"go for!\n";
		for(auto i:l[q.front().num]){
//			cout<<"i="<<i<<endl;
			if(i==a){
//				cout<<"continue!\n";
				continue;
			}
			if(q.front().value+1<is[i]){
//				cout<<"Yes!Is it!\n";
				q.push(make_node(i,q.front().value+1));
//				cout<<"i="<<i<<" queue head num="<<q.front().value<<" num="<<q.front().value+1<<endl;
				is[i]=true;
			}
		}
		q.pop();
	}
//	cout<<"function over!\n";
	if(r==inf){
//		cout<<"return inf\n\n";
		return inf;
	}else{
//		cout<<"return "<<r<<"\n\n";
		return r;
	}
}
int zong=0;
//inline void bfs(){
//	if(i>4){
//		score=max(score,(long long)s);
//		return;
//	}
//	for(int j=2;j<=n;j++){
//		int to=max(0,to_nodes(from,j));
//		if(book[j]==false&&zong+to<=k){
//			book[j]=true;
//			zong+=to;
//			dfs(j,i+1,s+v[j]);
//			book[j]=false;
//			zong-=to;
//		}
//	}
//    for(auto it:l[from]){
//    	int to=max(0,to_nodes(from,it));
//    	if(book[it]==false&&zong+to<=k){
//    		book[it]=true;
//    		zong+=to;
//    		dfs(it,i+1,s+v[it]);
//    		book[it]=false;
//    		zong-=to;
//		}
//	}
//	queue<node> q;
//	int ma[2700];
//	memset(ma,0,sizeof(ma));
//	q.push(make_node(1,0));
//	while(!q.empty()){
//		int t=q.front().value;
//		int x=q.front().num;
////		cout<<"num="<<x<<" value="<<t<<endl;
//		if(t>=4){
////			cout<<"t>=4! old score:"<<score;
//			score=max(score,(long long)ma[x]);
////			cout<<"  new score:"<<score<<endl;
//		}
////		cout<<"go for!";
//		for(auto it:l[x]){
////			cout<<"it="<<it<<endl;
//			if(book[it]==false){
////				cout<<"OK!\n";
//				book[it]=true;
////				cout<<"old ma[it]="<<ma[it];
//				ma[it]=max(ma[x],v[it]+ma[x]);
////				cout<<"  new ma[it]="<<ma[it]<<endl;
//				q.push(make_node(it,t+1));
//			}
//		}
//		q.pop();
////		cout<<"while done!\n\n";
//	}
//}
set<int> s;
inline void bfs(){
	queue<node> q;
	bool ma[2700];
	memset(ma,false,sizeof(ma));
	q.push(make_node(1,0));
	while(!q.empty()&&q.front().value<=4+k){
		int t=q.front().value;
		int x=q.front().num;
//		cout<<"num="<<x<<" value="<<t<<endl;
//		cout<<"go for!";
		for(auto it:l[x]){
//			cout<<"it="<<it<<endl;
			if(ma[it]==false){
//				cout<<"OK!\n";
				ma[it]=true;
				s.insert(it);
//				cout<<"old ma[it]="<<ma[it];
//				cout<<"  new ma[it]="<<ma[it]<<endl;
				q.push(make_node(it,t+1));
			}
		}
		q.pop();
//		cout<<"while done!\n\n";
	}
}
inline void dfs(int from,int w,int bu,int sc){
	if(w>4){
		score=max(score,(long long)sc);
		return ;
	}
	for(auto i:s){
		int t=to_nodes(from,i);
		if(book[i]==false&&bu+t<=k){
			book[i]=true;
			dfs(i,w+1,bu+t,sc+v[i]);
			book[i]=false;
		}
	}
}
int i,j;
int main(){
	cin>>n>>m>>k;
	for(i=2;i<=n;i++)
		cin>>v[i];
	for(i=1;i<=m;i++){
		int from,to;
		cin>>from>>to;
		l[from].push_back(to);
		l[to].push_back(from);
	}
	book[1]=true;
//	queue<node> q;
//	q.push(make_node(1,-1));
//	while(!q.empty()){
//		for(auto i:l[q.front().num]){
//
//		}
//	}
	bfs();
	dfs(1,1,0,0);
	cout<<score;
	return 0;
}

注释部分不用理会

2024/6/10 10:31
加载中...