图论模板
Posted junk_yao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图论模板相关的知识,希望对你有一定的参考价值。
#include<bits/stdc++.h>
using namespace std;
const int maxv=1001;
const double INFd=1e20;
struct Edge{
int begin, end;
double len;
Edge(int begin, int end, double len):begin(begin), end(end), len(len){}
};
struct Map_list{
int g_size, v_size;
vector<Edge> edges;
vector<int> G[maxv];
Map_list(){v_size=g_size=0;}
void AddEdge(int begin, int end, double len){
edges.push_back(Edge(begin, end, len));
G[begin].push_back(edges.size()-1);
}
void Make(int n, int m){
g_size=m;
v_size=n;
for(int i=0; i<m; i++){
int begin, end;
double len;
cin >> begin >> end >> len;
AddEdge(begin, end, len);
}
}
void show(){
for(int i=1; i<=v_size; i++)
for(int j=0; j<G[i].size(); j++){
Edge & e=edges[G[i][j]];
cout << e.begin << " " << e.end << " "<< e.len << endl;
}
}
}map1;
struct HeapNode{
int u, d;
HeapNode(int u, int d):u(u),d(d){}
bool operator < (const HeapNode & r) const {
return d>r.d;
}
};
void dijkstra(int s){
int pre[maxv], used[maxv];
double dis[maxv];
memset(used, 0, sizeof(used));
memset(pre, 0, sizeof(used));
for(int i=0; i<map1.v_size; i++) dis[i]=INFd;
dis[s]=0;
for(int i=0; i<map1.G[s].size(); i++){
dis[map1.edges[map1.G[s][i]].end]=map1.edges[map1.G[s][i]].len;
}
priority_queue<HeapNode> Q;
Q.push(HeapNode(s, 0));
while(!Q.empty()){
HeapNode t=Q.top();
Q.pop();
int x=t.u;
if(used[x]) continue;
used[x]=1;
for(int k=0; k<map1.G[x].size(); k++){
Edge & e = map1.edges[map1.G[x][k]];
if(dis[x]+e.len<dis[e.end]){
dis[e.end]=dis[x]+e.len;
pre[e.end]=x;
Q.push(HeapNode(e.end, dis[e.end]));
}
}
}
}
int main(void){
}
以上是关于图论模板的主要内容,如果未能解决你的问题,请参考以下文章