From a788926d442b33af0fcfe5834fa9780524b19575 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Wed, 5 Nov 2025 14:18:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0P3953.cpp=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E6=9C=80=E7=9F=AD=E8=B7=AF=E5=BE=84=E8=AE=A1=E6=95=B0?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实现了一个基于Dijkstra算法的最短路径计数解决方案,用于计算在给定约束条件下从起点到终点的路径数量。包含输入处理、最短路径计算和路径计数逻辑。当路径步数超过阈值时返回-1,否则返回路径数模p的结果。 --- src/11/5/P3953.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/11/5/P3953.cpp diff --git a/src/11/5/P3953.cpp b/src/11/5/P3953.cpp new file mode 100644 index 0000000..cb669d2 --- /dev/null +++ b/src/11/5/P3953.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include +#include +#include +using ll = int64_t; +#define sl static inline +// #define printf +const ll maxm=2e5+5; +ll n,m,k,p,nt; +struct E{ + ll v,w; +}; +struct P{ + ll now,step; + inline bool operator<(const P&o)const{ + return step>o.step; + } +}; +std::vector> edg; +std::bitset vis; +std::deque

dq; + +sl ll dj(){ + vis.reset(); + std::priority_queue

pq; + pq.emplace(1,0); + while(pq.size()){ + auto[now,step]=pq.top(); + pq.pop(); + if(now==n)return step; + if(vis[now])continue; + vis[now]=true; + for(auto[v,w]:edg[now]){ + pq.emplace(v,step+w); + } + } + return -1; +} + +sl void solve(){ + printf("NT = %lld\n",++nt); + std::cin>>n>>m>>k>>p; + edg.clear(); + edg.resize(n+1); + for(ll i=1;i<=m;i++){ + ll u,v,w; + std::cin>>u>>v>>w; + edg[u].emplace_back(v,w); + } + const ll d = dj(); + printf("found d=%lld\n",d); + if(d==-1){ + std::cout<<"0\n"; + return; + } + const ll ms=d+k; + ll ans=0; + dq.clear(); + dq.emplace_back(1,0); + ll tick=0; + while(dq.size()){ + tick++; + if(tick>1e6){ + std::cout<<-1<<"\n"; + return; + } + auto[now,step]=dq.front(); + dq.pop_front(); + if(now==n){ + ans++; + } + for(auto[v,w]:edg[now]){ + const ll ns=w+step; + if(ns>ms)continue; + dq.emplace_back(v,ns); + } + } + std::cout<>t; + while(t--){ + solve(); + } +} \ No newline at end of file