hdu 2583 How far away ? 离线算法 带权求最近距离
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 28961 Accepted Submission(s): 11639
are n houses in the village and some bidirectional roads connecting
them. Every day peole always like to ask like this "How far is it if I
want to go from house A to house B"? Usually it hard to answer. But
luckily int this village the answer is always unique, since the roads
are built in the way that there is a unique simple path("simple" means
you can't visit a place twice) between every two houses. Yout task is to
answer all these curious people.
For
each test case,in the first line there are two numbers
n(2<=n<=40000) and m (1<=m<=200),the number of houses and
the number of queries. The following n-1 lines each consisting three
numbers i,j,k, separated bu a single space, meaning that there is a road
connecting house i and house j,with length k(0<k<=40000).The
houses are labeled from 1 to n.
Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
第一行输入t,表示多个测试样例
第二行输入n,m,表示n个节点和m次询问
接下来n-1行,每行输入x,y,w,表示x到y的距离为w
最后m行每行输入两个数xx,yy,询问xx到yy的最短距离为多少
#include<iostream>
#include<string.h>
#include<vector>
using namespace std;
struct node
{
int id;
int len;
}p;
vector<node>mp[];//结构体动态数组
int dis[],xx[],yy[],vis[],z[],fa[];//dis是存距离,xx,yy存询问的点,z[i]表示i的LCA是点z[i],fa是存父亲节点
int t,n,m;
void add(int a,int b,int c)//加边建树
{
p.id=b;
p.len=c;
mp[a].push_back(p);
}
void init()//初始化
{
for(int i=;i<=n;i++)
{
fa[i]=i;
mp[i].clear();
dis[i]=;
vis[i]=;
z[i]=;
}
}
int find(int x)
{
if(fa[x]!=x)
return fa[x]=find(fa[x]);
return fa[x]; } void join(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)
fa[y]=x;
}
void tarjan(int x)
{
vis[x]=;
for(int i=;i<mp[x].size();i++)
{
int y=mp[x][i].id;
if(!vis[y])//未遍历过
{
dis[y]=dis[x]+mp[x][i].len;//更新距离
tarjan(y);//遍历下一个点
join(x,y);//合并
} }
for(int i=;i<=m;i++)
{
if(xx[i]==x&&vis[yy[i]])
z[i]==find(yy[i]);
if(yy[i]==x&&vis[xx[i]])
z[i]=find(xx[i]);
} }
int main()
{
cin>>t;
while(t--)
{
cin>>n>>m;
int a,b,c;
init();
for(int i=;i<n;i++)//描述边
{
cin>>a>>b>>c;
add(a,b,c);
add(b,a,c);
}
for(int i=;i<=m;i++)//询问
{
cin>>xx[i]>>yy[i];
}
tarjan();
for(int i=;i<=m;i++)
cout<<dis[xx[i]]+dis[yy[i]]-*dis[z[i]]<<endl;
}
return ;
}
hdu 2583 How far away ? 离线算法 带权求最近距离的更多相关文章
- HDU 2255 奔小康赚大钱(带权二分图最大匹配)
HDU 2255 奔小康赚大钱(带权二分图最大匹配) Description 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子. 这可是一件大事,关系到人民的住房问题啊 ...
- HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- HDU 4605 Magic Ball Game(离线算法)
题目链接 思路就很难想+代码实现也很麻烦,知道算法后,已经写的很繁琐而且花了很长时间,200+,好久没写过这么长的代码了. #pragma comment(linker, "/STACK:1 ...
- HDU 2838 (DP+树状数组维护带权排序)
Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...
- hdu 3038 How Many Answers Are Wrong ( 带 权 并 查 集 )
How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 3038 How Many Answers Are Wrong(带权并查集)
太坑人了啊,读入数据a,b,s的时候,我刚开始s用的%lld,给我WA. 实在找不到错误啊,后来不知怎么地突然有个想法,改成%I64d,竟然AC了 思路:我建立一个sum数组,设i的父亲为fa,sum ...
- KM算法 带权二分匹配 O(n^3)
#include<cstdio> #include<cstdlib> #include<cstring> #include<string> #inclu ...
- HDU Virtual Friends(超级经典的带权并查集)
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 奔小康赚大钱---hdu2255(最大带权匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 带权匹配问题的模板: 运用KM算法: #include<stdio.h> #incl ...
随机推荐
- 多个span标签在同一行显示
属性设置为display:inline或display:inline-block
- flutter样式基础
设置padding 1. 可以使用 Padding类设置 Padding( padding: const EdgeInsets.all(8.0), child:, ); 2. Container 参数 ...
- Linux oracle 服务器清理缓存
清理服务器缓存 echo 1 >/proc/sys/vm/drop_cachesecho 2 >/proc/sys/vm/drop_cachesecho 3 >/proc/sys/v ...
- 关于Tomcat部署项目的点点滴滴
在给客户部署环境时,我们不可能想开发一样,在编辑软件部署一下tomcat就可以正常运行.我们也应该清楚java的运行机制**“先编译,后解释”**的原则.(如下图)![图片描述][1]那么在Tomca ...
- Docker registry自签名证书
权威Registry 获取安全证书有两个办法:互联网认证的CA处获取.自建CA自己给自己签名. 1.从认证CA处获取签名证书,大多数是需要付出一定费用的,近些年也有认证CA提供免费证书,例如Let’s ...
- Matplotlib 安装
章节 Matplotlib 安装 Matplotlib 入门 Matplotlib 基本概念 Matplotlib 图形绘制 Matplotlib 多个图形 Matplotlib 其他类型图形 Mat ...
- Mybatis注解开发单表CRUD
Mybatis注解开发单表CRUD mybatis注解开发和xml开发不可兼容,要么全部使用注解,要么全部使用xml,个人建议注解,简单. 当实体类属性名称和数据库表属性名称一致时:无需配置Resul ...
- P1093 字符串A+B
1093 字符串A+B (20分) 给定两个字符串 A 和 B,本题要求你输出 A+B,即两个字符串的并集.要求先输出 A,再输出 B,但重复的字符必须被剔除. 输入格式: 输入在两行中分别给出 ...
- 实验吧——Recursive
环境:win10,kali虚拟机 工具:ida 虚拟机打开看看,发现是ELF文件,运行一下,额没有什么发现. Ida打开看看,发现是在文件内部运行python解释器,百度搜索了一个基本上可以找到Py_ ...
- mysq8设置编码utf8
设置mysql默认编码utf8 以及其他配置 系统:centos7 vi /etc/my.cnf #红色部分如果以存在则在他的下方添加 [mysql] default-character-set=ut ...