Find a Way


这道题,是一个典型最短路径问题,用宽搜,但不要理解为双起点,而要理解为双宽搜。这道题有多个终点,我们不要一个一个点地枚举,这样做会超时(我试过的,而且还卡了很久不知道为什么错.而是算没个人到每个KFC的距离,再用两人的距离和比较,取最小的。

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
long long n,b[40001][3]={0},s=0,d[201][201]={0},d1[201][201],m,direction[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
char a[201][201];
queue<int>p;
queue<int>p1;
void search(int i2,int j2){
memset(d,0x3f3f3f3f,sizeof(d));
	d[i2][j2]=0;
	while(p.size()){
		p.pop();p1.pop();
	}
	p.push(i2);p1.push(j2);
	while(p.size()){
	    int sx=p.front();p.pop();
		int sy=p1.front();  p1.pop();
	        for(int i=0;i<4;i++){
	        	int x1=sx+direction[i][0],x2=sy+direction[i][1];
	        	if(x1>=1&&x1<=n&&x2>=1&&x2<=m&&d[x1][x2]>d[sx][sy]+1&&a[x1][x2]!='#'){//没超出边界且没遇到墙才行(不要重复走)
	        		p.push(x1);p1.push(x2);
	        		d[x1][x2]=d[sx][sy]+1;
				} 
			}		  
	}
}
void search1(int i2,int j2){
memset(d1,0x3f3f3f3f,sizeof(d1));
	d1[i2][j2]=0;
	while(p.size()){
		p.pop();p1.pop();
	}
	p.push(i2);p1.push(j2);
	while(p.size()){
	    int sx=p.front();p.pop();
		int sy=p1.front();  p1.pop();
	        for(int i=0;i<4;i++){
	        	int x1=sx+direction[i][0],x2=sy+direction[i][1];
	        	if(x1>=1&&x1<=n&&x2>=1&&x2<=m&&d1[x1][x2]>d1[sx][sy]+1&&a[x1][x2]!='#'){
	        		p.push(x1);p1.push(x2);
	        		d1[x1][x2]=d1[sx][sy]+1;
				} 
			}		
		}  
	}

int main(){
	while(cin>>n>>m){
		long long s=0,stx1,sty1,stx2,sty2,ans=100000000;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				cin>>a[i][j];
				if(a[i][j]=='Y'){
					stx1=i;sty1=j;
				}
				else if(a[i][j]=='M'){
					stx2=i;sty2=j;
				}
				else if(a[i][j]=='@'){
					b[++s][1]=i;b[s][2]=j;//存终点
				}
			}
		}
search(stx1,sty1);search1(stx2,sty2);
for(int i=1;i<=s;i++){
	long long time=d[b[i][1]][b[i][2]]+d1[b[i][1]][b[i][2]];//距离和
	ans=min(time,ans);
}
cout<<ans*11<<endl;//记得乘以一个11哦!
} 
}


You and your team have worked tirelessly until you have a sequence a1,a2,…,a2n+1 of positive integers satisfying these properties. 1≤ai≤1018 for all 1≤i≤2n+1 . a1,a2,…,a2n+1 are pairwise distinct. a1=a2−a3+a4−a5+…+a2n−a2n+1 . However, the people you worked with sabotaged you because they wanted to publish this sequence first. They deleted one number from this sequence and shuffled the rest, leaving you with a sequence b1,b2,…,b2n . You have forgotten the sequence a and want to find a way to recover it. If there are many possible sequences, you can output any of them. It can be proven under the constraints of the problem that at least one sequence a exists.    Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104 ). The description of the test cases follows. The first line of each test case contains one integer n (1≤n≤2⋅105 ). The second line of each test case contains 2n distinct integers b1,b2,…,b2n (1≤bi≤109 ), denoting the sequence b . It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 .    Output For each test case, output 2n+1 distinct integers, denoting the sequence a (1≤ai≤1018 ). If there are multiple possible sequences, you can output any of them. The sequence a should satisfy the given conditions, and it should be possible to obtain b after deleting one element from a and shuffling the remaining elements. Example InputCopy 4 1 9 2 2 8 6 1 4 3 99 2 86 33 14 77 2 1 6 3 2 OutputCopy 7 9 2 1 8 4 6 9 86 99 2 77 69 14 33 4 6 1 2 3
最新发布
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值