From 4643a1b272e898eb58be5d10c99d1768fabdd9e4 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 09:18:58 +0800 Subject: [PATCH] 'commit' --- TangDou/Topic/HDU1599.cpp | 46 ++++++++++++++++++++++++++++++ TangDou/Topic/【Floyd专题】.md | 30 ++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 TangDou/Topic/HDU1599.cpp diff --git a/TangDou/Topic/HDU1599.cpp b/TangDou/Topic/HDU1599.cpp new file mode 100644 index 0000000..d5f2ddd --- /dev/null +++ b/TangDou/Topic/HDU1599.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; +#define int long long +#define endl "\n" +const int INF = 0x3f3f3f3f; +const int N = 110; + +int mp[N][N], dis[N][N]; +int n, m, a, b, c, ans; + +void floyd() { + for (int k = 1; k <= n; k++) { + for (int i = 1; i < k; i++) // 枚举ij + for (int j = i + 1; j < k; j++) // 注意ijk不能相同 + if (ans > mp[i][j] + dis[j][k] + dis[k][i]) + ans = mp[i][j] + dis[j][k] + dis[k][i]; + + for (int i = 1; i <= n; i++) // 原floyd + for (int j = 1; j <= n; j++) + if (mp[i][j] > mp[i][k] + mp[k][j]) + mp[i][j] = mp[i][k] + mp[k][j]; + } +} +signed main() { + while (cin >> n >> m && (~n && ~m)) { + for (int i = 1; i <= n; i++) + for (int j = 1; j <= n; j++) + if (i == j) + mp[i][j] = dis[i][j] = 0; + else + mp[i][j] = dis[i][j] = INF; + while (m--) { + cin >> a >> b >> c; + if (c < mp[a][b]) // 防重边,怕了怕了 + mp[a][b] = dis[a][b] = c; + if (c < mp[b][a]) + mp[b][a] = dis[b][a] = c; + } + ans = INF; + floyd(); + if (ans == INF) + puts("It's impossible."); + else + printf("%lld\n", ans); + } +} \ No newline at end of file diff --git a/TangDou/Topic/【Floyd专题】.md b/TangDou/Topic/【Floyd专题】.md index 63860a7..457fec8 100644 --- a/TangDou/Topic/【Floyd专题】.md +++ b/TangDou/Topic/【Floyd专题】.md @@ -190,7 +190,35 @@ int main() { #### [$HDU$-$1599$ $find$ $the$ $mincost$ $route$](https://acm.hdu.edu.cn/showproblem.php?pid=1599) -(最小环) + +**类型: 最小环** + +**题意**: +>杭州有$N$个景区,景区之间有一些双向的路来连接,现在$8600$想找一条旅游路线,这个路线从$A$点出发并且最后回到$A$点,假设经过的路线为$V_1,V_2,…V_K$,$V_1$,那么必须满足$K>2$,就是说至除了出发点以外至少要经过$2$个其他不同的景区,而且不能重复经过同一个景区。现在$8600$需要你帮他找一条这样的路线,并且花费越少越好。 +>**$Input$** +第一行是$2$个整数$N$和$M$($N <= 100, M <= 1000$),代表景区的个数和道路的条数。 +接下来的$M$行里,每行包括$3$个整数$a,b,c$.代表$a$和$b$之间有一条通路,并且需要花费$c$元($c <= 100$)。 +**$Output$** +对于每个测试实例,如果能找到这样一条路线的话,输出花费的最小值。如果找不到的话,输出"It’s impossible.". +**$Sample$ $Input$** +cpp +3 3 +1 2 1 +2 3 1 +1 3 1 +3 3 +1 2 1 +1 2 3 +2 3 1 +**$Sample$ $Output$** +3 +It’s impossible + +**分析**: +求最小环,用$dis[]$记录原距离,当枚举中间结点 $k$时,首先知道任意两点 $i、j$不经过 $k$的最短路径 $mp[i][j]$(原$floyd$的二三重循环后更新 $mp[i][j]$得到经过 $k$的最短路),此时枚举 $i$和 $j$得到一个经过 $k$的环( $i$到 $j$, $j$到 $k$, $k$到 $i$)并记录最小答案即可,即 $mp[i][j] + dis[j][k] + dis[k][i]$。 +注意题目 $i, j, k$不能相同,还有坑点:`long long` + + #### [$HDU$-$1704$ $Rank$](https://acm.hdu.edu.cn/showproblem.php?pid=1704) (传递闭包)