当前位置:网站首页>Hangdian Multi-School-Slipper- (tree map conversion + virtual point mapping)
Hangdian Multi-School-Slipper- (tree map conversion + virtual point mapping)
2022-08-04 04:16:00 【cute girl】
题意:
就是给你一个以1为根的树,Then each side has its own cost.然后小AYou can use any time magic,Use a magic can make depth is poor forkThe two points can transfer each other,花费为m.然后给你起点和终点,Ask your minimum cost.
思考:
1.See this tree problem,My feeling is that the treedp.But wanted to think how todp呢?The jump is not update transfer ah.So here is a treedp就不可以了.Feeling is supposed to be a problem,But later had a lot of,So, it is not problem.
2.Since then feel the treedp不能做,Also pleaseA到B的最短距离,It can build figure and then run the most short circuit.Here are simple,Built figure is established between can jump what point to build map good.You can put the depth of each point out maintenance,那么深度为aTo a depth ofa+kTo build edge,If violence enumerated little built side it isnn的复杂度.Actually can create a virtual point,然后aThe point set of the virtual point,The right of the point set even imaginary point.这样复杂度是4n的.Must be a directed edge to edge to establish.
3.After establishing the whole figure out,跑一边distra就可以了.If the card space time,可以把vectorWith adjacency list.初始化vectorAlso to the least amount of initialization,To the best of this time space optimization.At that time when I ordered to build virtual edge make wrong,傻逼了,Ran slowly.Later must draw a chart,Can't directly go up to,容易出错.
4.当然,This way of building figure can also be like next question,不用维护vectorSave the depth of the point.Directly to their own virtual point and other points to go,这样更快.While less,Points less.
代码:
#include<bits/stdc++.h>
#define ll long long
#define PII pair<ll,int >
#define fi first
#define se second
#define pb push_back
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const ll N = 1e6+50,inf = 1e18;
int T,n,m,k;
int dep[N];
int st,ed;
ll dist[3*N];
bool vis[3*N]; //卡空间,记得关longlong,开bool
int cnt,maxn;
vector<int > v[N];
vector<PII > e[3*N];
void dfs(int now,int p)
{
dep[now] = dep[p]+1;
v[dep[now]].pb(now);
maxn = max(maxn,dep[now]);
for(auto t:e[now])
{
int spot = t.fi;
if(spot==p) continue;
dfs(spot,now);
}
}
void distra()
{
for(int i=1;i<=cnt;i++) dist[i] = inf,vis[i] = 0;
priority_queue<PII,vector<PII>,greater<PII> > q;
q.push({
0,st});
dist[st] = 0;
while(q.size())
{
auto t = q.top();q.pop();
int now = t.se,dis = t.fi;
if(vis[now]) continue;
vis[now] = 1;
for(auto tt:e[now])
{
int spot = tt.fi,w = tt.se;
if(dist[spot]>dist[now]+w)
{
dist[spot] = dist[now]+w;
q.push({
dist[spot],spot});
}
}
}
}
signed main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++) v[i].clear(); //The least amount of initialization
for(int i=1;i<=cnt;i++) e[i].clear(); //The least amount of initialization,用多少,初始化多少
for(int i=1;i<n;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
e[a].pb({
b,c});
e[b].pb({
a,c});
}
scanf("%d%d%d%d",&k,&m,&st,&ed);
dfs(1,0);
cnt = n; //cnt最大3*n
for(int i=1;i<=n;i++)
{
int r = i+k;
if(r>maxn) break; //如果深度为r的没有,就提前退出,The following is not built side,浪费.
cnt++;
for(auto t:v[i]) e[t].pb({
cnt,m});
for(auto t:v[r]) e[cnt].pb({
t,0});
cnt++;
for(auto t:v[r]) e[t].pb({
cnt,m});
for(auto t:v[i]) e[cnt].pb({
t,0});
}
distra();
printf("%lld\n",dist[ed]);
}
return 0;
}
Acwing-NyaFigure the most short circuit
题意:
就是给你n个点,Each point has his layer.然后给你m条边,每条边有个花费.Then point between the adjacent layers can also move to each other,But cost isk.问你从1号点走到n号点的最小花费.
思考:
1.It is clear that this problem is the same,There is point to the adjacent layer built side each other,If violence built side must also timeout.So is established between two layers of virtual point is ok.But submit found or timeout?Stock with adjacency list also not line, on the edge of.
2.Then I doubt deposit layer countvector可能会超时,And built by the side too much?.If I change mode of built figure?对于每个点,His imaginary point is his layer+n.This will only at mostn个虚点,And before one is2n个虚点.对于每个点,Let his own imaginary point even to their own time0,Even around on two points when others spendk.
3.So always establish the edge3n个,Add itself to2m个.也就是51e5个边,2*1e5个点.一定要算好内存,This kind of time and memory card,As far as possible good open minimum.
代码:
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define db double
#define ll long long
#define PII pair<int,int >
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const ll mod = 1e9+7,inf = 1e18;
const int N = 200010,M = 500100;
int T,n,m,k;
int va[N];
ll dist[N];
bool vis[N];
int h[N],e[M],ne[M],w[M],idx;
//h是点的个数,The rest is the number of edge
void add(int a,int b,int c)
{
w[idx] = c;
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
void init()
{
idx = 0;
for(int i=0;i<=2*n;i++) h[i] = -1;
}
void distra()
{
for(int i=1;i<=2*n;i++) dist[i] = inf,vis[i] = 0;
priority_queue<PII,vector<PII>,greater<PII> > q;
q.push({
0,1});
dist[1] = 0;
while(q.size())
{
auto t = q.top();q.pop();
int now = t.se,dis = t.fi;
if(now==n) return ;
if(vis[now]) continue;
vis[now] = 1;
for(int i=h[now];i!=-1;i=ne[i])
{
int spot = e[i],x = w[i];
if(dist[spot]>dist[now]+x)
{
dist[spot] = dist[now]+x;
q.push({
dist[spot],spot});
}
}
}
}
signed main()
{
scanf("%d",&T);
for(int cs=1;cs<=T;cs++)
{
scanf("%d%d%d",&n,&m,&k);
init();
for(int i=1;i<=n;i++) scanf("%d",&va[i]);
for(int i=1;i<=n;i++) //Let his own points to is0,I go to someone else's imaginary point isk
{
int now = va[i]+n;
int l = va[i]-1+n,r = va[i]+1+n;
add(now,i,0);
if(l>n) add(i,l,k); //If the imaginary point exist
if(r>n) add(i,r,k);
}
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
add(b,a,c);
}
distra();
if(dist[n]==inf) dist[n] = -1;
printf("Case #%d: %lld\n",cs,dist[n]);
}
return 0;
}
总结:
多多总结.当出错的时候,考虑清楚,到底是哪里错了,多检查.多多积累经验.
边栏推荐
- sql语句查询String类型字段小于10的怎么查
- How to open a CITIC Securities online account?is it safe?
- docker安装mysql与宿主机相差8小时的问题。
- MRS: Introduction to the use of Alluxio
- 基本表单验证流程
- 张量篇-应用案例
- PHP高级开发案例(1):使用MYSQL语句跨表查询无法导出全部记录的解决方案
- Significant differences between Oracle and Postgresql in PLSQL transaction rollback
- 机器学习模型的“可解释性”
- 【21天学习挑战赛】直接插入排序
猜你喜欢
网络工程师入门必懂华为认证体系,附系统学习路线分享
Polygon zkEVM network node
Reproduce 20-character short domain name bypass
Y86. Chapter iv Prometheus giant monitoring system and the actual combat, Prometheus storage (17)
MySQL query optimization and tuning
8.Haproxy 搭建Web集群
if,case,for,while
帮助企业实现数字化转型成功的八项指导原则
Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch
技术解析|如何将 Pulsar 数据快速且无缝接入 Apache Doris
随机推荐
sql语句查询String类型字段小于10的怎么查
机器学习之视频学习【更新】
七夕节,我用代码制作了表白信封
3000字,一文带你搞懂机器学习!
仿牛客论坛项目梳理
【源码】使用深度学习训练一个游戏
2022年软件测试——精选金融银行面试真题
学会iframe并用其解决跨域问题
Postgresql源码(66)insert on conflict语法介绍与内核执行流程解析
Stop behind.
深度学习之 10 卷积神经网络3
数据治理平台项目总结和分析
烧录场景下开发如何进行源代码保密工作
7. The principle description of LVS load balancing cluster
杭电多校-Slipper-(树图转化+虚点建图)
RSS订阅微信公众号初探-feed43
高效IO模型
JVM Notes
文件系统的简单操作
2022 Hangzhou Electric Power Multi-School League Game 5 Solution