最短路径spaf算法
来源:互联网 发布:申万宏源交易软件下载 编辑:程序博客网 时间:2024/06/08 05:19
以hdu2544为例
#include <iostream>#include <stdio.h>#include <queue>#include <string.h>using namespace std;int a[123][123],d[123];bool vis1[123][123],vis2[123];void init(){ memset(vis1,false,sizeof(vis1)); memset(vis2,false,sizeof(vis2)); for(int i=1;i<=123;i++){ for(int j=1;j<=123;j++){ if(i==j) a[i][j]=0; else a[i][j]=100*10000; } } for(int i=1;i<=123;i++){ d[i]=100*10000; }}int main(){ int n,m; while(scanf("%d %d",&n,&m),(n||m)){ init(); queue<int> que; while(!que.empty()) que.pop(); for(int i=1;i<=m;i++){ int u,v,w; scanf("%d %d %d",&u,&v,&w); a[u][v]=a[v][u]=w; vis1[u][v]=vis1[v][u]=true; } que.push(1); vis2[1]=1; d[1]=0; while(!que.empty()){ int temp=que.front(); que.pop(); vis2[temp]=0; for(int i=1;i<=n;i++){ if(vis1[temp][i]){ if(d[i]>d[temp]+a[temp][i]){ d[i]=d[temp]+a[temp][i]; if(!vis2[i]){ vis2[i]=1; que.push(i); } } } } } printf("%d\n",d[n]); } return 0;}
阅读全文
0 0
- 最短路径spaf算法
- hdu2544-最短路径spaf
- spaf算法求最短路径
- 最短路径算法
- 最短路径算法
- 最短路径算法
- 最短路径算法
- 最短路径算法
- 最短路径算法
- 最短路径算法
- 最短路径算法
- 最短路径算法
- 最短路径算法
- 最短路径算法
- 最短路径算法
- 最短路径算法
- 最短路径算法
- 最短路径算法
- linux下高并发服务器实现
- 替换空格
- 关于单链表的基本操作
- 18通用属性
- Eclipse 格式化快捷键
- 最短路径spaf算法
- 19第一部分小结
- Problem 5 Smallest multiple
- ansysworkbench直线和曲线路径设置
- python queue
- codeforces 830B. Cards Sorting
- POJ 3135 Polygons on the Grid 笔记
- HDU 1029 Ignatius and the Princess IV (思路)
- 20header和footer