#include<iostream>
#include<cstdio>
#include<cstring>
#include<limits>
#include<vector>
using namespace std;
const int maxn=;
struct edge{
int to,cost;
edge(int t,int c){
this->to=t; this->cost=c;
}
};
void addEdge(vector<edge> &edgelist, vector<vector<int> > &G,int from,int to,int cost){
edge e = edge(to,cost);
edgelist.push_back(e);
G[from].push_back(edgelist.size()-);
}
void addDoubleEdge(vector<edge> &edgelist, vector<vector<int> > &G,int from,int to,int cost){
addEdge(edgelist,G,from,to,cost);
addEdge(edgelist,G,to,from,cost);
}
int dijkstra(vector<edge> edgelist,vector<vector<int> > G,int v,int END){
vector<int> d(G.size());
vector<int> vis(G.size());
for(int i=;i<vis.size();++i) vis[i]=false;
for(int i=;i<d.size();++i) d[i]=numeric_limits<int>::max();
for(int i=;i<G[v].size();++i){
edge e = edgelist[G[v][i]];
d[e.to] = e.cost;
} vis[v]=true; for(int i=;i<G.size();++i){
int Min=numeric_limits<int>::max(), k;
for(int j=;j<G.size();++j){
if(!vis[j] && d[j] < Min){
Min = d[j];
k = j;
}
}
vis[k]=true; for(int j=;j<G[k].size();++j){
edge e = edgelist[G[k][j]];
if(!vis[e.to] && d[k] + e.cost < d[e.to]) d[e.to] = d[k] + e.cost;
}
}
return d[END];
}
int prim(vector<edge> edgelist,vector<vector<int> > G,int v){
int overall_cost = ;
vector<int> lowcost(G.size());
vector<int> closet(G.size());
vector<int> vis(G.size());
for(int i=;i<vis.size();++i) vis[i]=false;
for(int i=;i<lowcost.size();++i) lowcost[i]=numeric_limits<int>::max(); for(int i=;i<G[v].size();++i){
edge e = edgelist[G[v][i]];
lowcost[e.to] = e.cost;
closet[e.to] = v;
} vis[v]=true;
for(int i=;i<G.size();++i){
int Min=numeric_limits<int>::max(), k;
for(int j=;j<G.size();++j){
if(!vis[j] && lowcost[j] < Min){
Min = lowcost[j];
k = j;
}
}
cout<< Min <<endl;
overall_cost += Min;
vis[k] = true;
//closet[k] = v;
for(int j=;j<G[k].size();++j){
edge e = edgelist[G[k][j]];
if(!vis[e.to] && e.cost < lowcost[e.to]){
lowcost[e.to] = e.cost;
closet[e.to] = k;
}
}
}
return overall_cost;
}
void buildMap(vector<edge> &edgelist, vector<vector<int> > &G){
addDoubleEdge(edgelist, G, , , );
addDoubleEdge(edgelist, G, , , );
addDoubleEdge(edgelist, G, , , ); addDoubleEdge(edgelist, G, , , );
addDoubleEdge(edgelist, G, , , );
addDoubleEdge(edgelist, G, , , ); addDoubleEdge(edgelist, G, , , );
addDoubleEdge(edgelist, G, , , );
addDoubleEdge(edgelist, G, , , );
addDoubleEdge(edgelist, G, , , ); /*
addEdge(edgelist,G,0,2,6);
addEdge(edgelist,G,0,1,4);
addEdge(edgelist,G,0,3,6); addEdge(edgelist,G,1,2,1);
addEdge(edgelist,G,1,4,7); addEdge(edgelist,G,2,5,4);
addEdge(edgelist,G,2,4,6); addEdge(edgelist,G,3,5,5);
addEdge(edgelist,G,3,2,2); addEdge(edgelist,G,4,6,6); addEdge(edgelist,G,5,4,1);
addEdge(edgelist,G,5,6,8);
*/
}
int main(){
vector<edge> edgelist;
vector<vector<int> > G(maxn); buildMap(edgelist, G); cout<<endl<<dijkstra(edgelist, G, , )<<endl; cout<<prim(edgelist, G, )<<endl; return ; }

