C. 连锁商店(暴力优化)

[连锁商店](https://codeforces.com/gym/103389/problem/C)
如果 1-2 ,2-3,1-3,则可以把边1-3去掉不影响答案

#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int inf=8e18;
const int maxn=2e5+100;
int c[maxn],w[maxn];
int ans[maxn];
int mh[50][50];
vector<int>g[maxn];
int sum;
bitset<37> bt;
void dfs(int x,bitset<37> bt,int res)
{
	if(x==1)
	{
		sum=max(sum,res);
		return ;
	}
	if(ans[x]+res<sum)return ;
	for(auto it:g[x])
	{
		if(bt[c[it]]==0)
		{
			bt[c[it]]=1;
			dfs(it,bt,res+w[c[it]]);
			bt[c[it]]=0;
		}
		else dfs(it,bt,res);
	}
}
signed main()
{
	IOS
	int n,m;
	cin>>n>>m;
	for(int i =1;i<=n;i++)
	{
		cin>>c[i];
	}
	for(int i=1;i<=n;i++)
	{
		cin>>w[i];
	}
	for(int i=1;i<=m;i++)
	{
		int x,y;
		cin>>x>>y;
		mh[x][y]=1;
		//g[y].pb(x);
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			for(int k=i+1;k<j;k++)
			{
				if(mh[i][j]&&mh[i][k]&&mh[k][j])mh[i][j]=0;
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			if(mh[i][j]) g[j].pb(i);
		}
	}
	for(int i=1;i<=n;i++)
	{
		bt=0;
		bt[c[i]]=1;
		int res=w[c[i]];
		sum=res;
		dfs(i,bt,res);
		ans[i]=sum;
		cout<<sum<<"\n";
	}
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

thusloop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值