搜索与图论

目录

搜索

 拓扑排序

最短路 

 Dijkstra 算法(稠密图 边 >= 点的平方 O(n^2))

 ⭐堆优化版 Dijkstra 算法(稀疏图 边 < 点的平方 O(mlogn))

Bellman-Ford O(nm)

 ⭐SPFA(一般O(m), 最坏O(nm))

 Floyd O(n^3)

 最小生成树 

 ⭐Prim O(n^2)

堆优化版Prim O(mlogn)

⭐克鲁斯卡尔算法 O(mlogm)

 二分图

染色法 O(m + n) (注:此算法用于判断图是否为二分图)

匈牙利算法 (O(mn),实际运行时间一般远小于O(mn))(注:用于判断最大匹配数)


搜索

dfs(深度优先搜索):一条路走到黑。沿着一条路一直搜索,直到走不通时,再进行回溯。

bfs(广度优先搜索):像波纹一样层层向外扩展。每层搜索完全后,在进行下一层的搜索。

 拓扑排序

 一个序列,包含图中所有的点;在这个序列中,排在后面的点,不会有指向排在前面节点的出边。且这个序列不是唯一的。

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int N = 100010;

int h[N], e[N], ne[N], idx;
int q[N], d[N];
int n, m;

void add(int a, int b)
{
	e[idx] = b , ne[idx] = h[a], h[a] = idx ++;
}

bool topsort()
{
	int hh = 0, tt = -1;
	
	for (int i = 1; i <= n; i ++ )
		if (!d[i])	q[ ++ tt] = i;
	
	while (hh <= tt)
	{
		int t = q[hh ++];
		
		for (int i = h[t]; i != -1; i = ne[i])
		{
			int j = e[i];
			d[j] --;
			if (d[j] == 0) q[ ++ tt] = j;
		}
	}
	
	return tt == n - 1;
}

int main()
{
	cin >> n >> m;
	
	memset(h, -1, sizeof h);
	
	for (int i = 0; i < m; i ++)
	{
		int a, b;
		cin >> a >> b;
		add(a, b);
		 d[b] ++;
	}
	
	if (topsort())
	{
		for (int i = 0; i < n; i ++) cout << q[i];
		puts("");
	}
	else puts("-1");
	
	return 0;
}

最短路 

 Dijkstra 算法(稠密图 边 >= 点的平方 O(n^2))
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

const int N = 10010;

int n, m;
int g[N][N];
int dist[N];
bool st[N];

int dijkstra()
{
	memset(dist, 0x3f, sizeof dist);
	dist[1] = 0;
	
	for (int i = 0; i  < n; i ++)
	{
		int t = -1;
		for (int j = 1; j <= n; j ++)
			if (!st[j] && (t == -1 || dist[t] > dist[j]))
				t = j;
				
		st[t] = true;
		
		for (int j = 1; j <= n; j ++)
			dist[j] = min (dist[j], dist[t] + g[t][j]);
	}
	
	if (dist[n] == 0x3f3f3f3f) return -1;
	return dist[n];
}

int main()
{
	cin >> n >> m;
	
	memset (g, 0x3f, sizeof g);
	
	while (m --)
	{
		int a, b, c;
		cin >> a >> b >> c;
		g[a][b] = min (g[a][b], c);
	} 
	
	int t = dijkstra();
	
	cout << t;
	
	return 0;
}
 ⭐堆优化版 Dijkstra 算法(稀疏图 边 < 点的平方 O(mlogn))
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>

using namespace std;

typedef pair<int, int> PII;

const int N = 100010;

int n, m;
int h[N], w[N], e[N], ne[N], idx;
int dist[N];
bool st[N];

void add(int a, int b, int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}

