标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3197 Accepted Submission(s): 960
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> #include<queue> #define INF 0x7fffffff using namespace std; queue<int> q; int n,dist[500][500],vis[500][500],a[500][500]; long long dp[500][500]; int dic[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; bool check(int x,int y) { if(x<1||x>n||y<1||y>n) return false; return true; } void spfa() { dist[n][n]=a[n][n]; vis[n][n]=1; q.push(n),q.push(n); int x,y,xx,yy; while(!q.empty()) { x=q.front(),q.pop(); y=q.front(),q.pop(); vis[x][y]=0; for(int i=0;i<4;i++) { xx=x+dic[i][0],yy=y+dic[i][1]; if(!check(xx,yy)) continue; if(dist[xx][yy]>dist[x][y]+a[xx][yy]) { dist[xx][yy]=dist[x][y]+a[xx][yy]; if(!vis[xx][yy]) vis[xx][yy]=true,q.push(xx),q.push(yy); } } } } long long dfs(int x,int y) { if(dp[x][y]!=-1) return dp[x][y]; dp[x][y]=0; for(int i=0;i<4;i++) { int xx,yy; xx=x+dic[i][0],yy=y+dic[i][1]; if(!check(xx,yy)) continue; if(dist[x][y]>dist[xx][yy]) { dp[x][y]+=dfs(xx,yy); } } return dp[x][y]; } int main() { while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { scanf("%d",&a[i][j]); vis[i][j]=0; dist[i][j]=INF; dp[i][j]=-1; } spfa(); dp[n][n]=1; printf("%I64d\n",dfs(1,1)); } return 0; }
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4287278.html