Skip to content

Commit cb0a733

Browse files
author
aaron.liu
committed
chapter of graph.
1 parent 9e03c97 commit cb0a733

18 files changed

+723
-1
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
77
ADD_SUBDIRECTORY(TwoPointers)
88
ADD_SUBDIRECTORY(DFS)
99
ADD_SUBDIRECTORY(BFS)
10+
ADD_SUBDIRECTORY(LinkedLists)
11+
ADD_SUBDIRECTORY(Trie)
12+
ADD_SUBDIRECTORY(Graph)
1013

1114
add_executable(AlgoInCpp main.cpp)

DFS/LC1020NumberOfEnclaves.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//
44

55
#include <vector>
6-
#include <unordered_map>
76
using namespace std;
87

98
class NumberOfEnclaves {

Graph/AW849DijkstraI.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// Created by Aaron Liu on 4/13/20.
3+
//
4+
5+
#include <cstring>
6+
#include <algorithm>
7+
#include <iostream>
8+
9+
using namespace std;
10+
11+
const int N = 505;
12+
int n,m;
13+
int g[N][N];
14+
int dist[N];
15+
bool st[N];
16+
17+
int main() {
18+
scanf("%d%d", &n, &m);
19+
20+
memset(g, 0x3f, sizeof g);
21+
22+
for ( ; m > 0; m--) {
23+
int a, b,c;
24+
scanf("%d%d%d", &a, &b, &c);
25+
g[a][b] = min(g[a][b], c);
26+
}
27+
28+
}
29+
30+
31+

Graph/AW850DijkstraII.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// 850. Dijkstra求最短路 II
3+
// problem link: https://www.acwing.com/problem/content/852/
4+
//
5+
6+
#include <cstring>
7+
#include <iostream>
8+
#include <algorithm>
9+
#include <queue>
10+
11+
using namespace std;
12+
13+
const int N = 1e6 + 5;
14+
15+
typedef pair<int, int> PII; // 堆里存的是pair<int, int>
16+
int n, m;
17+
int h[N], w[N], e[N], ne[N], idx;
18+
int dist[N];
19+
bool st[N];
20+
21+
22+
void add (int a, int b, int c) {
23+
e[idx] = b;
24+
w[idx] = c;
25+
ne[idx] = h[a];
26+
h[a] = idx;
27+
idx += 1;
28+
}
29+
30+
int dijkstra() {
31+
memset(dist, 0x3f, sizeof dist);
32+
dist[1] = 0;
33+
priority_queue<PII, vector<PII>, greater<PII>> heap;
34+
heap.push({0,1});
35+
36+
for ( ; heap.size() > 0; ) {
37+
auto t = heap.top();
38+
heap.pop();
39+
40+
int ver = t.second, distance = t.first;
41+
if (st[ver]) continue;
42+
st[ver] = true;
43+
for (int i = h[ver]; i != -1; i = ne[i]) {
44+
int j = e[i];
45+
if (dist[j] > distance + w[i]) {
46+
dist[j] = distance + w[i];
47+
heap.push({dist[j], j});
48+
}
49+
}
50+
}
51+
52+
if (dist[n] == 0x3f3f3f3f) return -1;
53+
return dist[n];
54+
}
55+
56+
57+
int main() {
58+
scanf("%d%d", &n, &m);
59+
memset(h, -1, sizeof h);
60+
61+
for ( ; m > 0; m--) {
62+
int a, b, c;
63+
scanf("%d%d%d", &a, &b, &c);
64+
add(a, b, c);
65+
}
66+
67+
cout << dijkstra() << endl;
68+
return 0;
69+
}

Graph/AW851SPFA.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//
2+
// 851. spfa求最短路
3+
// problem link: https://www.acwing.com/problem/content/853/
4+
//
5+
#include <cstring>
6+
#include <algorithm>
7+
#include <queue>
8+
9+
using namespace std;
10+
11+
const int N = 100010;
12+
13+
int n, m;
14+
int h[N], w[N], e[N], ne[N], idx;
15+
int dist[N];
16+
bool st[N];
17+
18+
void add(int a, int b, int c) {
19+
e[idx] = b;
20+
w[idx] = c;
21+
ne[idx] = h[a];
22+
h[a] = idx;
23+
idx += 1;
24+
}
25+
26+
int spfa() {
27+
memset(dist, 0x3f, sizeof dist);
28+
dist[1] = 0;
29+
queue<int> q;
30+
q.push(1);
31+
st[1] = true;
32+
33+
for ( ; q.size() != 0; ) {
34+
int t = q.front();
35+
q.pop();
36+
st[t] = false;
37+
38+
for (int i = h[t]; i != -1; i = ne[i]) {
39+
int j = e[i];
40+
if (dist[j] > dist[t] + w[i]) {
41+
dist[j] = dist[t] + w[i];
42+
if (!st[j]) {
43+
q.push(j);
44+
st[j] = true;
45+
}
46+
}
47+
}
48+
}
49+
50+
if (dist[n] == 0x3f3f3f3f) return -1;
51+
return dist[n];
52+
}
53+
54+
int main()
55+
{
56+
scanf("%d%d", &n, &m);
57+
58+
memset(h, -1, sizeof h);
59+
60+
for ( ; m > 0; m--) {
61+
int a, b, c;
62+
scanf("%d%d%d", &a, &b, &c);
63+
add(a, b, c);
64+
}
65+
66+
int t = spfa();
67+
if (t == -1) puts("impossible");
68+
else printf("%d\n", t);
69+
70+
return 0;
71+
}

Graph/AW852NegativeCycleSPFA.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// AcWing 852. spfa判断负环
3+
// link: https://www.acwing.com/problem/content/854/
4+
5+
#include <cstring>
6+
#include <algorithm>
7+
#include <queue>
8+
9+
using namespace std;
10+
const int N = 100010;
11+
12+
int n, m;
13+
int h[N], w[N], e[N], ne[N], idx;
14+
int dist[N], cnt[N];
15+
bool st[N];
16+
17+
void add(int a, int b, int c) {
18+
e[idx] = b;
19+
w[idx] = c;
20+
ne[idx] = h[a];
21+
h[a] = idx;
22+
idx += 1;
23+
}
24+
25+
bool spfa() {
26+
queue<int> q;
27+
for (int i = 1; i <= n; i++) {
28+
q.push(i);
29+
st[i] = true;
30+
}
31+
32+
for ( ; q.size() != 0; ) {
33+
int t = q.front();
34+
q.pop();
35+
st[t] = false;
36+
37+
for (int i = h[t]; i != -1; i = ne[i]) {
38+
int j = e[i];
39+
if (dist[j] > dist[t] + w[i]) {
40+
dist[j] = dist[t] + w[i];
41+
cnt[j] = cnt[t] + 1;
42+
if (cnt[j] >= n) return true;
43+
if (!st[j]) {
44+
q.push(j);
45+
st[j] = true;
46+
}
47+
}
48+
}
49+
}
50+
return false;
51+
}
52+
53+
int main() {
54+
scanf("%d%d", &n, &m);
55+
memset(h, -1, sizeof h);
56+
57+
for (; m > 0; m--) {
58+
int a, b, c;
59+
scanf("%d%d%d", &a, &b, &c);
60+
add(a, b, c);
61+
}
62+
63+
if (spfa()) puts("Yes");
64+
else puts("No");
65+
66+
return 0;
67+
}

Graph/AW853BFWithEdgeNumLimit.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// 853. 有边数限制的最短路
3+
// problem link: https://www.acwing.com/problem/content/855/
4+
//
5+
6+
#include <cstring>
7+
#include <iostream>
8+
#include <algorithm>
9+
10+
using namespace std;
11+
12+
const int N = 510, M = 10010;
13+
int n, m, k;
14+
int backup[N], dist[N];
15+
struct Edge {
16+
int a, b, w;
17+
} edges[M];
18+
19+
int bellman_ford() {
20+
memset(dist, 0x3f3f3f3f, sizeof dist);
21+
dist[1] = 0;
22+
for (int i = 0; i < k; i++) {
23+
memcpy(backup, dist, sizeof dist);
24+
for (int j = 0; j < m; j++) {
25+
26+
int a = edges[j].a, b = edges[j].b, w = edges[j].w;
27+
dist[b] = min(dist[b], backup[a] + w);
28+
}
29+
}
30+
31+
if (dist[n] > 0x3f3f3f3f / 2) return -1;
32+
return dist[n];
33+
}
34+
35+
int main() {
36+
37+
scanf("%d%d%d", &n, &m, &k);
38+
39+
for (int i = 0; i < m; i++) {
40+
int a, b, w;
41+
scanf("%d%d%d", &a, &b, &w);
42+
edges[i] = {a, b, w};
43+
}
44+
45+
int t = bellman_ford();
46+
if (t == -1) puts("impossible");
47+
else printf("%d\n", t);
48+
return 0;
49+
}

Graph/AW854Floyd.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// 854. Floyd求最短路
3+
// link: https://www.acwing.com/problem/content/856/
4+
5+
#include <cstring>
6+
#include <algorithm>
7+
8+
using namespace std;
9+
10+
const int N = 210, INF = 1e9;
11+
12+
int n, m, q;
13+
int d[N][N];
14+
15+
void floyd() {
16+
for (int k = 1; k <= n; k++) {
17+
for (int i = 1; i <= n; i++) {
18+
for (int j = 1; j <= n; j++) {
19+
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
20+
}
21+
}
22+
}
23+
}
24+
25+
int main() {
26+
scanf("%d%d%d", &n, &m, &q);
27+
for (int i = 1; i <= n; i++) {
28+
for (int j = 1; j <= n; j++) {
29+
if (j == i) {
30+
d[i][j] = 0;
31+
} else {
32+
d[i][j] = INF;
33+
}
34+
}
35+
}
36+
37+
for (; m > 0; m--) {
38+
int a, b, w;
39+
scanf("%d%d%d", &a, &b, &w);
40+
d[a][b] = min(d[a][b], w);
41+
}
42+
floyd();
43+
44+
for (; q > 0; q--) {
45+
int a, b;
46+
scanf("%d%d", &a, &b);
47+
// 特殊情况: 负权边更新负最大
48+
if (d[a][b] > INF / 2) puts("impossible");
49+
else printf("%d\n", d[a][b]);
50+
}
51+
52+
return 0;
53+
}

0 commit comments

Comments
 (0)