From 5233639a89eede7b1b0229c7d155f03e436bee32 Mon Sep 17 00:00:00 2001 From: Frederick Chen Date: Fri, 14 Feb 2025 02:48:38 +0000 Subject: [PATCH] New: total 2 codes --- FZOI/【模板】负环.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++ FZOI/最短路径问题.cpp | 55 +++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 FZOI/【模板】负环.cpp create mode 100644 FZOI/最短路径问题.cpp diff --git a/FZOI/【模板】负环.cpp b/FZOI/【模板】负环.cpp new file mode 100644 index 0000000..a1545c9 --- /dev/null +++ b/FZOI/【模板】负环.cpp @@ -0,0 +1,63 @@ +#include +#define lo long long +#define INF 0x3f3f3f3f3f3f3f3fl +#define LLM LONG_LONG_MAX + +using namespace std; +const lo N = 1e5 + 10; +/* + toothless. #17 + @fredcss_dev +*/ + +struct Edge { + lo u, v, w; +}; + +lo t, n, m; +lo c, p, W; + +vector edge; + +lo dis[N], u, v, w; + +bool bellmanford(lo n, lo s) +{ + memset(dis, 0x3f, (n + 1) * sizeof(lo)); + dis[s] = 0; + bool flag = false; + for (lo i = 1; i <= n; i++) { + flag = false; + for (lo j = 0; j < edge.size(); j++) { + u = edge[j].u, v = edge[j].v, w = edge[j].w; + if (dis[u] == INF) continue; + if (dis[v] > dis[u] + w) { + dis[v] = dis[u] + w; + flag = true; + } + } + if (!flag) { + break; + } + } + return flag; +} + +signed main() +{ + cin >> t; + while (t--) { + edge.clear(); + cin >> n >> m; + for (lo i = 1; i <= m; i++) { + cin >> c >> p >> W; + edge.push_back({c, p, W}); + if (W >= 0) edge.push_back({p, c, W}); + } + if (bellmanford(n, 1)) { + cout << "YES" << endl; + } else { + cout << "NO" << endl; + } + } +} diff --git a/FZOI/最短路径问题.cpp b/FZOI/最短路径问题.cpp new file mode 100644 index 0000000..ebfe194 --- /dev/null +++ b/FZOI/最短路径问题.cpp @@ -0,0 +1,55 @@ +#include +#define lo long long +#define INF INT_MAX +#define LLM LONG_LONG_MAX + +using namespace std; +const int N = 1e7 + 10; +/* + toothless. #17 + @fredcss_dev +*/ + +int n, m, s, t; +double ma[101][101], dp[101][101]; + +struct Node { + int x, y; +} a[101]; + +signed main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + cin >> n; + for (int i = 1; i <= n; i++) { + cin >> a[i].x >> a[i].y; + } + cin >> m; + for (int i = 1; i <= m; i++) { + int c, d; + cin >> c >> d; + double k = sqrt(double(pow((a[c].x - a[d].x), 2)) + double(pow((a[c].y - a[d].y), 2))); + ma[c][d] = k; + ma[d][c] = k; + } + cin >> s >> t; + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + if (ma[i][j]) { + dp[i][j] = ma[i][j]; + } else { + dp[i][j] = INT_MAX; + } + } + } + for (int k = 1; k <= n; k++) { + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]); + } + } + } + printf("%.2lf", dp[s][t]); +}