hdu-1142-A Walk Through the Forest
来源:互联网 发布:李钟硕李敏镐网络剧 编辑:程序博客网 时间:2024/06/03 20:42
就是从1到2之间,下一点到2的距离比当前点到2的距离要短,问这样的路径有多少条。
最短路径+记忆化搜索解决
#include<iostream>#include<cstring>using namespace std;const int MAX=1001;const int INF=99999999;int m,n;int map[MAX][MAX];int dis[MAX];int use[MAX];void Init(){ memset(use,0,sizeof(use)); for(int i=1; i<MAX; i++) for(int j=1; j<MAX; j++) map[i][j]=map[j][i]=INF;}void dijkstra(int s){ bool vis[MAX]; memset(vis,false,sizeof(vis)); int pos=s; vis[pos]=true; dis[pos]=0; for(int i=1; i<=n; i++) if(i!=pos) dis[i]=map[pos][i]; for(int i=1; i<n; i++) { int min=INF; for(int j=1; j<=n; j++) { if(!vis[j]&&dis[j]<min) { min=dis[j]; pos=j; } } vis[pos]=true; for(int k=1; k<=n; k++) { if(!vis[k]&&dis[k]>dis[pos]+map[pos][k]) dis[k]=dis[pos]+map[pos][k]; } }}int dfs(int x){ int sum=0; if(x==2) return 1; if(use[x]) return use[x]; for(int i=1; i<=n; i++) { if(map[x][i]!=INF&&dis[x]>dis[i]&&x!=i) { sum+=dfs(i); } } use[x]=sum; return use[x];}int main(){ int a,b,time; while(cin>>n&&n!=0) { cin>>m; Init(); for(int i=1; i<=m; i++) { cin>>a>>b>>time; if(map[a][b]>time||map[b][a]>time) map[a][b]=map[b][a]=time; } dijkstra(2); cout<<dfs(1)<<endl; }}
1 0
- 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
- 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
- io口的作用
- Linux源码包编译和安装
- 黑马程序员——集合(中)——TreeSet、泛型
- sqoop基本 操作
- Oracle学习笔记
- hdu-1142-A Walk Through the Forest
- call和apply的深入理解
- IO口的作用
- 一段文字中的几个关键字显示高亮
- 黑马程序员——JAVA——多线程
- linux 查看端口情况
- js为字符串编码
- Spring与Mybatis的整合过程中遇到的一些问题
- 上拉电阻和下拉电阻