BestCoder Round #74 (div.2 b) hdu5636 Shortest Path 【dfs】
来源:互联网 发布:linux oracle 11g下载 编辑:程序博客网 时间:2024/05/28 19:23
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5637
题意:有一条长度为nn的链. 节点ii和i+1i+1之间有长度为11的边. 现在又新加了3条边, 每条边长度都是1. 给出mm个询问, 每次询问两点之间的最短路.
分析:加了三条边,相当于六条边,每次从起点到终点跑个dfs就好了,裸的dfs复杂度为6!,可以发现经过一条边后就不会经过它的反向边,所以每次dfs复杂度6*4*2,总的复杂度48*10^6,加个剪枝就可以过了800+ms。
代码:
#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<string>#include<vector>#include<queue>#include<cmath>#include<stack>#include<set>#include<map>#define INF 0x3f3f3f3f#define Mn 100010#define Mm 2000005#define mod 1000000007#define CLR(a,b) memset((a),(b),sizeof((a)))#define CPY(a,b) memcpy ((a), (b), sizeof((a)))#pragma comment(linker, "/STACK:102400000,102400000")#define ul u<<1#define ur (u<<1)|1using namespace std;typedef long long ll;int read() { char c=getchar(); int re=0,f=1; while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();} while(c>='0'&&c<='9') {re=re*10+c-'0';c=getchar();} return re;}int x[6],y[6];int vis[6];int dt;int ans;void dfs(int dis,int pos) { if(pos==dt) { ans=min(dis,ans); return ; } dfs(dis+abs(dt-pos),dt); for(int i=0;i<=5;i++) { if(vis[i]) continue; vis[i]=1; vis[(i+3)%6]=1; if(dis+abs(pos-x[i])+1<ans) dfs(dis+abs(pos-x[i])+1,y[i]); vis[i]=0; vis[(i+3)%6]=0; }}int main() { int t=read(); while(t--) { int n=read(),m=read(); for(int i=0;i<=2;i++) { x[i]=read(),y[i]=read(); } for(int i=3;i<=5;i++) { x[i]=y[i-3]; y[i]=x[i-3]; } ll sum=0; for(int i=1;i<=m;i++) { CLR(vis,0); int u=read(),v=read(); if(u>v) swap(u,v); ans=v-u;dt=v; dfs(0,u); sum=(sum+((ll)ans*i)%mod)%mod; } printf("%I64d\n",sum); } return 0;}
0 0
- BestCoder Round #74 (div.2 b) hdu5636 Shortest Path 【dfs】
- hdu5636 BestCoder Round #74 (div.2) 1002 Shortest Path
- hdu5636 Shortest Path BestCoder题目
- BestCoder Round #74 (div.2) A.LCP Array & B.Shortest Path
- BestCoder Round #74 Shortest Path
- BestCoder Round #74 (div.1) 1002Shortest Path(hdoj5636)
- 【HDU5636 BestCoder Round 74 (div1)A】【floyd 最短路 复杂度考核】Shortest Path 一条链3条边1e6个询问的两点最短路
- HDU5636 -- Shortest Path -- floyd
- BestCoder Round #74 (div.2) T2 DFS HDOJ 5636
- BestCoder Round #78 (div.2) A B
- BestCoder Round #82 (div.2) A B
- BestCoder Round #74 (div.2)
- BestCoder Round #64 (div.2) HDOJ5587 Array(dfs)
- Codeforces Round #306 (Div. 2) B(dfs)
- Codeforces Round #306 (Div. 2) B DFS
- B、Infoplane in Tina Town-----(BestCoder Round #51 (div.2) )
- BestCoder Round #58 (div.2) A B C
- BestCoder Round #59 (div.2)B.Reorder the Books
- mysqldump 参数说明
- Android6的Logger日志系统
- studio使用
- Python IDLE无法显示行号、Python IDLE shell里运行py文件
- Java POI通过模板方法生成word文档
- BestCoder Round #74 (div.2 b) hdu5636 Shortest Path 【dfs】
- 历届试题 连号区间数
- 关于内存
- eclipse使用经验(一)运行不了
- C++中头文件嵌套定义问题
- RTSP服务器实例live555源代码分析
- spring mvc 用法
- 如何通过变量去调用对应的函数
- SDWebimage的缓存机制