求助,蒟蒻WA on test 20
  • 板块CF1271D Portals
  • 楼主charm1
  • 当前回复0
  • 已保存回复0
  • 发布时间2021/4/9 09:39
  • 上次更新2023/11/5 00:50:48
查看原帖
求助,蒟蒻WA on test 20
135258
charm1楼主2021/4/9 09:39

RT

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=5005;
const int maxm=300005;
int n,m,tot,cnt;
int sum[maxn],w[maxn],val[maxn],c[maxn],head[maxn],dp[maxn][maxn];
bool vis[maxn];
struct edge{
	int v,nxt;
}a[maxm];
struct sav{
	int x,y;
}s[maxm];
bool cmp(sav x,sav y){return x.x>y.x;}
void add(int x,int y){
	++cnt;
	a[cnt].v  =y;
	a[cnt].nxt=head[x];
	head[x]=cnt;
}
inline int read(){
	int ret=0,f=1;	char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
	while(isdigit(ch)){ret=ret*10+ch-'0';ch=getchar();}
	return ret*f;
}
void scan(){
	n=read();   m=read();   tot=read();
	for(int k=1;k<=n;k++)
	w[k]=read(),val[k]=read(),c[k]=read();
	for(int k=1;k<=m;k++){
		int x,y;
		x=read();   y=read();
		s[k].x=x; s[k].y=y;
	}
	sort(s+1,s+m+1,cmp);
	for(int k=1;k<=m;k++){
		int x,y;
		x=s[k].x;	y=s[k].y;
		if(vis[y])  continue;
		vis[y]=1;	add(x,y);
	}
}
void solve(){
	memset(dp,-0x3f,sizeof(dp));
	dp[0][0]=0;
	for(int k=1;k<=n;k++){
		if(tot<w[k]){
			puts("-1");
			return;
		}
		for(int j=min(k-1,tot-w[k]);j>=0;j--)	dp[k][j]=dp[k-1][j];
		if(!vis[k])
		for(int j=min(k-1,tot-w[k]);j>=0;j--)	dp[k][j+1]=max(dp[k][j+1],dp[k][j]+c[k]);
		for(int j=head[k];j;j=a[j].nxt){
			int v=a[j].v;
			for(int i=min(k-1,tot);i>=0;i--)	dp[k][i+1]=max(dp[k][i+1],dp[k][i]+c[v]);
		}
//		for(int j=0;j<=k;j++)    cout<<dp[k][j]<<" ";
//		puts("");
		tot+=val[k];
	}
	int ans=-1;
	for(int k=1;k<=n;k++)   ans=max(ans,dp[n][k]);
	printf("%lld\n",ans);
}
signed main(){
	scan(); solve();
	return 0;
}
2021/4/9 09:39
加载中...