hdu1142-A Walk Through the Forest-最短路+dfs
来源:互联网 发布:飞鸿传书软件下载 编辑:程序博客网 时间:2024/05/16 09:02
下面是spfa+dfs
//56017202012-03-21 20:40:48Accepted114231MS624K1382 BC++zjwzcnjsy#include <cstdio>#include <vector>#include <queue>using namespace std;#define inf 0x3fffffff#define M 1001struct son{ int v,w; son(int tv,int tw):v(tv),w(tw){}};vector<son>g[M];int n,m;int dist[M],inq[M];__int64 ans[M];;void init(){ for(int i=0;i<=n;++i)g[i].clear(),ans[i]=-1;}void spfa(int u){ int i,v,w; for(i=1;i<=n;++i)dist[i]=inf,inq[i]=0; dist[u]=0; queue<int>Q; Q.push(u);inq[u]=1; while(!Q.empty()){ u=Q.front();Q.pop();inq[u]=0; for(i=0;i<g[u].size();++i){ v=g[u][i].v;w=g[u][i].w; if(dist[v]>dist[u]+w){ dist[v]=dist[u]+w; if(!inq[v]){Q.push(v);inq[v]=1;} } } }}__int64 dfs(int i){/*dfs也要优化,不然也会超时*/ if(ans[i]!=-1)return ans[i]; if(i==2)return 1; ans[i]=0; for(int j=0;j<g[i].size();++j) { if(g[i][j].v!=i && dist[g[i][j].v]<dist[i]){ ans[i]+=dfs(g[i][j].v); } } return ans[i];}int main(){ int a,b,d,i; while(scanf("%d",&n)!=EOF&&n){ scanf("%d",&m); init(); for(i=0;i<m;++i){ scanf("%d%d%d",&a,&b,&d); g[a].push_back(son(b,d)); g[b].push_back(son(a,d)); } spfa(2); dfs(1); printf("%I64d\n",ans[1]); } return 0;}
下面是dijkstra+dfs
//56017632012-03-21 20:44:11Accepted114293MS4156K1317 BC++zjwzcnjsy#include <stdio.h>#define inf 0x3fffffff#define M 1001int dist[M],map[M][M],n,m;__int64 ans[M];void init(){ for(int i=1;i<=n;++i){ ans[i]=-1; for(int j=1;j<=n;++j)map[i][j]=(i==j?0:inf); }}void dijkstra(int u){ int v[M],i,j,min,w; for(i=1;i<=n;++i)dist[i]=map[u][i],v[i]=0; dist[u]=0;v[u]=1; for(i=0;i<n;i++){ min=inf; for(j=1;j<=n;++j){ if(!v[j] && dist[j]<min){min=dist[j];w=j;} } v[w]=1; for(j=1;j<=n;++j){ if(!v[j] && dist[j]>dist[w]+map[w][j]){dist[j]=dist[w]+map[w][j];} } }}__int64 dfs(int i){/*dfs也要优化,不然也会超时*/ if(ans[i]!=-1)return ans[i]; if(i==2)return 1; ans[i]=0; for(int j=1;j<=n;++j) { if(i!=j && dist[j]<dist[i] && map[i][j]<inf){ ans[i]+=dfs(j); } } return ans[i];}int main(){ int a,b,d,i; while(scanf("%d",&n)!=EOF&&n){ scanf("%d",&m); init(); for(i=0;i<m;++i){ scanf("%d%d%d",&a,&b,&d); if(map[a][b]>d)map[a][b]=map[b][a]=d; } dijkstra(2); dfs(1); printf("%I64d\n",ans[1]); } return 0;}
- hdu1142-A Walk Through the Forest-最短路+dfs
- A Walk Through the Forest(hdu1142,最短路+dfs)
- hdu1142 A Walk Through the Forest 最短路和记忆式搜索dijstra+dfs
- A Walk Through the Forest(最短路+dfs)
- HDU 1142 A Walk Through the Forest 最短路+DFS
- hdu 1142 A Walk Through the Forest (最短路+dfs )
- HDU1142:A Walk Through the Forest(Dijkstra+dfs)
- A Walk Through the Forest hdu1142 最短路+简单动规
- hdu1142 A Walk Through the Forest
- HDU1142 A Walk Through the Forest
- hdu1142 A Walk Through the Forest
- hdu1142-A Walk Through the Forest
- hdu1142 A Walk Through the Forest
- hdu1142 A Walk Through the Forest
- hdu1142A Walk Through the Forest (最短路+BFS+DFS )
- 【最短路+dfs+dijkstra】杭电 hdu 1142 A Walk Through the Forest
- A Walk Through the Forest 最短路+记忆化搜索DFS
- hdu 1142 A Walk Through the Forest【Dijkstra+dfs求最短路条数】
- jar包 zip和 tgz区别
- 递归实现十进制转二进制
- 关于Android屏幕解锁与持续开亮
- 第五周任务1-1
- 在自己的电脑上安装wordpress
- hdu1142-A Walk Through the Forest-最短路+dfs
- Android 待机流程解析
- Hibernate标识符生成器
- VISUALSVN SERVER下载安装流程 及ca证书制作教程
- 一个静态库框架模板: iOS Universal Framework Mk 7
- BleachBit:通过winapp2.ini来深度清理windows的系统垃圾
- Struts标签中截取内容
- Spring batch ItemReaders and ItemWriters
- 第五周作业任务二