HDU 1142 A Walk Through the Forest (求最短路条数)
A Walk Through the Forest
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1142
Description
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
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
Hint
##题意:
求最短路的条数.
要求这些路径互相没有相同的边.
##题解:
直接标记出最短路中的边,对这些边dfs一次即可.
注意:应该求终点到其他点的最短路,再从起点开始dfs. (或反过来)
若要求各条最短路不共边,则需要用最大流.
[HDU3416-Marriage Match IV](http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q)
题解:http://www.cnblogs.com/Sunshine-tcf/p/5752180.html
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 501000
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n, m;
typedef pair<int,int> pii;
priority_queue<pii,vector,greater > q;
bool vis[maxn];
int edges, u[maxn], v[maxn], w[maxn];
int first[maxn], _next[maxn];
int dis[maxn];
int pre[maxn];
void add_edge(int s, int t, int val) {
u[edges] = s; v[edges] = t; w[edges] = val;
_next[edges] = first[s];
first[s] = edges++;
}
void dijkstra(int s) {
memset(pre, -1, sizeof(pre));
memset(vis, 0, sizeof(vis));
for(int i=1; i<=n; i++) dis[i]=inf; dis[s] = 0;
while(!q.empty()) q.pop();
q.push(make_pair(dis[s], s));
while(!q.empty()) {
pii cur = q.top(); q.pop();
int p = cur.second;
if(vis[p]) continue; vis[p] = 1;
for(int e=first[p]; e!=-1; e=_next[e]) if(dis[v[e]] > dis[p]+w[e]){
dis[v[e]] = dis[p] + w[e];
q.push(make_pair(dis[v[e]], v[e]));
pre[v[e]] = p;
}
}
}
int cnt[maxn];
//必须记忆化dfs:直接dfs-TLE bfs-MLE
int dfs(int s) {
if(s == 2) return 1;
if(cnt[s] != -1) return cnt[s];
int sum = 0;
for(int e=first[s]; e!=-1; e=_next[e]) //if(dis[v[e]]+w[e] == dis[u[e]]) {
if(dis[v[e]] < dis[u[e]]) {
sum += dfs(v[e]);
}
return cnt[s] = sum;
}
int main(void)
{
//IN;
while(scanf("%d", &n) != EOF && n)
{
memset(first, -1, sizeof(first)); edges = 0;
memset(cnt, -1, sizeof(cnt));
cin >> m;
while(m--) {
int u,v,w; scanf("%d %d %d", &u, &v, &w);
add_edge(u, v, w);
add_edge(v, u, w);
}
dijkstra(2);
int ans = dfs(1);
printf("%d\n", ans);
}
return 0;
}
HDU 1142 A Walk Through the Forest (求最短路条数)的更多相关文章
- HDU 1142 A Walk Through the Forest(最短路+记忆化搜索)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU 1142 A Walk Through the Forest (记忆化搜索 最短路)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- hdu 1142 A Walk Through the Forest (最短路径)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- 题解报告:hdu 1142 A Walk Through the Forest
题目链接:acm.hdu.edu.cn/showproblem.php?pid=1142 Problem Description Jimmy experiences a lot of stress a ...
- 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 (Jav ...
- 【解题报告】HDU -1142 A Walk Through the Forest
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 题目大意:Jimmy要从办公室走路回家,办公室在森林的一侧,家在另一侧,他每天要采取不一样的路线 ...
- hdu 1142 A Walk Through the Forest
http://acm.hdu.edu.cn/showproblem.php?pid=1142 这道题是spfa求最短路,然后dfs()求路径数. #include <cstdio> #in ...
- HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)
题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...
- HDU 1142 A Walk Through the Forest(Dijkstra+记忆化搜索)
题意:看样子很多人都把这题目看错了,以为是求最短路的条数.真正的意思是:假设 A和B 是相连的,当前在 A 处, 如果 A 到终点的最短距离大于 B 到终点的最短距离,则可以从 A 通往 B 处,问满 ...
随机推荐
- 使用HttpClient发送HTTPS请求以及配置Tomcat支持SSL
这里使用的是HttpComponents-Client-4.1.2 package com.jadyer.util; import java.io.File; import java.io.FileI ...
- R语言日期时间函数
Sys.Date( ) returns today's date. date() returns the current date and time.# print today's datetoday ...
- Android开发之单例模式
参考:http://blog.csdn.net/guolin_blog/article/details/8860649 http://www.cnblogs.com/liyiran/p/5283690 ...
- 替代Eval的两种方式
在asp.net中的数据绑定中,我们经常会用到Eval,不过大家都知道Eval绑定是通过反射来实现的, 而反射势必会对性能造成一定的影响.不过有两种替代的方式来实现绑定数据,对性能略有提高. 1 当数 ...
- Android中ProgressDialog的简单示例
网上一般对进度条的示例都是如何显示,没有在任务结束如何关闭的文章,参考其他文章经过试验之后把整套进度条显示的简单示例如下: 建立android工程等工作都略去,Google一下就可以了. 下面来介绍主 ...
- SPRING STS Virgo OSGI 开发一 : bundle 项目的创建
1. Spring STS 下载地址 (spring 最近改了站点 暂时不是太熟悉) http://spring.io/tools/sts/all 2. 下载 Virgo 插件 htt ...
- bzoj4177: Mike的农场
类似于最大权闭合图的思想. #include<cstdio> #include<cstring> #include<iostream> #include<al ...
- PHP适合做大型网站吗?
1. 对递归的不良支持 递归是一种函数调用自身的机制.这是一种强大的特性可以把某些复杂的东西变得很简单.有一个使用递归的例子是快速排序(quicksort).不幸的是,PHP并不擅长递归.Zeev,一 ...
- 解析CSS加密技术之“障眼法”
CSS(Cascading Style Sheet,可译为“层叠样式表”或“级联样式表”)是一组格式设置规则,用于控制Web页面的外观.通过使用CSS样式设置页面的格式,可将页面的内容与表现形式分离. ...
- 64位SqlServer通过链接服务器与32位oracle通讯
在SQL SERVER里只安装了32位的oracle客户端的情况下,添加链接服务器然后执行查询会报如下信息: 原因:在64位的SQL Engine中已经不提供MSDAORA 的驱动了,可以使用Ora ...