http://118.190.20.162/view.page?gpid=T7
递归bfs超时版 0分
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node{
ll x;
ll y;
bool add;
bool chose;
bool operator ==(const node bb) const {
return ((this->x==bb.x)&&(this->y==bb.y));
}
};
node a[220];
ll minn=(1<<30)-1;
ll n,m,k,r;
bool in(node aa,node bb){
if( (aa.x-bb.x)*(aa.x-bb.x)+(aa.y-bb.y)*(aa.y-bb.y) <= r*r){
return true;
}
else return false;
}
void bfs(node &aa,ll &minn,ll level,ll &prek){
int next=0;//从当前可以到多少个未访问的点
aa.chose=true;
if(prek==k){
for(int i=0;i<m+n;i++){
if(a[i].add==true)
a[i].chose=true;
}
}
if(in(aa,a[1])){//如果到达终点就返回
if(level<minn){
minn=level;
}
return;
}
for(int i=0;i<m+n;i++){//遍历所有点,寻找可达的未访问
if(( aa == a[i] )|| a[i].chose==true)
continue;
if(in(aa,a[i])){
next++;
if(a[i].add){
prek++;
}
a[i].chose=true;
//cout<<"level:"<<level<<" "<<a[i].x<<" "<<a[i].y<<endl;
bfs(a[i],minn,level+1,prek);
a[i].chose=false;
}
}
aa.chose=false;
//如果无路可选也返回
}
int main(){
scanf("%lld%lld%lld%lld",&n,&m,&k,&r);
for(int i=0;i<n;i++){
scanf("%lld%lld",&a[i].x,&a[i].y);
a[i].add=false;
a[i].chose=false;
}
for(int i=0;i<m;i++){
scanf("%lld%lld",&a[i+n].x,&a[i+n].y);
a[i+n].add=true;
a[i+n].chose=false;
}
ll prek=0;
bfs(a[0],minn,0,prek);
printf("%lld",minn);
return 0;
}
栈bfs 100分
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node{
ll x;
ll y;
bool add;
bool chose;
int prek;
int level;
bool operator ==(const node bb) const {
return ((this->x==bb.x)&&(this->y==bb.y));
}
};
node a[220];
ll minn=(1<<30)-1;
ll n,m,k,r;
bool in(node aa,node bb){
if( (aa.x-bb.x)*(aa.x-bb.x)+(aa.y-bb.y)*(aa.y-bb.y) <= r*r){
return true;
}
else return false;
}
int bfs(){
queue<node> q;
a[0].prek=0;
a[0].level=0;
a[0].chose=true;
q.push(a[0]);
node pre;
while(!q.empty()){
pre=q.front();
q.pop();
if(pre.x==a[1].x&&pre.y==a[1].y)
return pre.level;
int searchn=n+m;
if(pre.prek==k){
searchn=n;
}
for(int i=0;i<searchn;i++){
if(a[i].chose==true)
continue;
if(in(pre,a[i])){
a[i].chose=true;
a[i].level=pre.level+1;
a[i].prek=pre.prek;
if(a[i].add==true)
a[i].prek++;
q.push(a[i]);
}
}
}
return 0;
}
int main(){
scanf("%lld%lld%lld%lld",&n,&m,&k,&r);
for(int i=0;i<n;i++){
scanf("%lld%lld",&a[i].x,&a[i].y);
a[i].add=false;
a[i].chose=false;
}
for(int i=0;i<m;i++){
scanf("%lld%lld",&a[i+n].x,&a[i+n].y);
a[i+n].add=true;
a[i+n].chose=false;
}
int ans=bfs();
printf("%d",ans-1);
return 0;
}
SPFA 100分
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node{
ll x;
ll y;
bool add;
bool chose;
int prek;
int seq;
bool operator ==(const node bb) const {
return ((this->x==bb.x)&&(this->y==bb.y));
}
};
node a[220];
ll minn=(1<<30)-1;
ll n,m,k,r;
bool in(node aa,node bb){
if( (aa.x-bb.x)*(aa.x-bb.x)+(aa.y-bb.y)*(aa.y-bb.y) <= r*r){
return true;
}
else return false;
}
ll d[220]={(1<<30)-1};
void spfa(){
queue <node> q;
a[0].chose=true;
a[0].prek=0;
q.push(a[0]);
d[0]=0;
node pre;
while (!q.empty()){
pre=q.front();
q.pop();
a[pre.seq].chose=false;
int searchn=n+m;
if(pre.prek==k){
searchn=n;
}
for(int i=0;i<searchn;i++){
if(a[i].chose==true)
continue;
if(in(pre,a[i])){
if(d[pre.seq]+1<d[a[i].seq]){
d[a[i].seq]=d[pre.seq]+1;
if(a[i].add){
a[i].prek=pre.prek+1;
}
a[i].chose=true;
q.push(a[i]);
}
}
}
}
}
int main(){
scanf("%lld%lld%lld%lld",&n,&m,&k,&r);
for(int i=0;i<n;i++){
scanf("%lld%lld",&a[i].x,&a[i].y);
a[i].add=false;
a[i].chose=false;
a[i].seq=i;
}
for(int i=0;i<m;i++){
scanf("%lld%lld",&a[i+n].x,&a[i+n].y);
a[i+n].add=true;
a[i+n].chose=false;
a[i+n].seq=i+n;
}
for(int i=0;i<n+m;i++)
d[i]=(1<<30);
spfa();
printf("%d",d[1]-1);
return 0;
}