hdu 1142 A Walk Through the Forest
来源:互联网 发布:被淘宝卖家骂了怎么办 编辑:程序博客网 时间:2024/06/07 19:56
题目大意:找出编号为“1”的点到编号为“2”的点的路径数目。满足条件的路径定义为:A点到B点相连,且A到目标点的最短距离大于B到目标的的距离。
做法:从2点开始找出它到其他点的最短距离,然后直接对其进行dfs回溯,求出所有的解。但是这里得注意下,就是dfs过程中,要用到记忆化搜索,将搜索过的点保存下来。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn=1005;const int INF=1<<29;int Graph[maxn][maxn];int dist[maxn];void dijkstra(int st,int n){ bool s[maxn]; for(int i=1;i<=n;i++) { dist[i]=Graph[st][i]; s[i]=0; } s[st]=1; dist[st]=0; for(int i=2;i<=n;i++) { int mindist=INF; int u=st; for(int j=1;j<=n;j++) if(!s[j] && dist[j]<mindist) { u=j; mindist=dist[j]; } s[u]=1; for(int j=1;j<=n;j++) if(!s[j] && Graph[u][j]!=INF && Graph[u][j]+dist[u]<dist[j]) dist[j]=dist[u]+Graph[u][j]; }}int dp[maxn];int dfs(int k,int n){ if(dp[k])return dp[k]; if(k==2)return 1; for(int i=1;i<=n;i++) if(Graph[k][i]!=INF && dist[k]>dist[i]) dp[k]+=dfs(i,n); return dp[k];}int main(){ int n,m; int a,b,d; // freopen("in.txt","r",stdin); while(scanf("%d",&n) && n) { scanf("%d",&m); memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) Graph[i][j]=INF; Graph[i][i]=0; } for(int i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&d); Graph[a][b]=Graph[b][a]=d; } dijkstra(2,n); int res=dfs(1,n); printf("%d\n",res); } return 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
- Gerrit简易安装入门
- MFC窗口透明
- 【设计模式】学习笔记1:策略模式(Strategy)
- C++虚函数
- ScrollView中若嵌套有滑动的控件(如:WebView,ListView或GridView)焦点问题
- hdu 1142 A Walk Through the Forest
- android体系化展示
- tomcat 大并发报错 Maximum number of threads (200) created for connector with address null and port 8080 .
- hdu 4619 (二分匹配,最大匹配)
- gdb android 调试cgdbserver
- cg profiles,什么是profiles,以及如何使用CGC编译Cg语言
- 自己验证的一片扯淡的骨架提取论文
- status和state
- 嵌入式领域linux作为实时操作系统的缺点