Dijstra的greedy简单实现
来源:互联网 发布:java高级流和低级流 编辑:程序博客网 时间:2024/05/22 08:49
算法本意重于数据结构。
#include <iostream>#include <cstring>#include <limits>using namespace std;//The simpler,the betterconst int maxn=10000;const int inf=100000; // at first I wana use limits::max,but add operation causing overflow,so UDFint book[maxn];int g[maxn][maxn]; //DON'T use complex data structure making you confuing before really understand the origial ideaint dist[maxn];int n,m;void dij(int cur){int min,relaxIdx;//relax variabledist[cur]=0;book[cur]=1; int t=n;//O(n^2)while(--t){min = numeric_limits<int>::max();for(int i=1 ; i<= n ; ++i )//find the shortest pointif(g[cur][i]!=inf && g[cur][i]<min && book[i]==0){min=g[cur][i];relaxIdx = i;}dist[relaxIdx] = min;//update the distance to final statebook[relaxIdx] = 1; //add to the fianl setfor(int i=1 ; i<=n ; ++i)//relaxing the distance using relaxIdxif(g[cur][i]>g[cur][relaxIdx]+g[relaxIdx][i])g[cur][i]=g[cur][relaxIdx]+g[relaxIdx][i];}//Output the resultfor(int i=1; i<=n ; ++i)cout<<dist[i]<<" ";}int main() {// your code goes herecin>>n>>m;int a,b,c;int start=1;//initialize the graph;for(int i=1; i<= n ; ++i)for(int j=1;j<=n;++j)g[i][j]=inf;//input graphfor(int j=0; j<m ; j++){cin>>a>>b>>c;g[a][b]=c;}//initialize the distance arrayfor(int i= 1 ; i<=n ; ++i)if(g[start][i]!=inf)dist[i]=g[start][i];dij(start);return 0;}
0 0
- Dijstra的greedy简单实现
- dijstra(迪杰斯特拉)算法的简单实现
- java 实现的Dijstra算法
- Dijstra算法的Python实现
- hdu 1548 简单 dijstra
- hdu 2680 简单 dijstra
- Dijstra算法 C++实现
- 基于优先队列PriorityQueue的Dijstra算法之Java实现
- 简单实现dijstra算法和floyd算法并打印所有最短路径
- Dijstra
- dijstra
- Dijstra
- dijstra算法 c语言实现
- hdu5521(集合的dijstra)
- Dijstra算法的代码实现及解释(最短路径问题)
- 有向图中Dijstra最短路径算法的邻接表实现
- Dijstra算法实现求解单源最短路径问题
- Greedy
- 高精度运算之乘法
- c++中的vtordisp域
- 内存池设计与实现
- 【iOS】iOS/Xcode异常:reason:loaded the “SimpleTableViewController” nib but the view outlet was not set
- 自动生成IPv6列表
- Dijstra的greedy简单实现
- (转)《Java虚拟机原理图解》1.4 class文件中的字段表集合--field字段在class文件中是怎样组织的
- Java多线程(十)localthread
- 【iOS】UIImageView点击是现实高亮图,放手时现实默认图
- effective C++ 读书笔记 条款12
- 程序员到三十岁之后需要转行或者转管理吗
- Stanford
- URAL_2025_Line Fighting_均值不等式
- 二分查找