2023百度之星初赛1

很玄学,只会奇数题()

1.

//宽搜 最短路 枚举中间点
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=100010;
vector<int>e[maxn];
queue<int>q;
long long d1[maxn];
long long d2[maxn];
long long d3[maxn];
void bfs1(int x)
{
	memset(d1,-1,sizeof(d1));
    d1[x]=0;
	q.push(x);
	while(!q.empty())
	{
		int z=q.front();
		q.pop();
		for(int i=0;i<e[z].size();i++)
		{
			if(d1[e[z][i]]==-1)
			{
				d1[e[z][i]]=d1[z]+1;
				q.push(e[z][i]);
			}
		}
	}
}
void bfs2(int x)
{
	memset(d2,-1,sizeof(d2));
    d2[x]=0;
	q.push(x);
	while(!q.empty())
	{
		int z=q.front();
		q.pop();
		for(int i=0;i<e[z].size();i++)
		{
			if(d2[e[z][i]]==-1)
			{
				d2[e[z][i]]=d2[z]+1;
				q.push(e[z][i]);
			}
		}
	}
}
void bfs3(int x)
{
	memset(d3,-1,sizeof(d3));
    d3[x]=0;
	q.push(x);
	while(!q.empty())
	{
		int z=q.front();
		q.pop();
		for(int i=0;i<e[z].size();i++)
		{
			if(d3[e[z][i]]==-1)
			{
				d3[e[z][i]]=d3[z]+1;
				q.push(e[z][i]);
			}
		}
	}
}
int main()
{
	int a,b,s,k1,k2,n,m;
	cin>>a>>b>>s>>k1>>k2>>n>>m;
	for(int i=1;i<=m;i++)
	{
		int x,y;
		cin>>x>>y;
		e[x].push_back(y);
		e[y].push_back(x);
	}
	bfs1(n);
	if(d1[k1]==-1||d1[k2]==-1)
	{
		cout<<-1;
		return 0;
	}
	bfs2(k1);
	bfs3(k2);
	long long ans=d1[k1]*a+(d1[k2]*b);
	for(int i=1;i<=n;i++)
	{
		if(d2[i]!=-1&&d3[i]!=-1&&d1[i]!=-1)
		 ans=min(ans,d2[i]*a+(d3[i]*b)+(d1[i]*(a+b-s)));
		//cout<<i<<" "<<d2[i]*a+(d3[i]*b)+(d1[i]*(a+b-s))<<endl;
	}
	cout<<ans;
	return 0;
}
   

3.

//二分查找 二分答案
#include<iostream>
using namespace std;
const int maxn=100010;
long long t[maxn];
long long v[maxn];
long long w=1e12;
int id=0,n,s=0;
long long ch(long long x)
{
	long long ans=0,vi=0;
	for(int i=1;i<=n;i++)
	{
		if(t[i]<x)
		{
			ans=ans+((x-t[i])*v[i]);
			if((x-t[i])*v[i]>vi)
			{
				vi=(x-t[i])*v[i];
				id=i;
			}
		}
	}
    return ans;
}
long long ch2(long long x)
{
	long long ans=0,vi=0;
	for(int i=1;i<=n;i++)
		if(t[i]<x&&i!=id)
			ans=ans+((x-t[i])*v[i]);
    return ans;
}
int main()
{
	long long m;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>t[i]>>v[i];
	    if(v[i]==0)s++;
	}
	if(s>=n-1)
	{
		cout<<"-1";
		return 0;
	}
	long long l=1,r=w,z;
	while(l<r)
	{
	    z=(l+r)/2;
		if(ch(z)>=m)
			r=z;
		else
		    l=z+1;
	}
	ch(l);
	//cout<<id<<"!!!";
	r=w;
	while(l<r)
	{
		//cout<<l<<r<<endl;
		z=(l+r)/2;
		if(ch2(z)>=m)
			r=z;
		else l=z+1;
	}
	cout<<r;	
	return 0;
}
		

5.

//二分答案+注意特判(又是二分)
#include<iostream>
using namespace std;
long long k,p;
long long ch(long long x)
{
	long long ans=x;
	while(x>=p&&ans<k)
	{
		ans=ans+(x/p);
		x=(x/p)+(x%p);
	}
	return ans;
}
	
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		cin>>p>>k;
	    if(k==0)cout<<0<<"\n";
	    else if(p==1)cout<<1<<"\n";
	    else
	    {
		long long l=0,r=k+10;
		while(l<r)
		{
			int z=(l+r)/2;
			if(ch(z)>=k)
			{
				r=z;
			}
			else l=z+1;
		}
		cout<<l<<"\n";
	    }
	}
	return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值