博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
最短路+DFS
阅读量:6406 次
发布时间:2019-06-23

本文共 3197 字,大约阅读时间需要 10 分钟。

A Walk Through the ForestTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5071    Accepted Submission(s): 1846Problem DescriptionJimmy 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.  InputInput 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.  OutputFor 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 Input5 61 3 21 4 23 4 31 5 124 2 345 2 247 81 3 11 4 13 7 17 4 17 5 16 7 15 2 16 2 10 Sample Output24

  好难的题,好漂亮的深搜啊!

#include
#include
#define MAX 1002#define INF (1<<29)using namespace std;int cost[MAX][MAX];__int64 d[MAX];//最短距离int n, num[MAX];//个数,bool used[MAX];void dijkstra(int s){ int v = -1; fill(d, d + MAX, INF); fill(used, used + MAX, false); d[s] = 0; while (true){ v = -1; for (int i = 1; i <=n; i++){ if (!used[i] && (v == -1 || d[v]>d[i]))v = i; } if (v == -1)break; used[v] = true; for (int i = 1; i <=n; i++){ d[i] = min(d[i], d[v] + cost[v][i]); } }}__int64 dfs(int i){ if (i == 2)return 1;//找到一条路 if (num[i] != -1)return num[i];//i到home的路数已经求过,直接返回 num[i] = 0; for (int j = 1; j <= n; j++){ if (cost[i][j] != INF && d[i] > d[j]){ num[i] += dfs(j); } } return num[i];}int main(){ int m, a, b, c; while (true){ cin >> n; if (n == 0)break; cin >> m; for (int i = 1; i <= n; i++){ fill(cost[i], cost[i] + MAX, INF); } for (int i = 0; i < m; i++){ cin >> a >> b >> c; if (c < cost[a][b]){ cost[a][b] = cost[b][a] = c; } } dijkstra(2);//从home到每个点的最短距离 fill(num, num + MAX, -1); cout << dfs(1) << endl; } return 0;}

 

转载于:https://www.cnblogs.com/littlehoom/p/3554385.html

你可能感兴趣的文章
mongodb索引
查看>>
UEditor自动调节宽度
查看>>
JAVA做验证码图片(转自CSDN)
查看>>
Delphi TServerSocket,TClientSocket实现传送文件代码
查看>>
JS无聊之作
查看>>
Mac上搭建ELK
查看>>
443 Chapter7.Planning for High Availability in the Enterprise
查看>>
HttpHandler初探 - 页面上输出图像
查看>>
框架和语言的作用
查看>>
unidac连接ORACLE免装客户端驱动
查看>>
Cygwin + OpenSSH FOR Windows的安装配置
查看>>
咏南中间件支持手机客户端
查看>>
fastscript增加三方控件之二
查看>>
Windows Vista RTM 你准备好了么?
查看>>
Tensorflow Serving 模型部署和服务
查看>>
Java Web开发详解——XML+DTD+XML Schema+XSLT+Servlet 3.0+JSP 2.2深入剖析与实例应用
查看>>
topcoder srm 680 div1 -3
查看>>
topcoder srm 430 div1
查看>>
具体数学第二版第四章习题(1)
查看>>
高效前端优化工具--Fiddler入门教程
查看>>