Codeforces Gym - 101059C Gangsters [状态压缩]

本文介绍了一种结合图论与状态压缩算法解决路径寻优问题的方法,通过BFS遍历寻找从起点到终点的最小花费路径,同时考虑了特定条件下需要支付额外费用的情况。

题意:给你n个点m条边的图,再给出p个强盗和k,表示点距离强盗最短距离小于等于k的时候需要收钱。问从起点s到终点t的最小花费,每条边的长度为1

题解:首先对每个强盗做bfs,对bfs到的点打上该强盗的标记。然后状态压缩10个强盗,表示能否经过这些海盗从s走到t,由于状态压缩枚举所有情况,所以对所有s能到t的情况取min,就能得到答案。

AC代码:

#include<stdio.h>
#include<vector>
#include<queue>
#include<string.h>
#define N 100005
using namespace std;
typedef long long ll;
vector<ll>vt[N];
ll have[N];
ll n,m,K,p;
ll a[15],v[15];
ll mark[N];
ll mm[15];
ll s,t;
ll ans=1000000000000ll;
struct node
{
	ll v,w;
	node(){}
	node(ll v,ll w)
	{
		this->v=v;
		this->w=w;
	}
};
void bfs(ll u,ll biao)
{
	queue<node>que;
	memset(mark,0,sizeof(mark));
	que.push(node(u,0));
	mark[u]=1;
	while(!que.empty())
	{
		node k=que.front();
		que.pop();
		have[k.v]|=biao;
		if(k.w==K)continue;
		for(ll i=0;i<vt[k.v].size();i++)
		{
			ll to=vt[k.v][i];
			if(mark[to]==1)continue;
			mark[to]=1;
			que.push(node(to,k.w+1));
		}
	}
}
ll bfs2()
{
	queue<ll>que;
	memset(mark,0,sizeof(mark));
	que.push(s);
	mark[s]=1;
	while(!que.empty())
	{
		ll k=que.front();
		que.pop();
		for(ll i=0;i<vt[k].size();i++)
		{
			ll to=vt[k][i];
			if(mark[to]==1)continue;
			if(have[to]==0)
			{
				if(to==t)
					return 1;
				mark[to]=1;
				que.push(to);
			}
			else 
			{
				int flag=1;
				for(ll j=0;j<p;j++)
				{
					if(!(have[to]&(1<<j)))continue;
					if((have[to]&(1<<j))&&mm[j]);
					else flag=0;
				}
				if(flag==0)continue;
				if(to==t)
					return 1;
				mark[to]=1;
				que.push(to);
			}
		}
	}
	return 0;
}
int main()
{
	scanf("%lld%lld%lld%lld",&n,&m,&p,&K);	
	for(ll i=1;i<=p;i++)
		scanf("%lld",&a[i]);
	for(ll i=1;i<=p;i++)
		scanf("%lld",&v[i]);
	for(ll i=0;i<m;i++)
	{
		ll u,v;
		scanf("%lld%lld",&u,&v);
		vt[u].push_back(v);
		vt[v].push_back(u);
	}
	for(ll i=1;i<=p;i++)
		bfs(a[i],(1<<(i-1)));
	scanf("%lld%lld",&s,&t);
	if(s==t)
	{
		ll pay=0;
		for(ll j=0;j<p;j++)
			if(have[t]&(1<<j))
				pay+=v[j+1];
		printf("%lld\n",pay);
		return 0;
	}
	for(ll state=0;state<(1<<p);state++)
	{
		memset(mm,0,sizeof(mm));
		ll pay=0;
		for(ll j=0;j<p;j++)
			if(state&(1<<j))
			{
				mm[j]=1;
				pay+=v[j+1];
			}
		if(have[s]!=0&&have[s]!=state)continue;
		if(pay<ans)
			if(bfs2())
				ans=pay;
	}
	printf("%lld\n",ans);
}


### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值