求出家到其他点的最短路径,题目的条件变成了u->v不是回头路等价于d[u]>d[v]。

然后根据这个条件建DAG图,跑dp统计方案数,dp[u] = sum(dp[v])。

#include<bits/stdc++.h>
using namespace std; const int maxn = , maxm = ;
struct Edge
{
int v,w,nxt;
}; #define PB push_back
vector<Edge> edges;
vector<int> G[maxn];
int head[maxn];
int d[maxn]; void addEdge(int u,int v,int w)
{
edges.PB({v,w,head[u]});
head[u] = edges.size()-;
} void init()
{
memset(head,-,sizeof(head));
edges.clear();
} typedef pair<int,int> Node;
#define fi first
#define se second
void dijkstra(int s = )
{
memset(d,0x3f,sizeof(d));
priority_queue<Node,vector<Node>,greater<Node> > q;
q.push(Node(d[s] = ,s));
while(q.size()){
Node x = q.top(); q.pop();
int u = x.se;
if(x.fi != d[u]) continue;
for(int i = head[u]; ~i ; i = edges[i].nxt){
Edge &e = edges[i];
if(d[e.v] > d[u]+e.w){
d[e.v] = d[u]+e.w;
q.push(Node(d[e.v],e.v));
}
}
}
} int dp[maxn];
int dfs(int u)
{
int &ans = dp[u];
if(~ans) return ans;
if(u == ) return ans = ;
ans = ;
for(int i = ; i < (int)G[u].size(); i++){
ans += dfs(G[u][i]);
}
return ans;
} void rebuild(int n)
{
for(int i = ; i < n; i++) G[i].clear();
for(int u = ; u < n; u++){
for(int i = head[u]; ~i; i = edges[i].nxt){
int v = edges[i].v;
if(d[v] < d[u]) G[u].PB(v);
}
}
} int main()
{
//freopen("in.txt","r",stdin);
int n,m;
while(scanf("%d%d",&n,&m),n){
init();
while(m--){
int u,v,w; scanf("%d%d%d",&u,&v,&w); u--;v--;
addEdge(u,v,w); addEdge(v,u,w);
}
dijkstra();
rebuild(n);
memset(dp,-,sizeof(dp));
printf("%d\n",dfs());
}
return ;
}

UVA10917 A walk trough the Forest (最短路,dp)的更多相关文章

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

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

  2. 【uva10917】Walk Through the Forest (最短路)

    题目: gbn最近打算穿过一个森林,但是他比较傲娇,于是他决定只走一些特殊的道路,他打算只沿着满足如下条件的(A,B)道路走:存在一条从B出发回家的路,比所有从A出发回家的路径都短.你的任务是计算一共 ...

  3. UVA-10917 Walk Through the Forest (dijkstra+DP)

    题目大意:n个点,m条边的无向图.一个人从起点到终点按照下面的走法:从A走向B当A到终点的最小距离比B到终点的最小距离大时.问从起点到终点有多少路径方案. 题目分析:先用dijkstra预处理出终点到 ...

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

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

  5. UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)

    Problem    UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...

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

  8. hduoj----1142A Walk Through the Forest(记忆化搜索+最短路)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

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

随机推荐

  1. Asset Catalog Help (五)---Migrating an iOS App Icon Set or Launch Image Set

    Migrating an iOS App Icon Set or Launch Image Set Simplify image management by moving existing app i ...

  2. 3.17-3.18 HDFS2.x中高级特性讲解

    一.hdfs federation hdfs federation即hdfs的联邦:可以理解为有多个namenode节点的hdfs集群: HA方案解决的是单点故障问题,而Fdederation解决的是 ...

  3. myeclipse 重新关联项目和svn

    有时候重装了svn或重新定义了WorkSpaces,原项目和svn没关联了 那么 右击要提交的项目 在弹出的菜单依次:Team -->share project 在弹出的对话框里填入SVN的地址 ...

  4. c# 调用 webservices (转载)

    .net 调用webservice 总结 最近做一个项目,由于是在别人框架里开发app,导致了很多限制,其中一个就是不能直接引用webservice . 我们都知道,调用webserivice 最简单 ...

  5. 2019ICPC西安邀请赛 - B. Product - 数论

    打印的时候麻烦把:https://blog.csdn.net/skywalkert/article/details/50500009这个打印下来. 求\(\prod\limits_{i=1}^{n} ...

  6. 将字符串中的字符按Z字形排列,按行输出

    示例1: Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR" 示例2: Pyth ...

  7. Unity 5.4 公开测试版发布:增强的视觉效果,更佳的性能表现

    为用户提供可靠稳定的产品是我们的一贯使命,现在我们将发布Unity 5.4 beta版本,提供所有的用户公开测试,这包含了Unity Personal Edition版本用户.我们非常希望大家下载并尝 ...

  8. 【Unity3D】3D角色换装++ Advance

    http://www.cnblogs.com/dosomething/archive/2012/12/15/2818897.html 本文在之前的文章Unity3D角色换装的原理 基础上做一个补充 给 ...

  9. 黑马Stream流学习 Stream流 函数式接口 Lambda表达式 方法引用

  10. CentOS7 adb

    https://blog.csdn.net/u012700515/article/details/79021320