当前位置:网站首页>[2022 Hangzhou Electric Multi-School 5 1003 Slipper] Multiple Super Source Points + Shortest Path
[2022 Hangzhou Electric Multi-School 5 1003 Slipper] Multiple Super Source Points + Shortest Path
2022-08-04 20:56:00 【Uchiha one hit seven~】
题目描述

输入

输出

样例输入
1
6
6 1 2
3 5 2
2 4 6
5 2 2
5 6 20
3 8
6 5
样例输出
12
题意
一棵树,Each edge has a weight,然后可以以pThe cost is transmitted to the distance from the current nodek层的任意节点,pis given by the input,Now give you a starting point and an ending point,Ask what is the shortest cost from start to finish.
思路
In addition to being able to transmit this condition, it is a shortest path,if the distancekIf each point of the layer is built with an edge, then there are too many edges,Then you can think of building a super source point at each layer,The distance of this super source point from the point of this layer is 0,and distance this layer iskThe distance of the super source point is p,然后wa掉了,Because only one super source point is established, the distance between any two points on the same layer will become 0,这是不成立的,Then you can build two super source points per layer,一个进,一个出,这样问题就迎刃而解了,Let's take a look at this sample image first,The blue dots on each layer are the out points,The red dot is the entry point.
下面看代码吧:
#include<bits/stdc++.h>
using namespace std;
const int N = 5e6+10;
typedef long long LL;
int h[N],e[N+N],ne[N+N];
int w[N+N],idx;
void add(int a,int b,int c){
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
vector<int> de[N];
int n,k,p;
LL dis[N];
bool st[N];
int mx;
void dfs(int u,int fa,int deep){
de[deep].push_back(u);
mx = max(mx,deep);
for(int i=h[u];i!=-1;i=ne[i]){
int v = e[i];
if(v == fa) continue;
dfs(v,u,deep+1);
}
}
typedef pair<LL,int> PII;
void solve(){
scanf("%d",&n);
idx = 0;
mx = 0;
for(int i=1;i<=4*n;i++){
h[i] = -1;
dis[i] = 0x3f3f3f3f3f3f3f3fll;
de[i].clear();
st[i] = false;
}
for(int i=1;i<n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
scanf("%d%d",&k,&p);
dfs(1,0,1);
for(int i=1;i<=mx;i++){
for(auto u : de[i]){
add(u,n+i*2-1,0);
add(i*2+n,u,0);
}
if(i+k>mx) continue;
add(n+i*2-1,n+(i+k)*2,p);
add(n+(i+k)*2-1,n+i*2,p);
}
int sta,ed;
scanf("%d%d",&sta,&ed);
priority_queue<PII,vector<PII>,greater<PII> > q;
dis[sta] = 0;
q.push(make_pair(0,sta));
while(q.size()){
int t = q.top().second;
q.pop();
if(st[t]) continue;
if(t == ed) break;
st[t] = true;
for(int i=h[t];i!=-1;i=ne[i]){
int j = e[i];
if(dis[j] > dis[t] + w[i]){
dis[j] = dis[t] + w[i];
if(!st[j]) q.push({
dis[j],j});
}
}
}
printf("%lld\n",dis[ed]);
}
int main(){
int _;
scanf("%d",&_);
while(_--) solve();
return 0;
}
边栏推荐
猜你喜欢
随机推荐
动态规划_双数组字符串
How to train a deep learning model?
【debug】postgres数据存储错乱
win10 uwp 使用 WinDbg 调试
ASP.NET商贸进销存管理系统源码(带数据库文档)源码免费分享
Cryptography Series: PEM and PKCS7, PKCS8, PKCS12
五分钟入门文本处理三剑客grep awk sed
二叉搜索树解决硬木问题
vs Code runs a local web server
uwp ScrollViewer content out of panel when set the long width
数据仓库(1)什么是数据仓库,数仓有什么特点
格密码入门
run command for node
ADB 安装 + 打驱动全教程
构建Buildroot根文件系统(I.MX6ULL)
如何最简单、通俗地理解爬虫的Scrapy框架?
【学术相关】清华教授发文劝退读博:我见过太多博士生精神崩溃、心态失衡、身体垮掉、一事无成!...
关于 SAP 电商云 Spartacus UI SSR 的 state transfer 问题
【2022杭电多校5 1003 Slipper】多个超级源点+最短路
搭建MyCat2一主一从的MySQL读写分离









