hdu 2544 hdu 1874 poj 2387 Dijkstra 模板题
2015-06-18 19:21
127 查看
hdu 2544
求点1到点n的最短路 无向图
Sample Input
2 1 //结点数 边数
1 2 3 //u v w
3 3
1 2 5
2 3 5
3 1 2
0 0
Sample Output
3
2
堆优化Dijstra模板
# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # define LL long long using namespace std ; const int MAXN=1100; const int INF=0x3f3f3f3f; int n ; bool vis[MAXN]; int cost[MAXN][MAXN] ; int lowcost[MAXN] ; int pre[MAXN]; void Dijkstra(int beg) { for(int i=0;i<n;i++) { lowcost[i]=INF;vis[i]=false;pre[i]=-1; } lowcost[beg]=0; for(int j=0;j<n;j++) { int k=-1; int Min=INF; for(int i=0;i<n;i++) if(!vis[i]&&lowcost[i]<Min) { Min=lowcost[i]; k=i; } if(k==-1) break ; vis[k]=true; for(int i=0;i<n;i++) if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i]) { lowcost[i]=lowcost[k]+cost[k][i]; pre[i]=k; } } } int main () { // freopen("in.txt","r",stdin) ; int m ; while (scanf("%d %d" , &m , &n) !=EOF) { int u , v , w ; int i , j ; for (i = 0 ; i < n ; i++) for (j = 0 ; j < n ; j++) { if (i == j) cost[i][j] = 0 ; else cost[i][j] = INF ; } while(m--) { scanf("%d%d%d" , &u , &v , &w) ; if (w < cost[u-1][v-1]) //防止重边 { cost[u-1][v-1] = w ; cost[v-1][u-1] = w ; } } Dijkstra(n-1) ; if (lowcost[0] != INF) printf("%d\n" , lowcost[0]) ; } return 0 ; }View Code
相关文章推荐
- 操作系统的内存分配问题
- 了解一点大数据(一)
- 手机新闻网站,掌上移动新闻,手机报client,jQuery Mobile手机新闻网站,手机新闻网站demo,新闻阅读器开发
- 用nautilus命令打开文件管理器
- 人类的智慧是一般不多?爱因斯坦的智商是多少?
- qt486中文乱码问题
- ADF中选择表行后改变当前行的编辑状态
- 6月18日课堂心得
- 机器学习傻瓜的深入研究
- xshell传输文件
- bzoj1006 [HNOI2008]神奇的国度
- 逆向分析实战
- CART分类与回归树 学习笔记
- 排序 via F#
- java实现excel表格导出
- android studio 操作指南
- How to create zebra stripes in JSP tables
- Fragment使用技巧:使用静态Fragemnt
- document.forms
- XML文件解析---SAX方法