求问这两份代码的区别。
查看原帖
求问这两份代码的区别。
513684
Critics楼主2021/8/6 08:54

使用 1~end1 的代码可以通过,但是 2~end2 的代码会在第 24 个点错。

在我的理解中,这两个代码片段本质是一样的。

求问为什么第二个错了。

//伟大的导师,伟大的领袖,伟大的统帅,伟大的舵手
#include<bits/stdc++.h>
using namespace std;
int read() {
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*w;
}
const int maxn = 310;
int n,c[maxn],l[maxn];
map<int,char>v;map<int,int>d;
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q;
#define D(x) (d.count(x)?d[x]:1e18)
signed main() {
	n=read();
	for(int i=1;i<=n;i++) l[i]=read();
//1
	for(int i=1;i<=n;i++) c[i]=read();
	q.push(make_pair(d[0]=0,0));
//end1
//2
	for(int i=1;i<=n;i++) q.push(make_pair(d[l[i]]=c[i]=read(),l[i]));
//end2
	while(!q.empty()) {
		int x=q.top().second;q.pop();
		if(v[x]) continue;v[x]=1;
		for(int i=1,t;i<=n;i++) if(!v[t=__gcd(x,l[i])]&&d[x]+c[i]<D(t)) q.push(make_pair(d[t]=d[x]+c[i],t)); 
	}
	cout<<(d.count(1)?d[1]:-1)<<'\n';
	return 0;
}
2021/8/6 08:54
加载中...