From e74b893ff7cc4d00eddc857e4e874f049efdbaca Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Thu, 22 Jul 2021 22:41:50 +0800 Subject: [PATCH] =?UTF-8?q?P1078=20[NOIP2012=20=E6=99=AE=E5=8F=8A=E7=BB=84?= =?UTF-8?q?]=20=E6=96=87=E5=8C=96=E4=B9=8B=E6=97=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R53948670 --- Luogu/problem/P1078/P1078.cpp | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Luogu/problem/P1078/P1078.cpp diff --git a/Luogu/problem/P1078/P1078.cpp b/Luogu/problem/P1078/P1078.cpp new file mode 100644 index 00000000..3cbea9f0 --- /dev/null +++ b/Luogu/problem/P1078/P1078.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int n, k, m, s, t, u, v, w, c[105], g[105][105], f[105][105]; + +int main() { + cin >> n >> k >> m >> s >> t; + for (int i = 1; i <= n; i++) { + cin >> c[i]; + } + for (int i = 1; i <= k; i++) { + for (int j = 1; j <= k; j++) { + cin >> g[i][j]; + } + } + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + f[i][j] = i == j ? 0 : 0x3f3f3f3f; + } + } + for (int i = 1; i <= m; i++) { + cin >> u >> v >> w; + if (!g[c[u]][c[v]] && c[u] != c[v]) { + f[u][v] = min(f[u][v], w); + } + if (!g[c[v]][c[u]] && c[v] != c[u]) { + f[v][u] = min(f[v][u], w); + } + } + if (c[s] == c[t]) { + cout << -1 << endl; + exit(0); + } + for (int k = 1; k <= n; k++) { + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + if (k != i && k != j && i != j) { + f[i][j] = min(f[i][j], f[i][k] + f[k][j]); + } + } + } + } + cout << (f[s][t] == 0x3f3f3f3f ? -1 : f[s][t]) << endl; + return 0; +}