hdu-1142 A Walk Through the Forest
来源:互联网 发布:阿里云os应用中心 编辑:程序博客网 时间:2024/06/06 01:04
http://acm.hdu.edu.cn/showproblem.php?pid=1142
看了大神的题解!!!
题意:看样子很多人都把这题目看错了,以为是求最短路的条数。真正的意思是:假设 A 和 B 是相连的,当前在 A 处,如果 A 到终点的距离大于 B 到终点的距离,
则可以从 A 通往 B 处,问满足这种的条件的路径条数。
分析:1、以终点 2 为起点 dijkstra;
2、直接DFS记忆化搜索。
*/
#include<cstdio>#include<cstring>#define N 1010#define MAX 1000000int num,road,map[N][N],dis[N],dp[N];bool vis[N];void dijkstra(int start){ int temp,k; memset(vis,0,sizeof(vis)); for(int i=1;i<=num;i++) dis[i]=map[start][i]; dis[start]=0; vis[start]=1; for(int i=1;i<=num;i++) { temp=MAX; for(int j=1;j<=num;j++) if(!vis[j]&&temp>dis[j]) temp=dis[k=j]; if(temp==MAX) break; vis[k]=1; for(int j=1;j<=num;j++) if(!vis[j]&&dis[j]>dis[k]+map[k][j]) dis[j]=dis[k]+map[k][j]; }}int dfs(int v){ int sum=0; if(dp[v]!=-1) return dp[v];//记忆,节约时间 if(v==2) return 1; for(int i=1;i<=num;i++)//v到i有路且v到终点的距离大于i到终点的距离 { if(map[v][i]!=MAX&&dis[v]>dis[i]) sum+=dfs(i); } dp[v]=sum; return dp[v];}int main(){ int x,y,cost; while(scanf("%d",&num)!=EOF&&num) { scanf("%d",&road); for(int i=1;i<=num;i++) { dp[i]=-1; for(int j=1;j<=num;j++) map[i][j]=MAX; } for(int i=1;i<=road;i++) { scanf("%d%d%d",&x,&y,&cost); map[x][y]=map[y][x]=cost; } dijkstra(2);//终点开始最短路 printf("%d\n",dfs(1));//起点深搜,得到满足题意的路径条数 } return 0;}
0 0
- HDU 1142 A Walk Through the Forest
- A Walk Through the Forest HDU 1142
- hdu 1142 A Walk Through the Forest
- Hdu-1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- c++学习笔记
- http://docs.oracle.com/cd/E18283_01/server.112/e17766/framework_errormessages.htm
- STL 树
- 自制jquery插件
- error:unknow filesystem grub rescue 的解决
- hdu-1142 A Walk Through the Forest
- CF 293 E Close Vertices (树的分治+树状数组)
- android listview中smoothscrolltoposition的问题
- socket.io+angular.js+express.js做个聊天应用(一)
- 动态规划解决01背包问题
- OX2D 自然的旋转到一个指定角度
- TI_DSP_corePac_带宽管理 - 1.3(仲裁寄存器default值)
- BZOJ 2001([Hnoi2010]City 城市建设-CDQ重构图-动态最小生成树)
- 在四位共阴极数码上显示“2 3 5 8”四个数字