HDU_1142(最短路 + dfs)

Jimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he likes to walk home. To make things even nicer, his office is on one side of a forest, and his house is on the other. A nice walk through the forest, seeing the birds and chipmunks is quite enjoyable.
The forest is beautiful, and Jimmy wants to take a different route everyday. He also wants to get home before dark, so he always takes a path to make progress towards his house. He considers taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A. Calculate how many different routes through the forest Jimmy might take.
Input

Input contains several test cases followed by a line containing 0. Jimmy has numbered each intersection or joining of paths starting with 1. His office is numbered 1, and his house is numbered 2. The first line of each test case gives the number of intersections N, 1 < N ≤ 1000, and the number of paths M. The following M lines each contain a pair of intersections a b and an integer distance 1 ≤ d ≤ 1000000 indicating a path of length d between intersection a and a different intersection b. Jimmy may walk a path any direction he chooses. There is at most one path between any pair of intersections.
Output

For each test case, output a single integer indicating the number of different routes through the forest. You may assume that this number does not exceed 2147483647
Sample Input

5 6
1 3 2
1 4 2
3 4 3
1 5 12
4 2 34
5 2 24
7 8
1 3 1
1 4 1
3 7 1
7 4 1
7 5 1
6 7 1
5 2 1
6 2 1
0

Sample Output

2
4

题意: 给你一个图,找路。但是有个的条件:每走一步,如你选择要走a 到 b (如果a,b之间有路),那么必须保证a到终点的所有路 都小于 b到终点的每一条路。问满足这样的路径条数 有多少思路:  1. 1为起点,2为终点,因为要走ab路时,必须保证那个条件,所以从终点开始使用单源最短路Dijkstra算法,得到每个点到终点的最短路,保存在dis[]数组中。   2. 然后从起点开始深搜每条路,看看满足题意的路径有多少条。   3. 这样搜索之后,dp[1]就是从起点到终点所有满足题意的路径的条数。
 1 #include <iostream>
 2 #include <string.h>
 3 using namespace std;
 4 const int MAXN = 1234;
 5 const int INF = 12345678;
 6 int n, m;
 7 int vis[MAXN];
 8 int g[MAXN][MAXN];
 9 int dis[MAXN];
10 int dp[MAXN];
11
12 void init(){
13     for(int i = 1; i <= n; i++){
14         for(int j = 1; j <= n; j++){
15             if(i == j)
16                 g[i][j] = 0;
17             else g[i][j] = INF;
18         }
19     }
20     memset(vis,0,sizeof(vis));
21     memset(dp,-1,sizeof(dp));
22 }
23
24 void dij(int v0){
25     int pos = v0;
26     for(int i = 1; i <= n; i++){
27         dis[i] = g[v0][i];
28     }
29     vis[pos] = 1;
30     for(int i = 1; i < n; i++){
31         int mins = INF;
32         for(int j = 1; j <= n; j++){
33             if(!vis[j] && dis[j] < mins){
34                 pos = j;
35                 mins = dis[j];
36             }
37         }
38         vis[pos] = 1;
39         for(int j = 1; j <= n; j++){
40             if(!vis[j] && dis[j] > dis[pos] + g[pos][j])
41                 dis[j] = dis[pos] + g[pos][j];
42         }
43     }
44 }
45
46 int dfs(int start){
47     int ans = 0;
48     if(dp[start] != -1)
49         return dp[start];
50     if(start == 2)
51         return 1;
52     for(int i = 1; i <= n; i++){
53         if(g[start][i] != INF && dis[start] > dis[i])
54             ans += dfs(i);
55     }
56     dp[start] = ans;
57     return dp[start];
58 }
59
60 int main(){
61     while(cin >> n && n){
62         init();
63         cin >> m;
64         for(int i = 1; i <= m; i++){
65             int a, b, w;
66             cin >> a >> b >> w;
67             g[a][b] = g[b][a] = w;
68         }
69         dij(2);
70         cout << dfs(1) << endl;
71     }
72 }
时间: 2024-10-11 04:14:20

HDU_1142(最短路 + dfs)的相关文章

Uva 1600 Patrol Robot (BFS 最短路/DFS剪枝)

这道题运用的知识点是求最短路的算法.一种方法是利用BFS来求最短路. 需要注意的是,我们要用一个三维数组来表示此状态是否访问过,而不是三维数组.因为相同的坐标可以通过不同的穿墙方式到达. #include <bits/stdc++.h> using namespace std; struct Node{ int r; int c; int g; int cnt; Node(int r,int c,int g,int cnt):r(r),c(c),g(g),cnt(cnt){} }; int v

PAT-1111 Online Map (30分) 最短路+dfs

明天就要考PAT,为了应付期末已经好久没有刷题了啊啊啊啊,今天开了一道最短路,状态不是很好 1.没有读清题目要求,或者说没有读完题目,明天一定要注意 2.vis初始化的时候从1初始化到n,应该从0开始,以后初始化就从0到n即可 题目大意:给一张地图,两个结点中既有距离也有时间,有的单行有的双向,要求根据地图推荐两条路线:一条是最快到达路线,一条是最短距离的路线. 第一行给出两个整数N和M,表示地图中地点的个数和路径的条数.接下来的M行每一行给出:道路结点编号V1 道路结点编号V2 是否单行线 道

hdu 1142 A Walk Through the Forest (最短路+dfs )

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5809    Accepted Submission(s): 2147 Problem Description Jimmy experiences a lot of stress at work these days, especial

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

图论五百题!

生死看淡不服就淦,这才是人生! =============================以下是最小生成树+并查集======================================[HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

PAT (Advanced Level) 1003. Emergency (25)

最短路+dfs 先找出可能在最短路上的边,这些边会构成一个DAG,然后在这个DAG上dfs一次就可以得到两个答案了. 也可以对DAG进行拓扑排序,然后DP求解. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using nam

图论精炼500题

忘了从哪转的了... =============================以下是最小生成树+并查集====================================== [HDU] 1213               How Many Tables                    基础并查集★ 1272               小希的迷宫                     基础并查集★ 1325&&poj1308    Is It A Tree?       

hdu图论题目分类

=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 基础并查集★ 1325&&poj1308 Is It A Tree? 基础并查集★ 1856 More is better 基础并查集★ 1102 Constructing Roads 基础最小生成树★ 1232 畅通工程 基础并查集★ 123