algorithm@ dijkstra algorithm & prim algorithm
#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的更多相关文章
- prim algorithm
function re=biaoji(j,biao) %判断j点是否已被标记 l=length(biao); for i=1:l if j==biao(i) re=1; return; end end ...
- Dijkstra和Prim算法的区别
Dijkstra和Prim算法的区别 1.先说说prim算法的思想: 众所周知,prim算法是一个最小生成树算法,它运用的是贪心原理(在这里不再证明),设置两个点集合,一个集合为要求的生成树的点集合A ...
- dijkstra spfa prim kruskal 总结
最短路和最小生成树应该是很早学的,大家一般都打得烂熟,总结一下几个问题 一 dijkstra O((V+E)lgV) //V节点数 E边数 dijkstra不能用来求最长路,因为此时局部最优解已经 ...
- Algorithm --> Dijkstra和Floyd最短路径算法
Dijkstra算法 一.最短路径的最优子结构性质 该性质描述为:如果P(i,j)={Vi....Vk..Vs...Vj}是从顶点i到j的最短路径,k和s是这条路径上的一个中间顶点,那么P(k,s)必 ...
- [algorithm] Dijkstra双栈算法表达式求值算法
一.原理 Dijkstra所做的一个算法,双栈求值,用两个栈(一个保存运算符,一个用于保存操作数), 表达式由括号,运算符和操作数组成. (1).将操作数压入操作数栈 (2).将运算符压入运算符栈: ...
- 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:// ...
- 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 ...
- 全排列算法(字典序法、SJT Algorithm 、Heap's Algorithm)
一.字典序法 1) 从序列P的右端开始向左扫描,直至找到第一个比其右边数字小的数字,即. 2) 从右边找出所有比大的数中最小的数字,即. 3) 交换与. 4) 将右边的序列翻转,即可得到字典序的下一个 ...
- [Algorithm] Maximum Contiguous Subarray algorithm implementation using TypeScript / JavaScript
Naive solution for this problem would be caluclate all the possible combinations: const numbers = [1 ...
随机推荐
- gwt 创建 超链接cell (HyperTextCell)
package com.cnblogs.hooligen.client; import com.google.gwt.cell.client.AbstractCell; import com.goog ...
- 史上最全github使用方法:github入门到精通--备用
[初识Github] 首先让我们大家一起喊一句“Hello Github”.YEAH!就是这样. Git是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理 ...
- Oracle11g用户密码过期
今天一早来了,发现部署的网站访问报500,看日志,显示数据库连不上.然后我用sqldeveloper登录同样登不上,于是想到了之前碰到过的一个问题,Oralce11g的新特性:密码180天自动过期.随 ...
- this、call和apply
this call apply this 和其他语言不同,JavaScript的this总是指向一个对象,而具体指向哪个对象是在运行时基于函数的执行环境动态绑定的,而非函数被声明时的环境. this的 ...
- POj 2186 Popular Cows[连通分量]
题目大意:给出N头牛,有M种关系u, v.代表u牛崇拜v牛.要求找出有多少头牛被所有牛崇拜着题目链接:http://poj.org/problem?id=2186解题思路:1>求出强连通分量,标 ...
- spoj LCS
初识后缀自动机: 推荐学习:http://blog.sina.com.cn/s/blog_7812e98601012dfv.html #include<cstdio> #include&l ...
- ***微信浏览器禁止app下载链接怎么办
通过扫描二维码下载APP已成为一个非常方便的方式,微信也成为扫描二维码重要的工具,但是扫描后微信浏览器会对APK和appStore的链接进行屏蔽,导致用户无法正常下载.本文提供两个迂回的解决方案:1. ...
- HDU 2369 Broken Keyboard(字符串)
点我看题目 题意 : 这个人的键盘坏了,最多只能按n个键,给你一串字符串,问你找一个最长的字串,这个字串中包含的不同的字母不能超过n个. 思路 : 比赛的时候脑子没转过来,一直没模拟出来,都不知道怎么 ...
- MySql从服务器延迟解决方案
在从服务器上执行show slave status;可以查看到很多同步的参数,我们需要特别注意的参数如下:Master_Log_File: SLAVE中的I/ ...
- UVA 125 Numbering Paths
题目大意:给定n条单向边,求图中任意两点的连通路径的数目.其中点是从0-输入中出现的最大的点. 可以用floyd-warshall算法或者dfs. for(int k = 0; k < n; k ...