90分求助!
  • 板块P1130 红牌
  • 楼主沉鸣cmh
  • 当前回复6
  • 已保存回复6
  • 发布时间2020/8/30 13:52
  • 上次更新2023/11/5 13:58:00
查看原帖
90分求助!
334041
沉鸣cmh楼主2020/8/30 13:52

超时了一个点,能满吗?

#include<bits/stdc++.h>
using namespace std;
int n,m,a[2005][2005],su[2005][2005],sum=1e9;
int xx(int x){
	if(x+1<=m)return x+1;
	return 1;
}
int read(){
    int x=0,f=1;
    char ch;
    while(ch<'0'||ch>'9')  {if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return f*x;
}
int dfs(int wz,int k){int th=1e9;
	if(su[wz][k])return su[wz][k];if(wz==n+1)return 0;
	th=min(th,dfs(wz+1,xx(k))+a[xx(k)][wz]);
	th=min(th,dfs(wz+1,k)+a[k][wz]);
	return su[wz][k]=th;
}
int main(){
	n=read(),m=read();for(int i=1;i<=m;i++)for(int j=1;j<=n;j++)a[i][j]=read();
	for(int i=1;i<=m;i++)sum=min(sum,dfs(1,i));
	cout<<sum;
	return 0;
} 
2020/8/30 13:52
加载中...