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.

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

题意:给出无向图,问从1到2有多少种走法,满足每一步到的点的最短路递减。

思路:反向求最短路,然后就是一个拓扑图,然后拓扑就好了(貌似DFS也能过,毕竟是单源起点)。

如果要拓扑怎么搞呢,如果我不管1的入度是否为0,一开始就把1点放进去,就不太好搞。  事实上,还是应该像普通的拓扑那样写,只是除了1号点初始值为1,其他都为0;   是我傻逼了。。。。下周出给学弟们,看看有人和我一样的没。

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=;
int Laxt[maxn],Next[maxn*maxn],To[maxn*maxn],ind[maxn],N,vis[maxn];
int dp[maxn],Len[maxn*maxn],From[maxn*maxn],cnt,dis[maxn];
struct in{
int dis,id;
friend bool operator<(in w,in v){ return w.dis>v.dis; }
};
vector<int>G[maxn];
void add(int u,int v,int w){
Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v;
Len[cnt]=w; From[cnt]=u;
}
void SPFA()
{
for(int i=;i<=N;i++) dis[i]=inf; dis[]=;
priority_queue<in>q; q.push(in{,});
while(!q.empty()){
int u=q.top().id; q.pop();
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(dis[u]+Len[i]<dis[v]){
dis[v]=dis[u]+Len[i]; q.push(in{dis[v],v});
}
}
}
}
int main() {
int M,u,v,w;
while(~scanf("%d",&N)){
if(N==) break; cnt=; scanf("%d",&M);
for(int i=;i<=N;i++) Laxt[i]=dp[i]=ind[i]=vis[i]=,G[i].clear();
for(int i=;i<=M;i++){
scanf("%d%d%d",&u,&v,&w);
add(v,u,w); add(u,v,w);
}
SPFA();
for(int i=;i<=cnt;i++){
if(dis[To[i]]>dis[From[i]]){
G[To[i]].push_back(From[i]);
ind[From[i]]++;
}
}
queue<int>q; dp[]=;
for(int i=;i<=N;i++) if(ind[i]==) q.push(i),vis[i]=;
while(!q.empty()){
int u=q.front(); q.pop();
for(int i=;i<G[u].size();i++){
int v=G[u][i]; if(vis[v]) continue;
dp[v]+=dp[u]; ind[v]--;
if(ind[v]==&&vis[v]==){ q.push(v); vis[v]=;}
}
}
printf("%d\n",dp[]);
}
return ;
}

HDU - 1142:A Walk Through the Forest (拓扑排序)的更多相关文章

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

  2. HDU 1142 A Walk Through the Forest (求最短路条数)

    A Walk Through the Forest 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 Description Jimmy exp ...

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

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

  5. 题解报告:hdu 1142 A Walk Through the Forest

    题目链接:acm.hdu.edu.cn/showproblem.php?pid=1142 Problem Description Jimmy experiences a lot of stress a ...

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

  7. 【解题报告】HDU -1142 A Walk Through the Forest

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 题目大意:Jimmy要从办公室走路回家,办公室在森林的一侧,家在另一侧,他每天要采取不一样的路线 ...

  8. hdu 1142 A Walk Through the Forest

    http://acm.hdu.edu.cn/showproblem.php?pid=1142 这道题是spfa求最短路,然后dfs()求路径数. #include <cstdio> #in ...

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

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

  10. HDU 1142 A Walk Through the Forest(dijkstra+记忆化DFS)

    题意: 给你一个图,找最短路.但是有个非一般的的条件:如果a,b之间有路,且你选择要走这条路,那么必须保证a到终点的所有路都小于b到终点的一条路.问满足这样的路径条数 有多少,噶呜~~题意是搜了解题报 ...

随机推荐

  1. nlp基本知识点(不断更新)

    1.精确率与召回率 召回率也称为查全率,指的是:你查询到的相关数目/(你查询到的相关的数目+你没有查询到的相关的数目). 精确率: A/A+B 召回率:A/A+C 再比如: 我 是 中国人 这里正确的 ...

  2. spring的静态代理和动态代理

    Java静态代理 Jdk动态代理 java代理模式 即Proxy Pattern,23种java常用设计模式之一.代理模式的定义:对其他对象提供一种代理以控制对这个对象的访问. 原理: 代理模式的主要 ...

  3. 代码演示神器——jsfiddle

    目录: 1. 介绍 2. jsfiddle的具体使用 3. 总结 1. 介绍 很多时候,我们需要在我们写的文章或博客中,即时显示出我们写的demo,能方便的解释出我们的思路.很久之前我也写过一篇文章, ...

  4. ubuntu14.04安装hadoop2.6.0(伪分布模式)

    版本:虚拟机下安装的ubuntu14.04(64位),hadoop-2.6.0 下面是hadoop2.6.0的官方英文教程: http://hadoop.apache.org/docs/r2.6.0/ ...

  5. 关于hugepages 3.txt

    关于hugepages 3.txt --//有一段时间我一直强调安装oracle一定要配置hugepage,因为现在的服务器内存越来越大,如果还使用4K的页面表,如果内存表占用内存巨大, --//特别 ...

  6. 【BZOJ1061】【NOI2008】志愿者招募

    [BZOJ1061][NOI2008]志愿者招募 题面 BZOJ 题解 我们设每类志愿者分别招募了\(B[i]\)个 那么,我们可以得到一系列的方程 \[\sum_{S[i]\leq x\leq T[ ...

  7. 解决SecureCRT下spark-shell中scala无法删除问题

    转自:http://blog.csdn.net/huanbia/article/details/51318278 问题描述 当使用SecureCRT来打开Spark-shell的时候,有时会出现如下问 ...

  8. Hibernate[延迟加载] [三种状态] [脏检查] [缓存机制]

    一.持久化对象的唯一标识 java中按内存地址不同区分同一个类的不同对象,关系数据库用主键区分同一条记录,Hibernate使用OID来建立内存中的对象和数据库中记录的对应关系 什么是OID? 解析: ...

  9. pip 使用总结

    pip的安装: Windows Python2.7 以上的版本均自带pip,安装的时候记得勾选对应的选项即可. 安装easy_install, 通过easy_install pip 下载[easy_s ...

  10. TCP的time_wait、close_wait状态

    转载:http://huoding.com/2013/12/31/316  http://blog.csdn.net/lxnkobe/article/details/7525317  http://k ...