Atcoder Beginner Contest 070 D - Transit Tree Path
题意:n个点,n-1条边,组成一个无向的联通图,然后给出q和k,q次询问,每次给出两个点,问这两个点之间的最短距离但必须经过k点。
思路:我当时是用优化的Dijkstra写的(当天刚学的),求出k点到各点的最短距离,跑了160+ms,其实用搜索写更快,组里的几个大佬都用搜索写的,我在搜索这方面还是比较弱的。还要多练练。然后今天早上用搜索写了一下,跑了60+ms,并且内存用的也很小。
题目链接:http://abc070.contest.atcoder.jp/tasks/abc070_d
Dijkstra优先队列优化代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define ll long long
const ll INF=1e15+;
#define MAX 100005
typedef pair<ll,ll> p;
struct Edge{
ll to;
ll cost;
Edge(ll to,ll cost):to(to),cost(cost){}
};
vector<Edge> edge[MAX*];
ll dis[MAX];
ll u,v,w;
ll n,q,x;
void Dijkstra(int s)
{
priority_queue<p,vector<p>,greater<p> > que;
fill(dis+,dis+n+,INF);
dis[s]=;
que.push(p(,s));
while(!que.empty())
{
p P=que.top();
que.pop();
v=P.second;
if(dis[v]<P.first)
continue;
for(ll i=;i<edge[v].size();i++)
{
Edge e=edge[v][i];
if(dis[e.to]>dis[v]+e.cost)
{
dis[e.to]=dis[v]+e.cost;
que.push(p(dis[e.to],e.to));
}
}
}
}
int main()
{
scanf("%lld",&n);
for(ll i=;i<=n-;i++)
{
scanf("%lld%lld%lld",&u,&v,&w);
edge[u].push_back(Edge(v,w));
edge[v].push_back(Edge(u,w));
}
scanf("%lld%lld",&q,&x);
Dijkstra(x);
for(int i=;i<q;i++)
{
scanf("%lld%lld",&u,&v);
printf("%lld\n",dis[u]+dis[v]);
}
return ;
}
DFS搜索:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAX 100005
#define ll long long
struct Edge{
ll to;
ll next;
ll cost;
};
Edge edge[MAX*];
ll head[MAX];
ll dis[MAX];
void DFS(ll u,ll v,ll w)
{
dis[u]=w;
for(ll i=head[u];i!=-;i=edge[i].next)
{
ll to=edge[i].to;
if(to==v)
continue;
DFS(to,u,w+edge[i].cost);
}
return ;
}
int main()
{
ll n,u,v,w,q,k;
scanf("%lld",&n);
ll cnt=;
memset(head,-,sizeof(head));
for(ll i=;i<=n-;i++)
{
scanf("%lld%lld%lld",&u,&v,&w);
edge[cnt].to=v;
edge[cnt].cost=w;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].to=u;
edge[cnt].cost=w;
edge[cnt].next=head[v];
head[v]=cnt++;
}
scanf("%lld%lld",&q,&k);
DFS(k,-,);
while(q--)
{
scanf("%lld%lld",&u,&v);
printf("%lld\n",dis[u]+dis[v]);
}
return ;
}
Atcoder Beginner Contest 070 D - Transit Tree Path的更多相关文章
- AtCoder Beginner Contest 070 ABCD题
题目链接:http://abc070.contest.atcoder.jp/assignments A - Palindromic Number Time limit : 2sec / Memory ...
- AtCoder Beginner Contest 070
我好想有点思维江化,所以我想给这个丝毫没有问题的abc也写下 A - Palindromic Number Time Limit: 2 sec / Memory Limit: 256 MB Score ...
- AtCoder Beginner Contest 070|Elena|8.12|#471
打了场beginner的AtCoder,也是我第一次打AtCoder,虽然AK了,但是由于手速慢+撒比,才#471… 比赛链接:https://beta.atcoder.jp/contests/abc ...
- AtCoder Beginner Contest 133 F Colorful Tree
Colorful Tree 思路: 如果强制在线的化可以用树链剖分. 但这道题不强制在线,那么就可以将询问进行差分,最后dfs时再计算每个答案的修改值, 只要维护两个数组就可以了,分别表示根节点到当前 ...
- AtCoder Beginner Contest 133 E - Virus Tree 2(组合数学)
题意 n个点的树k种颜色,距离不超过2的点对需颜色不同,求方案数 Code(copy) #include<iostream> #include<cstdio> #include ...
- AtCoder Beginner Contest 177 题解
AtCoder Beginner Contest 177 题解 目录 AtCoder Beginner Contest 177 题解 A - Don't be late B - Substring C ...
- AtCoder Beginner Contest 173 题解
AtCoder Beginner Contest 173 题解 目录 AtCoder Beginner Contest 173 题解 A - Payment B - Judge Status Summ ...
- AtCoder Beginner Contest 148 题解
目录 AtCoder Beginner Contest 148 题解 前言 A - Round One 题意 做法 程序 B - Strings with the Same Length 题意 做法 ...
- HUSTOJ:Transit Tree Path
问题 D: Transit Tree Path You are given a tree with N vertices.Here, a tree is a kind of graph, and ...
随机推荐
- 分享一个微信自动跳转外部浏览器下载app的api接口!
现在微信渠道可以说是拉新最快的渠道,因为微信具备强裂变性.但是目前微信对第三方下载链接的拦截是越来越严格了,那么想要在微信内肆无忌惮地推广链接就需要用到微信跳转浏览器的接口,那如何获取该接口呢? ...
- python深拷贝,浅拷贝
1.浅拷贝, 首先要引用copy包 from copy import copy class A: def __init__(self): self.A=1 self.B=2 a = A() copy( ...
- [Flutter] 写第一个 Flutter app,part1 要点
模拟器中调试元素的布局: Android Studio 右侧边栏 Flutter Inspector,选择 Toggle Debug Paint 打开. 格式化代码: 编辑器中右键 Reformat ...
- 移动端ios下H5的:active样式失效的解决方法
在body上绑定一个touchstart事件,空函数就行: document.body.addEventListener('touchstart', function(){}, false) 或者在b ...
- VS code自定义用户代码片段snippet
打开VS code,“文件-首选项-用户代码片段-选择新建全局代码片段文件 “ 属性介绍:prefix 就是你自定义的快捷键 body 就是你自定义的代码片段 description 就是这 ...
- Grafana报警--通知渠道配置
最近研究了prometheus+grafana的系统监控,使用grafana的报警功能,grafana支持很多种通知渠道,下文记录使用到的几种notification channels,分别是emai ...
- 代码详解:TensorFlow Core带你探索深度神经网络“黑匣子”
来源商业新知网,原标题:代码详解:TensorFlow Core带你探索深度神经网络“黑匣子” 想学TensorFlow?先从低阶API开始吧~某种程度而言,它能够帮助我们更好地理解Tensorflo ...
- .Net23种设计模式
C#常见的设计模式 一.概要: 模式分为三种,设计模式.体系结构模式与惯用法.其中惯用法是一种语言紧密相关的模式,例如,定界加锁模式其实是一种惯用法. 在C#项目开发过程中,很多情况下您已经使用了某些 ...
- Win10 for Docker 安装 K8S
win 10 docker安装K8S中遇见的一些问题,记录下来方便自己以后避免采坑. 安装步骤: 1.安装Docker for windows.在docker官方下载,然后傻瓜式安装. 安装成功以后再 ...
- SpringCloud-day05-服务调用Ribbon
6.服务调用Ribbon 6.1Ribbon简介 前面讲了eureka服务注册与发现,但是结合eureka集群的服务调用并没有谈到.这里就要用到Ribbon,结合eureka,来实现服务的调用: Ri ...