http://acm.hdu.edu.cn/showproblem.php?pid=1142

这道题是spfa求最短路,然后dfs()求路径数。

 #include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define maxn 1001
using namespace std;
const int inf=<<; int g[maxn][maxn];
int dis[maxn];
bool vis[maxn],visi[maxn];
int n,m,a,b,d;
int di;
int cnt[maxn];
int num[maxn]; bool spfa()
{
queue<int>q;
memset(vis,false,sizeof(vis));
memset(cnt,,sizeof(cnt));
for(int i=; i<=n; i++) dis[i]=inf;
dis[]=;
vis[]=true;
q.push();
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=; i<=n; i++)
{
if(dis[i]>dis[u]+g[u][i]&&g[u][i]!=inf)
{
dis[i]=dis[u]+g[u][i];
if((++cnt[i])>n) return false;
if(!vis[i])
{
q.push(i);
vis[i]=true;
}
}
}
}
return true;
} void dfs(int src)
{
if(src==)
{
num[]=;
return ;
}
int sum=;
for(int i=; i<=n; i++)
{
if(g[src][i]!=inf&&dis[src]>dis[i])
{
if(num[i]>=)
{
sum+=num[i];
}
else
{
dfs(i);
sum+=num[i];
}
}
}
num[src]=sum;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==) break;
scanf("%d",&m);
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(i==j) g[i][j]=;
else g[i][j]=inf;
}
}
for(int i=; i<m; i++)
{
scanf("%d%d%d",&a,&b,&d);
g[a][b]=g[b][a]=min(g[a][b],d);
}
spfa();
//printf("%d\n",dis[1]);
for(int i=; i<=n; i++)
{
num[i]=-;
}
dfs();
printf("%d\n",num[]);
}
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(SPFA+记忆化搜索DFS)

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

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

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

随机推荐

  1. Powershell使用管道

    管道并不是什么新事物,以前的Cmd控制台也有重定向的命令,例如Dir | More可以将结果分屏显示.传统的Cmd管道是基于文本的,但是Powershell是基于对象. PS> ls | Sor ...

  2. Some General concepts in ISO C

    [ISO C11 Clause 3]对象(object):执行环境中数据存储的一块区域,它的内容可以用来表示值.-注释:对象可以具有特定的类型.--值(value):确定类型的对象的内容的确切含义.- ...

  3. NOI2011 NOI嘉年华

    http://www.lydsy.com/JudgeOnline/problem.php?id=2436 首先离散化,离散化后时间范围为[1,cnt]. 求出H[i][j],表示时间范围在[i,j]的 ...

  4. java实现矩阵连乘的动态规划

    package com.cjs.algorithm; public class DynamicPlan { /** * 此方法用来求解矩阵连乘的最小数乘次数 * * @param p * 传入的要连乘 ...

  5. phpcms:六、频道页(category.html)

    1.当前栏目的ID:{$catid}标题样式:{title_style($v[style])}(在添加内容或编辑内容的时候,标题右边 有一个选择颜色的块).{str_cut(strip_tags($v ...

  6. hdu 1695 GCD(欧拉函数+容斥)

    Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD( ...

  7. html5 app图片预加载

    function Laimgload(){} //图片预加载JS Laimgload.prototype.winHeight = function(){ //计算页面高度 var winHeight ...

  8. PHP本地域名解析教程

    1.找到C:\WINDOWS\system32\drivers\etc\hosts 127.0.0.1       localhost 127.0.0.1       www.zhosoft.com ...

  9. openwrt上网配置的一些理解

    其实已经有很多帖子讲过openwrt路由器上网配置了,我这里主要是讲我自己的一块硬件路由使用openwrt后的一些上网配置.之所以要研究我自己的配置,是因为硬件,硬件不一样,配置也就不一样,但是总的原 ...

  10. python网络编程之socket

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...