hdu 1874畅通工程续= =
2016-03-22 19:24
381 查看
Dijkstra: #include <iostream> #include <string.h> #include <stdlib.h> #include <stdio.h> using namespace std; int book[205]; int dis[205]; int map[205][205]; int n , m , a,b,x,s,t,mind,po; const int inf = 9999999; int d(int s){ memset(book,0,sizeof(book)); book[s] = 1; for(int i = 0;i < n; i++) dis[i] = map[s][i]; for(int i = 0;i < n; i++){ mind = inf; 每次找离出发点最近的点 for(int j = 0;j < n; j++){ if(dis[j]<mind&&!book[j]){ mind = dis[j]; po = j; } } book[po] = 1; 标记走过该点 for(int i = 0;i < n; i++){ if(dis[i]>dis[po]+map[po][i]) dis[i]=dis[po]+map[po][i]; } } return dis[t]; } int main(int argc, char *argv[]) { while(cin>>n>>m){ for(int i = 0;i < n; i++){ for(int j = 0;j < n; j++){ if(i==j)map[i][j] = 0; else map[i][j] = inf; } } for(int i = 1;i <= m; i++){ cin>>a>>b>>x; if(x<map[a][b]){//这地方wa了好多次。。唉,, map[a][b] = x; map[b][a] = x; } } for(int i = 0;i < n; i++) dis[i] = inf; cin>>s>>t; if(d(s)==inf) cout<<“-1”<<endl; else cout<<dis[t]<<endl; } return 0; } Floyd¡ªwarshall 这个算法貌似更简单。。 #include <iostream> #include <string.h> #include <stdlib.h> #include <stdio.h> using namespace std; int map[205][205]; const int inf = 999999; int main(int argc, char *argv[]) { int n,m,i,j,k,a,b,c,st,end; while(cin>>n>>m){ for(int i = 0;i < n; i++){ for(int j = 0;j < n; j++){ if(i==j)map[i][j] = 0; else map[i][j] = inf; } } for(int i = 0;i < m ;i++) { cin>>a>>b>>c; if(map[a][b]>c){ map[a][b] = c; map[b][a] = c; } } for(int k = 0;k < n; k++){ 检验i、j点借助k点能否缩短它们间的距离 for(int i = 0;i < n; i++){ for(int j = 0;j < n; j++){ if(map[i][j]>map[i][k]+map[k][j]) map[i][j]=map[i][k]+map[k][j]; } } } cin>>st>>end; if(map[st][end]==inf)cout<<"-1"<<endl; else cout<<map[st][end]<<endl; } return 0; }
相关文章推荐
- 详解图的应用(最小生成树、拓扑排序、关键路径、最短路径)
- python编写的最短路径算法
- 【算法】最短路径之A*搜索
- Dijkstra和floyd——求单源点最短路径
- 初学图论-Dijkstra单源最短路径算法基于优先级队列(Priority Queue)的实现
- poj 1511 Invitation Cards
- 最少步数BFS
- 想讨论一个指定必经节点求最短路径思路,大伙有什么思路没有?
- Six Degrees of Cowvin Bacon(最短路径floyd算法)
- 关于N*N方格从(1,1)到(N,N)的最短距离
- 最短路径问题迪杰斯特拉(Dijkstra)Android 测试
- HUD----3790最短路径问题
- dijkstra_最短路径_hdu_3790
- poj_1135_最短路径&枚举
- hdu1874 最短路径 畅通工程续
- arcgis for android 短路径分析 查找最短路径
- 单源最短路径 : Bellman-Ford 算法
- 每对顶点的最短路径 : 基本算法
- 每对顶点的最短路径 : Johnson 算法
- 遗传算法染色体变长编码的实现