hdu 1142 A Walk Through the Forest(最短路径+dfs)
来源:互联网 发布:javascript和jquery 编辑:程序博客网 时间:2024/05/21 07:58
http://acm.h
大致题意:若a,b之间有边,走ab边的条件是b到目的地的最短路径小于a到目的地的最短路径。按这个要求走,问最后从1到2的路径数目。
思路:先预处理到2的最短路径。然后从1dfs寻求路径数目。有点像DAG上的动态规划求路径数目。
#include <stdio.h>#include <algorithm>#include <set>#include <map>#include <vector>#include <math.h>#include <string.h>#define LL long long#define _LL __int64using namespace std;const int INF = 0x3f3f3f3f;int n,m;int mapp[1100][1100];int dis[1100];int vis[1100];int cnt[1100];void init(){for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){if(i != j)mapp[i][j] = INF;else mapp[i][j] = 0;}}}void dijstra(int s){memset(vis,0,sizeof(vis));memset(dis,INF,sizeof(dis));vis[s] = 1;for(int i = 1; i <= n; i++)dis[i] = mapp[s][i];for(int i = 1; i < n; i++){int min = INF;int pos;for(int j = 1; j <= n; j++){if(!vis[j] && min > dis[j]){min = dis[j];pos = j;}}vis[pos] = 1;for(int j = 1; j <= n; j++){if(!vis[j] && dis[j] > dis[pos] + mapp[pos][j])dis[j] = dis[pos] + mapp[pos][j];}}}//求1到2的路径数目int dfs(int s){if(s == 2)return cnt[2] = 1;if(cnt[s] >= 0)return cnt[s];else{int res = 0;for(int i = 1; i <= n; i++){if(mapp[s][i] != INF && dis[i] < dis[s])res += dfs(i);}return cnt[s] = res;}}int main(){int u,v,w;while(~scanf("%d",&n) && n){scanf("%d",&m);init();while(m--){scanf("%d %d %d",&u,&v,&w);mapp[u][v] = mapp[v][u] = w;}dijstra(2); //预处理最短路径memset(cnt,-1,sizeof(cnt));int ans = dfs(1);printf("%d\n",ans);}return 0;}
0 0
- hdu 1142 A Walk Through the Forest(最短路径+dfs)
- A Walk Through the Forest(最短路径+DFS)
- HDU 1142 A Walk Through the Forest 最短路+DFS
- hdu 1142 A Walk Through the Forest (最短路+dfs )
- HDU--1142--A Walk Through the Forest--深广搜/DP/最短路径/记忆化搜索
- HDU-1142 A Walk Through the Forest(最短路径+记忆化搜索)
- A Walk Through the Forest(最短路径+深搜)
- HDOJ 1142 A Walk Through the Forest(最短路径)
- HDU-1142-A Walk Through the Forest(Dijkstra+dfs)
- HDU 1142 A Walk Through the Forest (搜索-DFS)
- 【最短路+dfs+dijkstra】杭电 hdu 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest【Dijkstra+dfs求最短路条数】
- HDU 1142 A Walk Through the Forest(spfa最短路+dfs记忆化搜索)
- 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
- A Walk Through the Forest HDU 1142
- unity3d 克服不支持gif,实现动画
- 关于iOS开发的CoreData
- cookie的使用
- 查找LOV的SQL脚本
- ubuntu下整合apache2与tomact
- hdu 1142 A Walk Through the Forest(最短路径+dfs)
- elasticsearch资料
- sybase 备份和恢复
- BroadcastReceiver应用详解
- freebsd下服务硬件性能测试
- Sample Mean Shift 样本均值漂移原理
- js语法
- WCF REST 工作总结(一)
- 常用邮件协议及其命令行使用