From 88b393ac28d81881b32378a5f4509ab9ed66e115 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Wed, 10 Sep 2025 20:51:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0P9981.cpp=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E6=8B=93=E6=89=91=E6=8E=92=E5=BA=8F=E5=92=8C=E6=9C=80?= =?UTF-8?q?=E9=95=BF=E8=B7=AF=E5=BE=84=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实现了一个解决特定图论问题的程序,主要功能包括: - 读取输入并构建图结构 - 执行拓扑排序 - 计算每个节点的最长路径及其边权和 - 输出每个节点的路径长度和边权总和 --- src/9/10/P9981.cpp | 107 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/9/10/P9981.cpp diff --git a/src/9/10/P9981.cpp b/src/9/10/P9981.cpp new file mode 100644 index 0000000..c01f818 --- /dev/null +++ b/src/9/10/P9981.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include +#include +#include +#include +using ll = int64_t; + +ll n,m; + +struct Edge{ + ll to,l; +}; +std::vector> edg; +std::vector ind; +std::queue q; +std::vector topo; +std::vector len; +std::vector> lv; + +#define p1v(ind)do{\ + std::cout<<#ind<<"\n";\ + for(auto& i:ind){\ + std::cout<&a,const vector&b){ + if((a.size()|b.size()) == 0){ + return false; + } + if(a.size()==0 && b.size()!=0){ + return true; + } + if(a.size()!=0 && b.size() == 0){ + return false; + } + for(ll i=0;ib[i])return false; + } + return a.size() &v){ + ll res=0; + for(auto i:v){ + res+=i; + } + return res; +} + +int main(){ + std::iostream::sync_with_stdio(false); + std::cin.tie(nullptr); + // std::cout<<(std::vector({1,2,2})({1,2,3}))<<"\n"; + std::cin>>n>>m; + edg.resize(n+1); + ind.resize(n+1); + len.resize(n+1); + lv.resize(n+1); + + for(ll i=1;i<=m;i++){ + ll a,b,l; + std::cin>>b>>a>>l; + edg[a].emplace_back(b,l); + ind[b]++; + } + for(ll u=1;u<=n;u++){ + if(ind[u]==0){ + q.emplace(u); + } + } + while(q.size()){ + ll top = q.front(); + q.pop(); + topo.emplace_back(top); + for(auto [to,l]:edg[top]){ + ind[to]--; + if(ind[to]==0){ + q.emplace(to); + // len[to]=std::max(len[to],len[top]+1); + if(len[top]+1>len[to]){ + len[to]=len[top]+1; + lv[to] = lv[top]; + lv[to].emplace_back(l); + }else if(len[to]==len[top]+1){ + std::vector nlv = lv[top]; + nlv.emplace_back(l); + if(lv[to]