1. 程式人生 > >「學習筆記」鏈式前向星

「學習筆記」鏈式前向星

鏈式前向星

圖的儲存一般有兩種:鄰接矩陣、前向星。

若圖是稀疏圖,邊很少,開二維陣列a[][]很浪費;

若點很多(如10000個點)a[10000][10000]又會爆.只能用前向星做.

 

前向星的效率不是很高,優化後為鏈式前向星,直接介紹鏈式前向星。

 

(一)鏈式前向星

 

1. 結構

這裡用兩個東西:

1 結構體陣列edge存邊,edge[i]表示第i條邊,

2 head[i]存以i為起點的第一條邊(在edge中的下標)

struct EDGE{
	int next;   //下一條邊的儲存下標(預設0) 
	int to;     //這條邊的終點 
	int w;      //權值 
}; 
EDGE edge[500010];

 

 

2.增邊

若以點i為起點的邊新增了一條,在edge中的下標為j.

那麼edge[j].next=head[i];然後head[i]=j.

即每次新加的邊作為第一條邊,最後倒序遍歷

void Add(int u, int v, int w) {  //起點u, 終點v, 權值w 
	//cnt為邊的計數,從1開始計 
	edge[++cnt].next = head[u];
	edge[cnt].w = w;
	edge[cnt].to = v;
	head[u] = cnt;    //第一條邊為當前邊 
} 

 

 

 

3. 遍歷

遍歷以st為起點的邊

for(int i=head[st]; i!=0; i=edge[i].next)

i開始為第一條邊,每次指向下一條(以0為結束標誌)  (若下標從0開始,next應初始化-1)

 

 

一個簡單的輸出有向圖熟悉鏈式前向星:

#include <iostream>
using namespace std;
 
#define MAXM 500010
#define MAXN 10010
 
struct EDGE{
	int next;   //下一條邊的儲存下標 
	int to;     //這條邊的終點 
	int w;      //權值 
}; 
EDGE edge[MAXM];
 
int n, m, cnt;
int head[MAXN];  //head[i]表示以i為起點的第一條邊 
 
void Add(int u, int v, int w) {  //起點u, 終點v, 權值w 
	edge[++cnt].next = head[u];
	edge[cnt].w = w;
	edge[cnt].to = v;
	head[u] = cnt;    //第一條邊為當前邊 
} 
 
void Print() {
	int st;
	cout << "Begin with[Please Input]: \n";
	cin >> st;
	for(int i=head[st]; i!=0; i=edge[i].next) {//i開始為第一條邊,每次指向下一條(以0為結束標誌)若下標從0開始,next應初始化-1 
		cout << "Start: " << st << endl;
		cout << "End: " << edge[i].to << endl;
		cout << "W: " << edge[i].w << endl << endl; 
	}
}
 
int main() {
	int s, t, w;
	cin >> n >> m;
	for(int i=1; i<=m; i++) {
		cin >> s >> t >> w;
		Add(s, t, w);
	}
	Print(); 
	return 0;
}

 

(二)鏈式前向星實現SPFA

 

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
 
#define MAXM 500010
#define MAXN 10010
#define ANS_MAX 2147483647
 
struct EDGE {
	int next;
	int to;
	int w;
};
EDGE edge[MAXM];
 
int n, m, st, cnt;
int head[MAXN];
 
int d[MAXN];
bool inq[MAXN];
 
inline int Read() {
	char c; int ans = 0; bool Sign = false;
	while(!isdigit(c=getchar()) && c != '-');
	if(c == '-') {
		Sign = true;
		c = getchar();
	}
	do {
		ans = (ans<<3) + (ans<<1) + (c ^ '0');
	} while(isdigit(c=getchar()));
	return Sign ? -ans : ans;
}
 
void Add(int u, int v, int w) {
	edge[++cnt].next = head[u];
	edge[cnt].to = v;
	edge[cnt].w = w;
	head[u] = cnt;
}
 
void read() {
	int x, y, w;
	n = Read();
	m = Read();
	st = Read();
	for(int i=1; i<=m; i++) {
		x = Read();
		y = Read();
		w = Read();
		Add(x, y, w);
	}
}
 
void SPFA(int x) {
	d[x] = 0; for(int i=1; i<=n; i++) d[i] = ANS_MAX;
	queue<int> Q; Q.push(x); inq[x] = true;
	while(!Q.empty()) {
		int k = Q.front(); Q.pop(); inq[k] = false;
		for(int i=head[k]; i!=0; i=edge[i].next) {
			int j = edge[i].to;
			if(d[j] > d[k] + edge[i].w) {
				d[j] = d[k] + edge[i].w;
				if(!inq[j]) {
					Q.push(j);
					inq[j] = true;
				}
			}
		}
	}
	for(int i=1; i<=n; i++) printf("%d ", d[i]);
	printf("\n");
}
 
int main() {
	read();
	SPFA(st);
	return 0;
}

 

 

轉載自:https://blog.csdn.net/Binary_Heap/article/details/78209086