hdu 1142 A Walk Through the Forest(spfa+深搜)
来源:互联网 发布:兰州知豆招聘信息 编辑:程序博客网 时间:2024/06/10 09:48
http://blog.acmj1991.com/?p=785
题意:a->b的条件是:a到终点的最短距离比b到终点的最短距离要大,问起点到终点有多少条路能走?
思路:用spfa求出终点为源点到其他点的最短距离,然后依据题目条件进行深搜#include<stdio.h>#include<string.h>#include<iostream>using namespace std;#define maxN 1010#define inf 0x0fffffffint num,head[maxN],vist[maxN],dis[maxN],dp[maxN];struct node{int v,w,next;}po[maxN*maxN];void init(){num=0;memset(head,-1,sizeof(head));memset(vist,0,sizeof(vist));memset(dp,-1,sizeof(dp));}void add(int u,int v,int w){po[num].v=v,po[num].w=w;po[num].next=head[u],head[u]=num++;po[num].v=u,po[num].w=w;po[num].next=head[v],head[v]=num++;}void spfa(int n,int s){for(int i=0;i<=n;i++)dis[i]=inf;dis[s]=0,vist[s]=1;int stack[200*maxN],hea=0,tail=0;stack[tail++]=s;while(hea!=tail){int u=stack[hea++];vist[u]=0;for(int i=head[u];i!=-1;i=po[i].next){int v=po[i].v;if(dis[u]+po[i].w<dis[v]){dis[v]=dis[u]+po[i].w;if(!vist[v]){vist[v]=1;stack[tail++]=v;}}}}}int dfs(int u){if(u==2)return 1;if(dp[u]!=-1)return dp[u];else dp[u]=0; for(int i=head[u];i!=-1;i=po[i].next){int v=po[i].v;if(dis[v]<dis[u]&&dis[v]<=dis[1]){dp[u]+=dfs(v);}}return dp[u];}int main(){int n,m;while(scanf("%d",&n)&&n){int u,v,w;init();scanf("%d",&m);while(m--){scanf("%d%d%d",&u,&v,&w);add(u,v,w);}spfa(n,2);dfs(1);printf("%d\n",dp[1]);}}
- hdu 1142 A Walk Through the Forest(spfa+深搜)
- hdu 1142 A Walk Through the Forest spfa
- hdu 1142 A Walk Through the Forest(spfa求最短路+记忆化搜索)
- HDU 1142 A Walk Through the Forest (SPFA+记忆化DFS)
- HDU 1142 A Walk Through the Forest
- A Walk Through the Forest HDU 1142
- hdu 1142 A Walk Through the Forest
- Hdu-1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- ORACLE和SQL语法区别归纳(2)
- Mysql无法创建文件/写入文件错误的解决
- 忘记敏捷
- 编程之美读书笔记3.7队列中取最大值操作的问题 解法2
- 最大公共连续子串(LCS问题)
- hdu 1142 A Walk Through the Forest(spfa+深搜)
- 利用读写锁和智能指针来实现多线程下对资源的多个地方读和多个地方写的实现。
- Object-c学习笔记十六-----文件加载与保存
- Henry拣钱(money.pas/c/cpp)
- 一步一步写算法(之非递归排序)
- Linux C之atio()函数
- iPhone中的UIActionSheet与UIAlterView
- java web开发一个帐号同一时间只能一个人登录
- Creating EXT2 USB Flash Drive from Windows