Blue Bridge Cup - 3 ways to store pictures

  • Adjacency matrix
    uses a two-dimensional array d[i][j]to store the graph, d[i][j]generally the distance or connection relationship between point i and point j
  • The adjacency linked list
    stores the graph in the form of a linked list, which saves space. The basic idea is that each node acts as the head of a linked list, and we will construct n linked lists for n nodes. Nodes other than the head node on a linked list, representing the adjacent nodes to the head node
  • Chained Forward Star
    An accelerated data structure for seeking the shortest path, the essence is to use an array to represent the adjacency linked list. There are two core data structures:
  • head
int head[N];

The head head[i]indicates the number of the edge corresponding to the node i, for example, head[i]=cntit indicates that the i node corresponds to cntthe edge number

  • side
struct Edge{
    
    
	int next=0;	//next表示下一个边的编号,即兄弟边
	int to=0;	//to表示作为终点的节点的编号
	int w;		//w表示权重
}edge[N];

head[i]=cntIndicates that node i has a cnt-th edge edge[cnt], this edge is i->edge[cnt].to, and the length is edge[cnt].w; node i also has an adjacent edge edge[cnt].next, and the adjacent edge isi->edge[edge[cnt].next].to

  • traverse all adjacent edges of node i
cnt=head[i]
while(cnt!=0){
    
    
	printf("%d->%d	w:%d",i,edge[cnt].to,edge[cnt].w);
	cnt=edge[cnt].next;
}
  • add edge
//这一步相当于在链表中插入一个新节点
void addEdge(int u,int v,int w){
    
    
	cnt++;
	//确定终点
	edge[cnt].v=v;
	//确定权重
	edge[cnt].w=w;
	//插入节点
	edge[cnt].next=head[u];
	head[u]=cnt;
}

The essence of chained forward star is an adjacency linked list

  • Chain forward star + heap optimization + dijkstra
#include<iostream>
#include<bits/stdc++.h>
#include <queue>
using namespace std;
const int N=1e5+9;
const int MAXEDGE=5e5+9;
const long long int MAXNUM=0x7fffffff;
int n,m,s;
long long int d[N];
bool flag[N];
//链式前向星
int head[N];
long long int cnt=0;
struct Edge{
    
    
	long long int next;
	long long int to;
	long long int w;
}edge[MAXEDGE];
void addEdge(long long int u,long long int v,long long int w){
    
    
	cnt++;
	//构造边 
	edge[cnt].to=v;
	edge[cnt].w=w;
	edge[cnt].next=head[u];
	head[u]=cnt;
}
int main(){
    
    
	cin>>n>>m>>s;
	//初始化距离
	for(int i=1;i<=n;i++){
    
    
		d[i]=MAXNUM;
		flag[i]=false;
		head[i]=0;
	}
	//堆优化 
	d[s]=0;
	priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> minHeap;
	minHeap.push(pair<int,int>(0,s));
	
	//构造前向星(本质上是邻接链表)
	long long u,v,w;
	for(int i=1;i<=m;i++){
    
    
		cin>>u>>v>>w;
		addEdge(u,v,w);
	} 
	
	//dijkstra算法
	//采用堆优化时必须用堆空作为循环结束条件,因为堆中会有重复的节点 
	while(!minHeap.empty()){
    
    
		 
		//寻找距离s最近的点
		int k=minHeap.top().second; 
		minHeap.pop();
		//先判断k是否访问过
		if(flag[k]==true)	continue;
		flag[k]=true;
		//进行松弛
		for(int j=head[k];j!=0;j=edge[j].next){
    
    
			if(flag[edge[j].to]==false&&d[edge[j].to]>d[k]+edge[j].w){
    
    
				d[edge[j].to]=d[k]+edge[j].w;
				//压入堆
				minHeap.push(pair<int,int>(d[edge[j].to],edge[j].to));		
			}
		} 
	} 
	for(int i=1;i<=n;i++){
    
    
		cout<<d[i]<<" ";
	}
	return 0;	
}

Guess you like

Origin blog.csdn.net/qq_33880925/article/details/129465686