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. 给vs2015添加EF

    今天做EF的小例子时,发现需要添加实体数据模型,但是不管怎么找在新建项中都找不到这个选项,这是怎么回事,于是就开始百度吧,有的说可能是VS安装时没有全选,也有的人说可能是重装VS时,没有将注册表清除, ...

  2. 认识浏览器请求头User-Agent

    一.定义 User Agent中文名为用户代理,是Http协议中的一部分,属于头域的组成部分,User Agent也简称UA. 它是一个特殊字符串头,是一种向访问网站提供你所使用的浏览器类型及版本.操 ...

  3. MySQL备份与恢复之percona-xtrabackup实现增量备份及恢复 实例

    innobackupex 的使用方法1.完全备份 参数一是完全备份地址 完全备份到/data/mysql/back_up/all_testdb_20140612 目录下innobackupex --u ...

  4. Python机器学习笔记 使用scikit-learn工具进行PCA降维

    之前总结过关于PCA的知识:深入学习主成分分析(PCA)算法原理.这里打算再写一篇笔记,总结一下如何使用scikit-learn工具来进行PCA降维. 在数据处理中,经常会遇到特征维度比样本数量多得多 ...

  5. XML——对XML文档的创建与增删改查

    一.创建的第一种方式  //1.创建一个XML文档 XmlDocument doc = new XmlDocument(); //2.创建第一行描述信息 XmlDeclaration dec = do ...

  6. SyntaxHighlighter 代码高亮极简单配置

    页首Html代码: <!--<link type="text/css" rel="stylesheet" href="https://bl ...

  7. C# 跨进程 设置窗口owner

    窗口间跨进程通信 1. 发送方 public const int WM_InsertChart_Completed = 0x00AA; //查找窗口 [DllImport("User32.d ...

  8. Predicate--入门简介

    说明:表示定义一组条件并确定指定对象是否符合这些条件的方法.此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素. var predicate = new Predicate&l ...

  9. input框限制只能输入正整数、字母、小数、

    这篇博文大部分来自于网上,为了方便自己查阅,以及帮助他人.   1,只能输入正整数 <input onkeyup="if(this.value.length==1){this.valu ...

  10. 深度 | AI芯片终极之战

    深度 | AI芯片终极之战 https://mp.weixin.qq.com/s?__biz=MzA4MTQ4NjQzMw==&mid=2652712307&idx=1&sn= ...