algorithm@ dijkstra algorithm & prim algorithm的更多相关文章

  1. prim algorithm

    function re=biaoji(j,biao) %判断j点是否已被标记 l=length(biao); for i=1:l if j==biao(i) re=1; return; end end ...

  2. Dijkstra和Prim算法的区别

    Dijkstra和Prim算法的区别 1.先说说prim算法的思想: 众所周知,prim算法是一个最小生成树算法,它运用的是贪心原理(在这里不再证明),设置两个点集合,一个集合为要求的生成树的点集合A ...

  3. dijkstra spfa prim kruskal 总结

    最短路和最小生成树应该是很早学的,大家一般都打得烂熟,总结一下几个问题 一  dijkstra  O((V+E)lgV) //V节点数 E边数 dijkstra不能用来求最长路,因为此时局部最优解已经 ...

  4. Algorithm --> Dijkstra和Floyd最短路径算法

    Dijkstra算法 一.最短路径的最优子结构性质 该性质描述为:如果P(i,j)={Vi....Vk..Vs...Vj}是从顶点i到j的最短路径,k和s是这条路径上的一个中间顶点,那么P(k,s)必 ...

  5. [algorithm] Dijkstra双栈算法表达式求值算法

    一.原理 Dijkstra所做的一个算法,双栈求值,用两个栈(一个保存运算符,一个用于保存操作数), 表达式由括号,运算符和操作数组成. (1).将操作数压入操作数栈 (2).将运算符压入运算符栈: ...

  6. A Fast Priority Queue Implementation of the Dijkstra Shortest Path Algorithm

    http://www.codeproject.com/Articles/24816/A-Fast-Priority-Queue-Implementation-of-the-Dijkst http:// ...

  7. hdu, KMP algorithm, linear string search algorithm, a nice reference provided 分类: hdoj 2015-07-18 13:40 144人阅读 评论(0) 收藏

    reference: Rabin-Karp and Knuth-Morris-Pratt Algorithms By TheLlama– TopCoder Member https://www.top ...

  8. 全排列算法(字典序法、SJT Algorithm 、Heap's Algorithm)

    一.字典序法 1) 从序列P的右端开始向左扫描,直至找到第一个比其右边数字小的数字,即. 2) 从右边找出所有比大的数中最小的数字,即. 3) 交换与. 4) 将右边的序列翻转,即可得到字典序的下一个 ...

  9. [Algorithm] Maximum Contiguous Subarray algorithm implementation using TypeScript / JavaScript

    Naive solution for this problem would be caluclate all the possible combinations: const numbers = [1 ...

随机推荐

  1. 数据结构---顺序表(C++)

    顺序表 是用一段地址连续的存储单元依次存储线性表的数据元素. 通常用一维数组来实现 基本操作: 初始化 销毁 求长 按位查找 按值查找 插入元素 删除位置i的元素 判空操作 遍历操作 示例代码: // ...

  2. python与编码

    Python中的文字对象 Python 3.x中处理文字的对象有str, bytes, bytearray. bytes和bytearray可以使用除了用作格式化的方法(format, format_ ...

  3. jvm 性能调优

    [转载]:http://blog.csdn.net/chen77716/article/details/5695893 最近因项目存在内存泄漏,故进行大规模的JVM性能调优 , 现把经验做一记录. 一 ...

  4. Oracle数据库安装完成之后的启动操作

    由于是菜鸟,在 完成Oracle数据库的安装之后,不知道该怎么启动.在经过一番折腾之后明白了其中的一些道理,总结如下: 其实Oracle数据库和Mysql数据库的启动都是相同的原理. Mysql数据库 ...

  5. join的一对多,去除重复,排序优先的group方法

    想将问题列表按照最新回答来排列.但问题和回答是分拆在两张表来存放的.所以,要完成上述需求,需从主表“问题”取显示数据,但是得按照次表(回答)的更新日期来排序. 用join来做,始终无法去除重复,折腾了 ...

  6. ConfigParser读取记事本、notepad++修改后的配置文件会出现:ConfigParser.MissingSectionHeaderError

    使用ConfigParser来读取配置文件,经常会发现经过记事本.notepad++修改后的配置文件读取时出现下面的问题: ConfigParser.MissingSectionHeaderError ...

  7. Spring 配置XML文件头部文件格式

    普通格式: <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns:xsi="ht ...

  8. H2O与Java线程同步

    Java 5以前的线程同步采用syncronized和wait,notify,notifyAll来实现,比较粗糙.之后有了Lock和Condition.ReentrantLock的简单lock,unl ...

  9. 升级yosemite后java出错的解决

    昨天升级mac os到yosemite后,因为是系统整体升级,有一些在设置会丢失,这是后话,先说说我在执行一个需要java参与的程序的时候得到如下错误: Error: JAVA_HOME is not ...

  10. BZOJ_1600_[Usaco2008_Oct]_建造栅栏_(动态规划)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1600 将长度为n的线段分成4段,长度为整数,围成面积>0的四边形,求方案数. 分析 首先 ...