#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. less的使用方法

    LESS学习 1.变量 我们可以把一个css样式的值赋给一个参数,然后再设置样式的时候只需要设置这个参数名,如果要修改,就改参数的值就可以了. demo.html <!DOCTYPE html& ...

  2. 【BZOJ】1046 : [HAOI2007]上升序列

    1046: [HAOI2007]上升序列 题意:给定S={a1,a2,a3,…,an}问是否存在P={ax1,ax2,ax3,…,axm},满足(x1 < x2 < … < xm)且 ...

  3. MVC-简单验证码制作

    1.制作验证码: using System; using System.Collections.Generic; using System.Drawing; using System.Drawing. ...

  4. hdu 3333 Turing Tree 图灵树(线段树 + 二分离散)

    http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Others)    ...

  5. msisdn与imsi简介

    =======================================imsi========================================1 定义:imsi=MCC+MNC ...

  6. 对于数组使用sizeof(a)和使用sizeof(a[0])

    #include "stdafx.h" #include <iostream> using namespace std; int main() { ]={}; cout ...

  7. Codeforces Round #210

    A:简单题: #include<cstdio> using namespace std; int n,k; int main() { scanf("%d%d",& ...

  8. HDU 1176 免费馅饼(DP)

    点我看题目 题意 : 中文题.在直线上接馅饼,能接的最多是多少. 思路 :这个题其实以前做过.....你将这个接馅饼看成一个矩阵,也不能说是一个矩阵,反正就是一个行列俱全的形状,然后秒当行,坐标当列, ...

  9. Linux使用wake_up_interruptible()唤醒注册到等待队列上的进程

    http://blog.sina.com.cn/s/blog_4770ef020101h48l.html     功能:唤醒注册到等待队列上的进程 原型:     #include     void ...

  10. [Unity菜鸟] Mecanim 系统遇到的问题

    1. 给角色添加一个Animator组件和New State,运行后,摆出这种奇怪的姿势 这是因为没有把动画片段赋给New State,可以看到此时的New State为空,把Idle片段拖进去就好了 ...