HDU ACM 1142 A Walk Through the Forest (最短路径Dijkstra + 深搜DFS)
2012-08-27 16:03
363 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1142
题意:点1为起点 点2为终点 从点1走到点x点后,要求点x到点2的距离小于点1到点2的距离 统计路径数目.
输入:n为点的数目 m为道路数目
a b c 为 a到b的权值为c
输出:路径数目
思路:用dijkstra +DFS()
dijkstra:找出所有点到点2的最短路径,但是遍历所有点到2在记录会超时,所以用2为起点求到各点的最短路径.
DFS():需要剪枝,否则也会超时 用mark()数组记录之前进行过DFS得点数.
View Code
题意:点1为起点 点2为终点 从点1走到点x点后,要求点x到点2的距离小于点1到点2的距离 统计路径数目.
输入:n为点的数目 m为道路数目
a b c 为 a到b的权值为c
输出:路径数目
思路:用dijkstra +DFS()
dijkstra:找出所有点到点2的最短路径,但是遍历所有点到2在记录会超时,所以用2为起点求到各点的最短路径.
DFS():需要剪枝,否则也会超时 用mark()数组记录之前进行过DFS得点数.
View Code
#include <iostream> using namespace std; const int MAX = 1000 + 10; const int INF = 0x3fffffff; int map[MAX][MAX]; int used[MAX]; int dis[MAX]; int n; int Dijkstra(int start) { memset(used,0,sizeof(used)); int i; for(i=1;i<=n;i++) { dis[i]= INF; } dis[start] = 0; for(int j=1;j<=n;j++) { int min=INF; int mid=0; for(i=1;i<=n;i++) { if( !used[i] && dis[i]<min ) { min=dis[i]; mid=i; } } used[mid]=1; for(i=1;i<=n;i++) { if( !used[i] && dis[i]>dis[mid]+map[mid][i] && map[mid][i]) { dis[i]=dis[mid]+map[mid][i]; } } } return 1; } int sum; int visit[MAX]; int mark[MAX]; int DFS(int x) { int i; if(mark[x]) { return mark[x]; } if(x == 2) { return 1; } for(i=1;i<=n;i++) { if(visit[i] == 0 && map[x][i] && dis[i] < dis[x]) { mark[x] +=DFS(i); } } return mark[x]; } int main() { int m; while(cin>>n,n) { memset(map,0,sizeof(map)); memset(visit,0,sizeof(visit)); memset(mark,0,sizeof(mark)); cin>>m; int i; for(i=1;i<=m;i++) { int a,b,c; cin>>a>>b>>c; map[a][b] = map[b][a] = c; } Dijkstra(2); cout<<DFS(1)<<endl; } return 0; }
相关文章推荐
- hdu 1142 A Walk Through the Forest(最短路径+dfs)
- HDOJ 1142 A Walk Through the Forest 【Dijkstra】+【DFS】
- 【Dijkstra+DFS(记忆化)】hdu 1142 A Walk Through the Forest(外:hdu 1428)
- hdoj 1142 A Walk Through the Forest 【dijkstra + dfs】
- HDU 1142 A Walk Through the Forest(Dijkstra+Dfs(第一次用记忆化搜索))
- HDU 1142 A Walk Through the Forest(dijkstra+记忆化DFS)
- HDU--1142--A Walk Through the Forest--深广搜/DP/最短路径/记忆化搜索
- HDU-1142 A Walk Through the Forest(最短路径+记忆化搜索)
- A Walk Through the Forest(最短路径+DFS)
- hdu 1142 A Walk Through the Forest (最短路径)
- HDU-1142-A Walk Through the Forest(Dijkstra+dfs)
- hdu 1142 A Walk Through the Forest【Dijkstra+dfs求最短路条数】
- 【最短路+dfs+dijkstra】杭电 hdu 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest (Dijkstra + 记忆化搜索 好题)
- UVa10917 - Walk Through the Forest(单源最短路径及动态规划)
- HDOJ 1142 A Walk Through the Forest(最短路径)
- 杭电1142 A Walk Through the Forest (最短路+DFS+dp)
- hdu 1142 A Walk Through the Forest(dijkstra)
- hdu 1142 A Walk Through the Forest (最短路+dfs)
- POJ-2662-A Walk Through the Forest;UVA-10917 - Walk Through the Forest(最短路径)