int dijkstra()
{
	memset(dist, 0x3f, sizeof dist);
	dist[1] = 0;
	
	priority_queue<PII, vector<PII>, greater<PII> > heap;
	heap.push({0, 1});
	
	while (heap.size())
	{
		PII t = heap.top();
		heap.pop();
		
		int ver = t.second, distance = t.first;
		if (st[ver]) continue;
		
		for (int i = h[ver]; i != -1; i = ne[i])
		{
			int j = e[i];
			if (dist[j] > distance + w[i])
			{
				dist[j] = distance + w[i];
				heap.push ({dist[j], j});
			}
		}
	}
	
	if (dist[n] == 0x3f3f3f3f) return -1;
	return dist[n];
}

int main()
{
	cin >> n >> m;
	
	memset (h, -1, sizeof h);
	
	while (m --)
	{
		int a, b, c;
		cin >> a >> b >> c;
		add (a, b, c);
	} 
	
	int t = dijkstra();
	
	cout << t;
	
	return 0;
}
Bellman-Ford O(nm)
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

const int N = 510, M = 10010;

int n, m, k;
int dist[N], backup[N];

struct Edge
{
	int a, b, w;
}edges[M];

int bellman_ford()
{
	for (int i = 0; i < k; i ++)  // 只经过 K 条边, 能到的点的最短距离
	{
		memcpy(backup, dist, sizeof dist);
		for (int j = 0; j < m; j ++)
		{
			int a = edges[j].a, b = edges[j].b, w = edges[j].w;
			dist[b] = min(dist[b], backup[a] + w);
		}
	}
	
	if (dist[n] > 0x3f3f3f3f / 2) return -1;
	return dist[n];
}

int main()
{
	cin >> n >> m >> k;
	
	for (int i = 0; i < m; i ++)
	{
		int a, b, w;
		cin >> a >> b >> w;
		edges[i] = {a, b, w};
	}
	
	int t = bellman_ford();
	
	if (t == -1) puts("impossible");
	else cout << t << endl;
	
	return 0;
}
 ⭐SPFA(一般O(m), 最坏O(nm))
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

const int N = 100010;

int n, m;
int h[N], w[N], e[N], ne[N], idx;
int dist[N], cnt[N];
bool st[N];

void add(int a, int b, int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}

int spfa()
{
	memset(dist, 0x3f, sizeof dist);
	dist[1] = 0;
	
	queue<int> q;
	q.push(1);
	st[1] = true;
	
	while (q.size())
	{
		int t = q.front();
		q.pop();
		
		st[t] = false;
		
		for (int i = h[t]; i != -1; i = ne[i])
		{
			int j = e[i];
			if (dist[j] > dist[t] + w[i])
			{
				dist[j] = dist[t] + w[i];
				if (!st[j])
				{
					q.push(j);
					st[j] = true;
				}
			}
		}
	}
	
	if (dist[n] == 0x3f3f3f3f) return -1;
	return dist[n];
}

int main()
{
	cin >> n >> m;
	memset(h, -1, sizeof h);
	
	for (int i = 0; i < m; i ++)
	{
		int a, b, w;
		cin >> a >> b >> w;
		add(a, b, w);
	}
	
	int t = spfa();
	
	if (t == -1) puts("impossible");
	else cout << t << endl;
	
	return 0;
}
 Floyd O(n^3)
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 210, INF = 1e9;

int n, m, Q;
int d[N][N];

void floyd()
{
	for (int k = 1; k <= n; k ++)
		for (int i = 1; i <= n; i ++)
			for (int j = 1; j <= n; j ++)
				d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}

int main()
{
	cin >> n >> m >> Q;
	
	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= n; j ++)
			if (i == j) d[i][j] = 0;
			else d[i][j] = INF;
			
	while (m --)
	{
		int a, b, w;
		cin >> a >> b >> w;
		d[a][b] = min(d[a][b], w);
	}
	
	floyd();
	
	while (Q --)
	{
		int a, b;
		cin >> a >> b;
		
		if (d[a][b] > INF / 2) puts("impossible");
		else cout << d[a][b] << endl;
	}
	
	return 0;
}

 最小生成树 

 

 ⭐Prim O(n^2)
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 510, INF = 0x3f3f3f3f;

int n, m;
int g[N][N];
int dist[N];
bool st[N];

