基础算法——搜索与图论
图的存储方式https://i-blog.csdnimg.cn/direct/1b0581ceeda14586ba8c00c1f014b300.png
2、最短路问题
最短路问题可以分为单源最短路问题和多源最短路问题,单源最短路问题就是求出从点1->n的最短隔断,而多源最短路问题就是求出从点i->j的最短隔断。单源最短路问题还可以分为正权边的单源最短路问题和负权边的单源最短路问题。具体算法和时间复杂度如下图:
https://i-blog.csdnimg.cn/direct/0238b77cbd6c47c9b9b33a35f82b6a12.png
2.1、Dijkstra算法(朴素版)
https://i-blog.csdnimg.cn/direct/c8aec52fab904ccf80813b152fd2ff30.png
算法模板:
#include <iostream>
#include <cstring>
using namespace std;
const int N = 510;
int g, d;
int n, m;
bool st;
int dijkstra()
{
memset(d, 0x3f, sizeof d);
d = 0;
for (int i = 0; i < n; i++)
{
int t = -1;
for (int j = 1; j <= n; j++)
if (!st && (t == -1 || d > d))
t = j;
st = true;
for (int j = 1; j <= n; j++)
d = min(d, d + g);
}
return d == 0x3f3f3f3f ? -1 : d;
}
int main()
{
cin >> n >> m;
memset(g, 0x3f, sizeof g);
while (m--)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
g = min(g, c);
}
cout << dijkstra() << endl;
return 0;
}
2.2、Dijkstra算法(堆优化版)
下面来看看怎样优化:
https://i-blog.csdnimg.cn/direct/9f6b01a5ced949cfa1f41a1b9a829604.png
算法模板:
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 1.5e5+10;
int h, e, w, ne, idx;
int n, m, d;
bool st;
void add(int a, int b, int c)
{
e = b, w = c, ne = h, h = idx++;
}
int dijkstra()
{
memset(d, 0x3f, sizeof d);
d = 0;
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({0, 1});
while (heap.size())
{
auto t = heap.top();
heap.pop();
int ver = t.second, dis = t.first;
if (st) continue;
st = true;
for (int i = h; i != -1; i = ne)
{
int j = e;
if (d > dis + w)
{
d = dis + w;
heap.push({d, j});
}
}
}
return d == 0x3f3f3f3f ? -1 : d;
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof h);
while (m--)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
cout << dijkstra() << endl;
return 0;
}
2.3、Bellman-Ford算法
https://i-blog.csdnimg.cn/direct/21df9032e2484b72a13361e77fd553b8.png
代码模板:
#include <iostream>
#include <cstring>
using namespace std;
const int N = 510, M = 10010;
int n, m, k;
int d, backup;
struct Edge
{
int a, b, w;
}edges;
void bellman_ford()
{
memset(d, 0x3f, sizeof d);
d = 0;
for (int i = 0; i < k; i++)
{
memcpy(backup, d, sizeof d);
for (int j = 0; j < m; j++)
{
auto e = edges;
d = min(d, backup + e.w);
}
}
}
int main()
{
cin >> n >> m >> k;
for (int i = 0; i < m; i++)
{
int a, b, w;
scanf("%d%d%d", &a, &b, &w);
edges = {a, b, w};
}
bellman_ford();
if (d > 0x3f3f3f3f / 2) cout << "impossible" << endl;
else cout << d << endl;
return 0;
}
2.4、SPFA求最短路
https://i-blog.csdnimg.cn/direct/bc9d17cab0db414385a3d2afe8094668.png
代码模板:
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 1e5+10;
int n, m;
int h, e, w, ne, idx;
int d;
bool st;
void add(int a, int b, int c)
{
e = b, w = c, ne = h, h = idx++;
}
void spfa()
{
memset(d, 0x3f, sizeof d);
d = 0;
queue<int> q;
q.push(1);
st = true;
while (q.size())
{
auto t = q.front();
q.pop();
st = false;
for (int i = h; i != -1; i = ne)
{
int j = e;
if (d > d + w)
{
d = d + w;
if (!st)
{
st = true;
q.push(j);
}
}
}
}
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof h);
while (m--)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
spfa();
if (d == 0x3f3f3f3f) cout << "impossible" << endl;
else cout << d << endl;
return 0;
}
2.5、SPFA判负环
https://i-blog.csdnimg.cn/direct/fcebf45978a64d09941d0c73eb0aee11.png
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 2010, M = 10010;
int h, e, w, ne, idx;
int n, m;
int d, cnt;
bool st;
void add(int a, int b, int c)
{
e = b, w = c, ne = h, h = idx++;
}
bool spfa()
{
queue<int> q;
for (int i = 1; i <= n; i++)
{
q.push(i);
st = true;
}
while (q.size())
{
auto t = q.front();
q.pop();
st = false;
for (int i = h; i != -1; i = ne)
{
int j = e;
if (d > d + w)
{
d = d + w;
cnt = cnt + 1;
if (cnt >= n) return true;
if (!st)
{
st = true;
q.push(j);
}
}
}
}
return false;
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof h);
while (m--)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
if (spfa()) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
2.6、Floyd算法
https://i-blog.csdnimg.cn/direct/a78bbb6b86694dc2a32ea53d0502df92.png
#include <iostream>
#include <cstring>
using namespace std;
const int N = 210, INF = 0x3f3f3f3f;
int d;
int n, m, k;
void floyd()
{
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
d = min(d, d + d);
}
int main()
{
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i == j) d = 0;
else d = INF;
while (m--)
{
int a, b, c;
cin >> a >> b >> c;
d = min(d, c);
}
floyd();
while (k--)
{
int l, r;
cin >> l >> r;
if (d > INF / 2) cout << "impossible" << endl;
else cout << d << endl;
}
return 0;
}
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]