DFS求助!!!
查看原帖
DFS求助!!!
546597
ymyctsz楼主2023/11/15 07:46

不知道哪里错了

#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline ll read()
{
	ll x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
	return x*f;
}
const int N=101010;
int n,m,logn=0,ans=0x3f3f,k=-0x3f3f;
int s[N],t[N],c[N],a[N],b[N],p[N],mm[N],need[N];
bool vis[N];
inline void dfs(int ch,int mey)
{
	if(ch-1==m)
	{
	    for(int i=1;i<=logn;i++) if(need[i]>0)return;
	    ans=min(ans,mey);return;
	}
	for(int i=ch;i<=m;i++)
	{
		if(vis[i]==1) return;vis[i]=1;
		for(int j=a[i];j<=b[i];j++) need[j]-=p[i];mey+=mm[i];
		dfs(i+1,mey);
		for(int j=a[i];j<=b[i];j++) need[j]+=p[i];mey-=mm[i];vis[i]=0;
	}
}
int main()
{
	vis[0]=0;
	n=read(),m=read();
	for(int i=1;i<=n;i++)
	{
		s[i]=read(),t[i]=read(),c[i]=read(),logn=max(logn,t[i]);
		for(int j=s[i];j<=t[i];j++) need[j]+=c[i];
	}
	for(int i=1;i<=m;i++)a[i]=read(),b[i]=read(),p[i]=read(),mm[i]=read();
	dfs(0,0);
	cout<<ans<<endl;
    return 0;
} 
数据:
9 10     
54 66 23488  
49 53 29032  
38 48 128859  
92 92 1   
19 19 128859  
20 37 228179  
11 18 105371   
2 10 99827  
70 84 72013  
11 53 5544 789  
26 76 844 610  
4 28 35866 497  
12 74 80762 508  
92 92 1 89  
19 66 23488 164  
2 48 99827 49  
70 84 72013 555  
20 37 99320 492  
39 41 94103 811  

cout:2138
my cout:2949
2023/11/15 07:46
加载中...