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. 【转】ubuntu中的Wine详解

    原文网址:http://blog.csdn.net/iwtwiioi/article/details/10530561 什么是wine?(转自百度百科,具体看百科) wine,是一款优秀的Linux系 ...

  2. libjingle开发人员指南

    libjingle开发人员指南 libjingle SDK包含C + +源代码和文档,使您能够设计连接和网络交换数据的应用程序(通过点对点数据连接).该SDK包含代码和示例应用程序,一个Window平 ...

  3. bzoj4002

    http://www.lydsy.com/JudgeOnline/problem.php?id=4002 好吧,完全不会做,在考场只能爆零. 膜拜PoPoQQQ大神 #include<cstdi ...

  4. hdu4622-Reincarnation(后缀自动机)

    Problem Description Now you are back,and have a task to do:Given you a string s consist of lower-cas ...

  5. JVM运行时内存结构

    原文转载自:http://my.oschina.net/sunchp/blog/369707 1.JVM内存模型 JVM运行时内存=共享内存区+线程内存区 1).共享内存区 共享内存区=持久带+堆 持 ...

  6. java 字符串为空问题

    java 字符串为空问题 String testStr = null; System.out.println(testStr); if (testStr == null) { System.out.p ...

  7. 对easyui datagrid进行扩展,当滚动条拉直最下面就异步加载数据。

    以下方法是通用的,只要把datagrid定义为全局的即可,其他部分的代码不用进行修改! 可以把以下代码放入到一个单独的js文件,然后再需要的页面引入即可! $(function(){ try{ $(& ...

  8. Zedboard甲诊opencv图像处理(三)

    整个工程进展到这一步也算是不容易吧,但技术含量也不怎么高,中间乱起八糟的错误太烦人了,不管怎么样,现在面临了最大的困难吧,图像处理算法.算法确实不好弄啊,虽然以前整过,但都不是针对图像的. 现在的图像 ...

  9. 一张图解析如何让img垂直居中对齐

    测试代码: <!DOCTYPE html> <html> <head> <style> .dd { background-color: gray; po ...

  10. word-wrap 和 word-break

    一.word-wrap 1.浏览器支持 所有主流浏览器都支持 word-wrap属性 2.定义和用法 word-wrap 属性允许长单词或 URL 地址换行到下一行. 语法 word-wrap: no ...