Problem    UVA - 10917 - Walk Through the Forest

Time Limit: 3000 mSec

Problem 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 < 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>其实就是满足式子d[B] < d[A],因此跑一边最短路之后,可行路径就出来了,显然只保留可行路径的图是DAG,有向是肯定的,无环也很好理解,对于环上的节点,按照顺时针(或者逆时针)的顺序始终满足上述不等式,绕一圈之后会出现d[s] < d[s],这样的矛盾不等式,所以无环,DAG上统计路径就很简单了,记忆化搜索呗。

 #include <bits/stdc++.h>

 using namespace std;

 #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0); struct Edge
{
int to, next, w;
} edge[maxm]; struct HeapNode
{
int dis, u;
bool operator<(const HeapNode &a) const
{
return dis > a.dis;
}
}; int tot, head[maxn]; void AddEdge(int u, int v, int w)
{
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].w = w;
head[u] = tot++;
} int st, en, n, m;
int dist[maxn];
bool vis[maxn]; void Dijkstra()
{
for (int i = ; i <= n; i++)
{
dist[i] = INF;
vis[i] = false;
}
dist[en] = ;
priority_queue<HeapNode> que;
que.push((HeapNode){, en});
while (!que.empty())
{
HeapNode x = que.top();
que.pop();
if (vis[x.u])
continue;
int u = x.u;
vis[u] = true;
for (int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if (dist[v] > dist[u] + edge[i].w)
{
dist[v] = dist[u] + edge[i].w;
que.push((HeapNode){dist[v], v});
}
}
}
} int dp[maxn]; int dfs(int u)
{
if (u == en)
return 1LL;
if (dp[u] != -)
{
return dp[u];
}
int &ans = dp[u];
ans = ;
for (int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if (dist[v] < dist[u])
{
ans += dfs(v);
}
}
return ans;
} void init()
{
for (int i = ; i <= n; i++)
{
head[i] = -;
}
tot = ;
} int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
st = , en = ;
while (~scanf("%d", &n) && n)
{
scanf("%d", &m);
init();
int u, v, w;
for (int i = ; i < m; i++)
{
scanf("%d%d%d", &u, &v, &w);
u--, v--;
AddEdge(u, v, w);
AddEdge(v, u, w);
}
Dijkstra();
memset(dp, -, sizeof(dp));
printf("%d\n", dfs(st));
}
return ;
}

UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)的更多相关文章

  1. A Walk Through the Forest (最短路+记忆化搜索)

    Jimmy experiences a lot of stress at work these days, especially since his accident made working dif ...

  2. HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)

    题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...

  3. UVa10917 A Walk Through the Forest(SPFA+记忆化搜索)

    题目给一张有向图,问从起点1到终点2沿着合法的路走有种走法,合法的路指从u到v的路,v到终点的距离严格小于u到终点的距离. 先SPFA预处理出所有合法的路,然后这些路肯定形成一个DAG,然后DP一下就 ...

  4. UVA 10917 Walk Through the Forest(dijkstra+DAG上的dp)

    用新模板阿姨了一天,换成原来的一遍就ac了= = 题意很重要..最关键的一句话是说:若走A->B这条边,必然是d[B]<d[A],d[]数组保存的是各点到终点的最短路. 所以先做dij,由 ...

  5. uva 10917 Walk Through The Forest

    题意: 一个人从公司回家,他可以从A走到B如果从存在从B出发到家的一条路径的长度小于任何一条从A出发到家的路径的长度. 问这样的路径有多少条. 思路: 题意并不好理解,存在从B出发到家的一条路径的长度 ...

  6. 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 ...

  7. Luogu P3953 逛公园(最短路+记忆化搜索)

    P3953 逛公园 题面 题目描述 策策同学特别喜欢逛公园.公园可以看成一张 \(N\) 个点 \(M\) 条边构成的有向图,且没有自环和重边.其中 \(1\) 号点是公园的入口,\(N\) 号点是公 ...

  8. Luogu P2149 [SDOI2009]Elaxia的路线(最短路+记忆化搜索)

    P2149 [SDOI2009]Elaxia的路线 题意 题目描述 最近,\(Elaxia\)和\(w**\)的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们必须合理地安排两个人在一起的 ...

  9. UVA 10917 Walk Through the Forest SPFA

    uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= ...

随机推荐

  1. JAVA集合类兄妹List和Set

    List 接口及其实现类 有序集合,集合中每个元素都有其对应的顺序索引,类似数组,索引也是从 0 开始,可以根据元素的索引,来访问元素. List 集合允许添加相同的元素,因为它是通过下标来取值的,不 ...

  2. JavaScript面向对象--封装

    一.封装的概念 面向对象的类包括两大成员,一种是暴露给外部的接口,另一种是只在类内部才能访问的私有属性.在这个类被实例化成对象后,用户只能通过操作给定的接口来访问该类内部的私有属性,这就被称为面向对象 ...

  3. Asp.Net Core 程序部署到Linux(centos)生产环境(一):普通部署

    运行环境 照例,先亮底 centos:7.2 cpu:1核 2G内存 1M带宽 辅助工具:xshell xftp 搭建.net core运行环境 .net core 的运行环境我单独写了一篇,请看我的 ...

  4. 前后端数据加密传输 RSA非对称加密

    任务需求:要求登陆时将密码加密之后再进行传输到后端. 经过半天查询摸索折腾,于是有了如下成果: 加密方式:RSA非对称加密.实现方式:公钥加密,私钥解密.研究进度:javascript与java端皆已 ...

  5. Spring Cloud Alibaba基础教程:Nacos配置的多环境管理

    前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...

  6. Java开发笔记(五十五)关键字static的用法

    前面介绍嵌套类的时候讲到了关键字static,用static修饰类,该类就变成了嵌套类.从嵌套类的用法可知,其它地方访问嵌套类之时,无需动态创建外层类的实例,直接创建嵌套类的实例就行.其实static ...

  7. Java开发笔记(六十一)Lambda表达式

    前面介绍了匿名内部类的简单用法,通过在sort方法中运用匿名内部类,不但能够简化代码数量,还能保持业务代码的连续性.只是匿名内部类的结构仍显啰嗦,虽然它省去了内部类的名称,但是花括号里面的方法定义代码 ...

  8. springmvc 文件上传(粘贴即用)

    这里记录下,方便以后复制粘贴. maven配置 <dependency> <groupId>commons-fileupload</groupId> <art ...

  9. js正则表达式 数字和小数点 非负数 保留两位小数点

    验证数字非负数  小数点保留两位小数点 下面正则已验证通过 /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/

  10. GNOME图形界面的基本操作

    成功登录进入CentOS系统之后,我们首先看到的桌面就是GNOME图形界面,下面来看一下相关的基本操作. 个性化设置 1,设置屏幕分辨率 进入菜单 2,更换桌面背景 进入下面菜单. 选择一张背景图片, ...