int prim()
{
	memset(dist, 0x3f, sizeof dist);
	
	int res = 0;
	
	for (int i = 0; i < n; i ++)
	{
		int t = -1;
		for (int j = 1; j <= n; j ++)
			if (!st[j] && (t == -1 || dist[t] > dist[j]))
				t = j;
		
		if (i && dist[t] == INF) return INF;
		if (i) res += dist[t];
		
		for (int j = 1; j <= n; j ++) dist[j] = min(dist[j], g[t][j]);
		
		st[t] = true;
	}
	
	return res;
}

int main()
{
	cin >> n >> m;
	
	memset(g, 0x3f, sizeof g);
	
	while (m --)
	{
		int a, b, c;
		cin >> a >> b >> c;
		
		g[a][b] = min (g[a][b], c);
	}
	
	int t = prim();
	
	if (t == INF) puts("impossible");
	else printf("%d\n", t);
	
	return 0;
}
堆优化版Prim O(mlogn)

 略(参照 Dijkstra 与 堆优化版Dijkstra 算法)

⭐克鲁斯卡尔算法 O(mlogm)
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;

int n, m;
int p[N];

struct Edge
{
	int a, b, w;
	
	bool operator< (const Edge &W)const
	{
		return w < W.w;
	}
}edges[N];

int find(int x)
{
	if (p[x] != x) p[x] = find(p[x]);
	return p[x];
	
}

int main()
{
	cin >> n >> m;
	
	for (int i = 0; i < m; i ++)
	{
		int a, b, w;
		cin >> a >> b >> w;
		edges[i] = {a, b, w};
	}
	
	sort(edges, edges + m);
	
	for (int i = 1; i <= n; i ++) p[i] = i;
	
	int res = 0, cnt = 0;
	for (int i = 0; i < m; i ++)
	{
		int a = edges[i].a, b = edges[i].b, w = edges[i].w;
		
		a = find(a), b = find(b);
		if (a != b)
		{
			p[a] = b;
			res += w;
			cnt ++;
		}
	}
	
	if (cnt < n - 1) puts("impossible");
	else printf("%d\n", res);
	
	return 0;
}

 二分图

染色法 O(m + n) (注:此算法用于判断图是否为二分图)
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010, M = 200010;

int n, m;
int h[N], e[M], ne[M], idx;
int color[N];

void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

bool dfs(int u, int c)
{
	color[u] = c;
	
	for (int i = h[u]; i != -1; i = ne[i])
	{
		int j = e[i];
		if (!color[j])
		{
			if (!dfs(j, 3 - c)) return false;
		}
		else if (color[j] == c) return false;
	}
	
	return true;
}

int main()
{
	cin >> n >> m;
	
	memset(h, -1, sizeof h);
	
	while (m --)
	{
		int a, b;
		cin >> a >> b;
		
		add(a, b), add(b, a);
	}
	
	bool flag = true;
	for (int i = 1; i <= n; i ++)
		if (!color[i])
		{
			if (!dfs(i, 1))
			{
				flag = false;
				break;
			}
		}
		
	if (flag) puts("Yes");
	else puts("No");
	
	return 0;
}
匈牙利算法 (O(mn),实际运行时间一般远小于O(mn))(注:用于判断最大匹配数)
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 510, M = 100010;

int n1, n2, m;
int h[N], e[N], ne[N], idx;
int match[N];
bool st[N];

void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

bool find(int x)
{
	for (int i = h[x]; i != -1; i = ne[i])
	{
		int j = e[i];
		if (!st[j])
		{
			st[j] = true;
			if (match[j] == 0 || find(match[j]))
			{
				match[j] = x;
				return true;
			}
		}
	}
	
	return false;
}

int main()
{
	cin >> n1 >> n2 >> m;
	
	memset(h, -1, sizeof h);
	
	while (m --)
	{
		int a, b;
		cin >> a >> b;
		add(a, b);
	}
	
	int res = 0;
	for (int i = 1; i <= n1; i ++)
	{
		memset(st, false, sizeof st);
		if (find(i)) res ++;
	}
	
	cout << res << endl;
	
	return 0;
}
  • 11
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值