Compare commits

...

2 Commits

Author SHA1 Message Date
Zengtudor
e66f06db69 feat: 添加三个算法题目解决方案
添加了三个算法题目的解决方案文件:
1. wwfsqf.cpp - 实现树结构的DFS遍历和合并操作
2. P3393.cpp - 实现基于BFS和优先队列的最短路径算法
3. P3831.cpp - 实现基于Dijkstra算法的地铁站最短路径计算
2025-10-02 12:14:04 +08:00
Zengtudor
d94980c638 feat: 添加图遍历算法的初始实现
实现基础的图遍历算法框架,包括输入处理和DFS函数占位
2025-10-02 09:56:34 +08:00
3 changed files with 248 additions and 0 deletions

63
src/10/2/P3393.cpp Normal file
View File

@ -0,0 +1,63 @@
#include <algorithm>
#include <bitset>
#include <cstdint>
#include <functional>
#include <iostream>
#include <istream>
#include <queue>
#include <utility>
#include <vector>
using ll = int64_t;
const ll maxn = 2e5+5, inf=1e9+7;
ll n,m,k,s,p,q,c[maxn],distb[maxn],dist[maxn];
std::vector<std::vector<ll>> adj;
std::queue<ll> que;
std::bitset<maxn> isc,isw;
int main(){
std::iostream::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin>>n>>m>>k>>s>>p>>q;
for(ll i=1;i<=n;i++)distb[i]=inf,dist[i]=inf;
for(ll i=1;i<=k;i++){
std::cin>>c[i];
isc[c[i]]=true;
que.push(c[i]);
distb[c[i]]=0;
}
adj.resize(n+1);
for(ll i=1;i<=m;i++){
ll a,b;
std::cin>>a>>b;
adj[a].emplace_back(b);
adj[b].emplace_back(a);
}
while(que.size()){
ll u = que.front();
que.pop();
for(ll v:adj[u]){
distb[v]=std::min(distb[v],distb[u]+1);
if(distb[v]<s)que.push(v);
}
}
std::priority_queue<std::pair<ll, ll>, std::vector<std::pair<ll, ll>>, std::greater<std::pair<ll, ll>>> pq;
pq.emplace(0,1);
while(pq.size()){
auto[ss,u] = pq.top();
pq.pop();
if(dist[u]<ss){
continue;
}
for(auto v:adj[u]){
if(v==n){
std::cout<<ss<<"\n";
return 0;
}
if(isc[v])continue;
dist[v]=std::min(dist[v],ss+(distb[v]<=s?q:p));
pq.emplace(ss+(distb[v]<=s?q:p),v);
}
}
}// MLE TODEBUG

137
src/10/2/P3831.cpp Normal file
View File

@ -0,0 +1,137 @@
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <queue>
#include <vector>
using ll = int64_t;
struct Station {
int id;
int x, y;
};
using State = std::pair<ll, int>;
const ll INF = 1e18;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m;
std::cin >> n >> m;
std::vector<Station> sta(m + 2);
for (int i = 0; i < m; ++i) {
sta[i].id = i;
std::cin >> sta[i].x >> sta[i].y;
}
int startx, starty, endx, endy;
std::cin >> startx >> starty >> endx >> endy;
sta[m].id = m;
sta[m].x = startx;
sta[m].y = starty;
sta[m + 1].id = m + 1;
sta[m + 1].x = endx;
sta[m + 1].y = endy;
const int N = m + 2;
const int prenode = 2 * N;
std::vector<std::vector<std::pair<int, int>>> adj(prenode);
std::vector<int> p(N);
for (int i = 0; i < N; ++i)
p[i] = i;
std::sort(p.begin(), p.end(), [&](int a, int b) {
if (sta[a].x != sta[b].x)
return sta[a].x < sta[b].x;
return sta[a].y < sta[b].y;
});
for (int i = 1; i < N; ++i) {
int uidx = p[i - 1];
int vidx = p[i];
if (sta[uidx].x == sta[vidx].x) {
ll weight = 2LL * (sta[vidx].y - sta[uidx].y);
adj[uidx].push_back({vidx, (int)weight});
adj[vidx].push_back({uidx, (int)weight});
}
}
std::sort(p.begin(), p.end(), [&](int a, int b) {
if (sta[a].y != sta[b].y)
return sta[a].y < sta[b].y;
return sta[a].x < sta[b].x;
});
for (int i = 1; i < N; ++i) {
int uidx = p[i - 1];
int vidx = p[i];
if (sta[uidx].y == sta[vidx].y) {
ll weight = 2LL * (sta[vidx].x - sta[uidx].x);
adj[uidx + N].push_back({vidx + N, (int)weight});
adj[vidx + N].push_back({uidx + N, (int)weight});
}
}
if (startx == endx) {
adj[m].push_back({m + 1, (int)(2LL * std::abs(starty - endy))});
adj[m + 1].push_back({m, (int)(2LL * std::abs(starty - endy))});
}
if (starty == endy) {
adj[m + N].push_back({m + 1 + N, (int)(2LL * std::abs(startx - endx))});
adj[m + 1 + N].push_back({m, (int)(2LL * std::abs(startx - endx))});
}
for (int i = 0; i < m; ++i) {
adj[i].push_back({i + N, 1});
adj[i + N].push_back({i, 1});
}
std::vector<ll> dist(prenode, INF);
std::priority_queue<State, std::vector<State>, std::greater<State>> pq;
int startid = m;
int endid = m + 1;
dist[startid] = 0;
pq.push({0, startid});
dist[startid + N] = 0;
pq.push({0, startid + N});
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (d > dist[u]) {
continue;
}
for (auto &edge : adj[u]) {
int v = edge.first;
int weight = edge.second;
if (dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
pq.push({dist[v], v});
}
}
}
ll result = std::min(dist[endid], dist[endid + N]);
if (result == INF) {
std::cout << -1 << "\n";
} else {
std::cout << result << "\n";
}
}

48
src/10/2/wwfsqf.cpp Normal file
View File

@ -0,0 +1,48 @@
#include <cstdint>
#include <iostream>
#include <istream>
#include <queue>
#include <vector>
using ll = int64_t;
#define sl static inline
std::vector<std::vector<ll>> edg;
ll T,n;
std::vector<std::priority_queue<ll>> pq;
sl void merge(ll f,ll s){
}
sl void dfs(ll u){
for(auto v:edg[u]){
dfs(v);
}
}
sl void solve(){
std::cin>>n;
edg.clear();
pq.clear();
pq.resize(n+1);
edg.resize(n+1);
for(ll i=1;i<=n-1;i++){
ll u,v;
std::cin>>u>>v;
edg[u].emplace_back(v);
edg[v].emplace_back(u);
}
dfs(1);
}
int main(){
std::iostream::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin>>T;
while(T--){
solve();
}
}