From 311747d5423c510c524bc47e95f65a5a7b75ff93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Fri, 5 Jan 2024 11:27:50 +0800 Subject: [PATCH] 'commit' --- TangDou/Topic/P1828.cpp | 38 +++++++++++++++ TangDou/Topic/SSL_1760.cpp | 29 ++++++++++++ TangDou/Topic/【Floyd专题】.md | 75 +++++++++++++++++++++++++++++- 3 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 TangDou/Topic/P1828.cpp create mode 100644 TangDou/Topic/SSL_1760.cpp diff --git a/TangDou/Topic/P1828.cpp b/TangDou/Topic/P1828.cpp new file mode 100644 index 0000000..640ea05 --- /dev/null +++ b/TangDou/Topic/P1828.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; +const int N = 814; +const int INF = 0x3f3f3f3f; +int id[N]; +int a, b, c, g[N][N], res = INF; +int main() { + // 加快读入 + ios::sync_with_stdio(false), cin.tie(0); + int p, n, m; // p只奶牛,n个牧场,m条边 + cin >> p >> n >> m; + + memset(g, 0x3f, sizeof g); + for (int i = 1; i <= n; i++) g[i][i] = 0; // 初始化 + + for (int i = 1; i <= p; i++) cin >> id[i]; // i号奶牛,在id[i]这个牧场 + + while (m--) { + cin >> a >> b >> c; + g[a][b] = g[b][a] = min(c, g[a][b]); + } + // 标准的Floyd板子 + for (int k = 1; k <= n; k++) + for (int i = 1; i <= n; i++) { + if (g[i][k] == INF) continue; // floyd小优化 + for (int j = 1; j <= n; j++) + if (g[i][j] > g[i][k] + g[k][j]) + g[j][i] = g[i][j] = g[i][k] + g[k][j]; + } + + for (int i = 1; i <= n; i++) { // 每个牧场出发 + int ans = 0; + for (int j = 1; j <= p; j++) ans += g[i][id[j]]; + res = min(res, ans); + } + printf("%d", res); + return 0; +} diff --git a/TangDou/Topic/SSL_1760.cpp b/TangDou/Topic/SSL_1760.cpp new file mode 100644 index 0000000..65d006d --- /dev/null +++ b/TangDou/Topic/SSL_1760.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; +const int N = 210; +int n, g[N][N]; +const int INF = 0x3f3f3f3f; +int ans = INF; +int main() { + cin >> n; + for (int i = 1; i <= n; i++) + for (int j = 1; j <= n; j++) { + cin >> g[i][j]; + if (g[i][j] == 0 && i != j) g[i][j] = INF; // 建图(注意i==j要为0) + } + // floyd + for (int k = 1; k <= n; k++) + for (int i = 1; i <= n; i++) + for (int j = 1; j <= n; j++) + if (g[i][j] > g[i][k] + g[k][j]) + g[i][j] = g[i][k] + g[k][j]; + + int s; + for (int i = 1; i <= n; i++) { + s = 0; + for (int j = 1; j <= n; j++) s += g[i][j]; + if (s < ans) ans = s; + } + printf("%d", ans); + return 0; +} diff --git a/TangDou/Topic/【Floyd专题】.md b/TangDou/Topic/【Floyd专题】.md index 825828d..d0ab3ac 100644 --- a/TangDou/Topic/【Floyd专题】.md +++ b/TangDou/Topic/【Floyd专题】.md @@ -378,4 +378,77 @@ int main() { } return 0; } -``` \ No newline at end of file +``` + +### 八、其它习题 + +#### $SSL-1760$(商店选址) +**题目** +给出一个城市的地图(用邻接矩阵表示),商店设在一点,使各个地方到商店距离之和最短。 + +$Input$ +第一行为$n$(共有几个城市); $N$小于$201$ +第二行至第$n+1$行为城市地图(用邻接矩阵表示) + +$Output$ +最短路径之和 + +$Sample$ $Input$ +```cpp {.line-numbers} +3 +0 3 1 +3 0 2 +1 2 0 +1 +2 +3 +4 +``` + +$Sample$ $Output$ +```cpp {.line-numbers} +3 +1 +``` +```cpp {.line-numbers} +#include +using namespace std; +const int N = 210; +int n, g[N][N]; +const int INF = 0x3f3f3f3f; +int ans = INF; +int main() { + cin >> n; + for (int i = 1; i <= n; i++) + for (int j = 1; j <= n; j++) { + cin >> g[i][j]; + if (g[i][j] == 0 && i != j) g[i][j] = INF; // 建图(注意i==j要为0) + } + // floyd + for (int k = 1; k <= n; k++) + for (int i = 1; i <= n; i++) + for (int j = 1; j <= n; j++) + if (g[i][j] > g[i][k] + g[k][j]) + g[i][j] = g[i][k] + g[k][j]; + + int s; + for (int i = 1; i <= n; i++) { + s = 0; + for (int j = 1; j <= n; j++) s += g[i][j]; + if (s < ans) ans = s; + } + printf("%d", ans); + return 0; +} + +``` +#### [$P1828$ [$USACO3.2$] 香甜的黄油 $Sweet$ $Butter$](https://www.luogu.com.cn/problem/P1828) + + +SSL-1761(城市问题) + +#### [$P1364$ 医院设置](https://www.luogu.com.cn/problem/P1364) + +SSL-1613=CCF1342(最短路径问题) + +