题意: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的更多相关文章

  1. AtCoder Beginner Contest 070 ABCD题

    题目链接:http://abc070.contest.atcoder.jp/assignments A - Palindromic Number Time limit : 2sec / Memory ...

  2. AtCoder Beginner Contest 070

    我好想有点思维江化,所以我想给这个丝毫没有问题的abc也写下 A - Palindromic Number Time Limit: 2 sec / Memory Limit: 256 MB Score ...

  3. AtCoder Beginner Contest 070|Elena|8.12|#471

    打了场beginner的AtCoder,也是我第一次打AtCoder,虽然AK了,但是由于手速慢+撒比,才#471… 比赛链接:https://beta.atcoder.jp/contests/abc ...

  4. AtCoder Beginner Contest 133 F Colorful Tree

    Colorful Tree 思路: 如果强制在线的化可以用树链剖分. 但这道题不强制在线,那么就可以将询问进行差分,最后dfs时再计算每个答案的修改值, 只要维护两个数组就可以了,分别表示根节点到当前 ...

  5. AtCoder Beginner Contest 133 E - Virus Tree 2(组合数学)

    题意 n个点的树k种颜色,距离不超过2的点对需颜色不同,求方案数 Code(copy) #include<iostream> #include<cstdio> #include ...

  6. AtCoder Beginner Contest 177 题解

    AtCoder Beginner Contest 177 题解 目录 AtCoder Beginner Contest 177 题解 A - Don't be late B - Substring C ...

  7. AtCoder Beginner Contest 173 题解

    AtCoder Beginner Contest 173 题解 目录 AtCoder Beginner Contest 173 题解 A - Payment B - Judge Status Summ ...

  8. AtCoder Beginner Contest 148 题解

    目录 AtCoder Beginner Contest 148 题解 前言 A - Round One 题意 做法 程序 B - Strings with the Same Length 题意 做法 ...

  9. 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 ...

随机推荐

  1. python大法好——多线程

    Python 多线程 多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件 ...

  2. Django07-cookie和session

    一.Cookie 1.Cookie的由来 大家都知道HTTP协议是无状态的.无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响 ...

  3. matplotlib.mlab库的重要函数

    连接地址 matplotlib.mlab¶ 与 MATLAB兼容的函数 MATLAB compatible functions¶ cohere() Coherence (normalized cros ...

  4. python中线程2

    cpython中的GIL和pool GIL锁(全局解释器锁) 1.what? GIL是全局解释器锁,和普通锁加在数据上不同的是:GIL加在加在解释器上,是为了防止多个线程在同一时间执行python字节 ...

  5. mkdir npm

    mkdir -p 创建目录 没有就创建 有就用 npm init -y 不提示创建package.json 文件

  6. Loadrunner回放https脚本时出现错误Error -27780 Connection reset by peer解决办法

    录制好的https协议的web脚本,在脚本回放时会出现Error -27780: [GENERAL_MSG_CAT_SSL_ERROR]connect to host "......&quo ...

  7. https://github.com/gaoyangxiaozhu/DockerVI

    [更新]分享一个开源项目DockerVI,一个基于NodeJS实现的Docker Swarm可视化解决方案

  8. 【博客开篇】服务器配置:Windows2008R2+PHP5.6+SQLServer2008(X64)

    现下流行LAMP,如果选择Windows服务器,那么一般都会选择IIS+Asp.Net+SQL Server(可以简称为WINS),这些配置起来,都是非常方便的. 但也有一些特殊的服务器配置,例如:W ...

  9. Game Engine Architecture 4

    [Game Engine Architecture 4] 1.a model of multiple semi-independent flows of control simply matches ...

  10. C++/CLI

    [C++/CLI] A C++/CLI application or component uses extensions to C++ syntax (as allowed by the C++ Sp ...