hdu1428之spfa+dfs
漫步校园
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2421 Accepted Submission(s): 715
1 2 3
1 2 3
1 2 3
3
1 1 1
1 1 1
1 1 1
6
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=50+10;
__int64 s[MAX][MAX],dist[MAX][MAX],sum[MAX][MAX];
bool mark[MAX][MAX];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
int n; void spfa(int x,int y){
int a,b,oq;
queue<int>q;
q.push(x*n+y);
mark[x][y]=true;
while(!q.empty()){
oq=q.front();
q.pop();
a=oq/n,b=oq%n;
mark[a][b]=false;
for(int i=0;i<4;++i){
x=a+dir[i][0];
y=b+dir[i][1];
if(x<0 || y<0 || x>=n || y>=n)continue;
if(dist[a][b]+s[x][y]<dist[x][y]){
dist[x][y]=dist[a][b]+s[x][y];
if(!mark[x][y])q.push(x*n+y);
mark[x][y]=true;
}
}
}
} __int64 dfs(int a,int b){
if(mark[a][b])return sum[a][b];
int x,y;
for(int i=0;i<4;++i){
x=a+dir[i][0];
y=b+dir[i][1];
if(x<0 || y<0 || x>=n || y>=n)continue;
if(dist[x][y]>=dist[a][b])continue;
sum[a][b]+=dfs(x,y);
}
mark[a][b]=true;
return sum[a][b];
} void Init(int n){
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
dist[i][j]=INF;
sum[i][j]=0;
mark[i][j]=false;
}
}
dist[n-1][n-1]=s[n-1][n-1];
sum[n-1][n-1]=1;
} int main(){
while(~scanf("%d",&n)){
for(int i=0;i<n;++i){
for(int j=0;j<n;++j)scanf("%d",&s[i][j]);
}
Init(n);
spfa(n-1,n-1);
mark[n-1][n-1]=true;
printf("%I64d\n",dfs(0,0));
}
return 0;
}
hdu1428之spfa+dfs的更多相关文章
- 【PAT甲级】1003 Emergency (25 分)(SPFA,DFS)
题意:n个点,m条双向边,每条边给出通过用时,每个点给出点上的人数,给出起点终点,求不同的最短路的数量以及最短路上最多能通过多少人.(N<=500) AAAAAccepted code: #in ...
- 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)
题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
- 【PAT甲级】1018 Public Bike Management (30 分)(SPFA,DFS)
题意: 输入四个正整数C,N,S,M(c<=100,n<=500),分别表示每个自行车站的最大容量,车站个数,此次行动的终点站以及接下来的M行输入即通路.接下来输入一行N个正整数表示每个自 ...
- POJ3621Sightseeing Cows[01分数规划 spfa(dfs)负环 ]
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9703 Accepted: 3299 ...
- PAT天梯赛练习题 L3-011. 直捣黄龙(多关键字SPFA+DFS)
L3-011. 直捣黄龙 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题是一部战争大片 —— 你需要从己方大本营出发,一路 ...
- PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- Shopping(SPFA+DFS HDU3768)
Shopping Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- POJ 3411 Paid Roads(SPFA || DFS)
题目链接 题意 : 要从1城市到n城市,求最短路是多少,从a城市到达b城市的路程,如果你到过c城市,则需要走p,否则走r长. 思路 : 因为可以来回走,所以不能用单纯的最短路,可以用二维SPFA,状态 ...
- Key Vertex (hdu 3313 SPFA+DFS 求起点到终点路径上的割点)
Key Vertex Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
随机推荐
- SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3457&rd=5869 解答分析:http://comm ...
- 真机測试时的错误:No matching provisioning profiles found
1.出现错误的原因是这种---- 公司接收一个外包项目,原来做真机測试的时候,用的是公司申请的苹果开发人员账号.如今项目结束了,准备上线,但客户要求使用客户自己的苹果开发人员是账号上线,于是就用客户的 ...
- 来推荐个免费的PPT演示工具--ZohoShowTime
事实上这个不算新产品了,这次是做了一些大的改进.上次在Zoho的全球用户大会上,全程演讲都是用的这个工具.Zoho这点非常好啊.自己的产品自己带头用.个人认为它最大的用处就是.离得远的观众能够在自己的 ...
- sqlserver 存储过程实例
ALTER PROC [dbo].[SP_mm_NS] ( @ID NVARCHAR(60), @ReturnCode NVARCHAR(30) OUT ...
- delphiXE调用Objective-c库
http://stackoverflow.com/questions/16515218/xe4-firemonkey-ios-static-library-pascal-conversion-from ...
- 在WPF使用FolderBrowserDialog和OpenFileDialog
原文 在WPF使用FolderBrowserDialog和OpenFileDialog 相信习惯以前winform开发的朋友们都对FolderBrowserDialog和OpenFileDialog这 ...
- UVA 10494-If We Were a Child Again(一流的塔尔苏斯)
Problem C If We Were a Child Again Input: standard input Output: standard output seconds "Oo ...
- MFC如何生成一个可串行化的类
一.MFC允许对象在程序运行的整个过程中持久化的串行化机制 (1)串行化是指向持久化存储媒介(如一个磁盘文件)读或写对象的过程. (2)串行化用于在程序运行过程时或之后修复结构化数据(如C++类或结构 ...
- 深入研究Clang(四) Clang编译器的简单分析
作者:史宁宁(snsn1984) 首先我们确定下Clang编译器的详细内容和涵盖范围.之前在<LLVM每日谈之二十 Everything && Clang driver>中 ...
- Java中ArrayList和LinkedList差别
一般大家都知道ArrayList和LinkedList的大致差别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机訪问get和set.A ...