BNUOJ 2105 Distance Queries
Distance Queries
This problem will be judged on PKU. Original ID: 1986
64-bit integer IO format: %lld Java class name: Main
Input
* Line 2+M: A single integer, K. 1 <= K <= 10,000
* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.
Output
Sample Input
7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6
Sample Output
13
3
36
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,w;
};
struct query{
int to,id;
};
int n,m,k,ans[maxn],d[maxn],uf[maxn];
bool vis[maxn];
vector<arc>g[maxn];
vector<query>q[maxn];
int Find(int x){
if(x != uf[x])
uf[x] = Find(uf[x]);
return uf[x];
}
void tarjan(int u,int ds){
vis[u] = true;
d[u] = ds;
uf[u] = u;
int i;
for(i = ; i < g[u].size(); i++){
if(!vis[g[u][i].to]) {tarjan(g[u][i].to,ds+g[u][i].w);uf[g[u][i].to] = u;}
}
for(i = ; i < q[u].size(); i++)
if(vis[q[u][i].to]){
ans[q[u][i].id] = d[u]+d[q[u][i].to]-*d[Find(q[u][i].to)];
}
}
int main(){
int i,j,u,v,w;
char ch;
while(~scanf("%d %d",&n,&m)){
for(i = ; i <= n; i++){
g[i].clear();
q[i].clear();
d[i] = ;
vis[i] = false;
}
for(i = ; i < m; i++){
scanf("%d %d %d %c",&u,&v,&w,&ch);
g[u].push_back((arc){v,w});
g[v].push_back((arc){u,w});
}
scanf("%d",&k);
for(i = ; i < k; i++){
scanf("%d %d",&u,&v);
q[u].push_back((query){v,i});
q[v].push_back((query){u,i});
}
tarjan(,);
for(i = ; i < k; i++)
printf("%d\n",ans[i]);
}
return ;
}
BNUOJ 2105 Distance Queries的更多相关文章
- POJ1986 Distance Queries (LCA)(倍增)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12950 Accepted: 4577 ...
- POJ 1986 Distance Queries(Tarjan离线法求LCA)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12846 Accepted: 4552 ...
- poj 1986 Distance Queries LCA
题目链接:http://poj.org/problem?id=1986 Farmer John's cows refused to run in his marathon since he chose ...
- poj 1986 Distance Queries(LCA)
Description Farmer John's cows refused to run in his marathon since he chose a path much too long fo ...
- 【LCA求最近公共祖先+vector构图】Distance Queries
Distance Queries 时间限制: 1 Sec 内存限制: 128 MB 题目描述 约翰的奶牛们拒绝跑他的马拉松,因为她们悠闲的生活不能承受他选择的长长的赛道.因此他决心找一条更合理的赛道 ...
- POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)
POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...
- POJ 1986 Distance Queries(LCA Tarjan法)
Distance Queries [题目链接]Distance Queries [题目类型]LCA Tarjan法 &题意: 输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面 ...
- POJ 1986 - Distance Queries - [LCA模板题][Tarjan-LCA算法]
题目链接:http://poj.org/problem?id=1986 Description Farmer John's cows refused to run in his marathon si ...
- poj 1986 Distance Queries 带权lca 模版题
Distance Queries Description Farmer John's cows refused to run in his marathon since he chose a pa ...
随机推荐
- 导出数据库报错 EXP-00002: 写入导出文件时出错 EXP-00000: 导出终止失败
解决方法: 1.检查磁盘所在空间是否够用. 2.磁盘修复下 排除故障考虑的地方要全面啊.
- 转 SQLPLUS中SQL换行执行
权声明:本文为博主原创文章,未经博主允许不得转载. 正常情况下,在SQLPLUS中输入命令时,可以换行,但不能有空格,否则不能执行,会直接返回到SQL>下.但通过命令设置可以实现语句换行时允许有 ...
- 安装SNMP
http://songknight.blog.51cto.com/2599480/655337
- WAMP配置虚拟目录
1.启动wamp所有服务,输入localhost或localhost:端口号确保wamp环境正常无误. 2.设置httpd.conf 2.1打开文件:单击wamp在电脑右下角的图标=>wamp= ...
- greendao3.2.3配置时遇到的问题
这两天我一直在研究greendao这个框架,我在GitHub下载了 greendao3.2.2:https://github.com/greenrobot/greenDAO,照着网址里面来配置: // ...
- 5 Transforms 转移 笔记
5 Transforms 转移 笔记 Transforms Unfortunately, no one can be told what the Matrix is. You have to ...
- 读取Java文件到byte数组的三种方式
package zs; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io ...
- webpack2代码分割
代码分割-CSS 要通过webpack打包CSS,像任何其他模块一样将CSS导入JavaScript代码,并使用css-loader(它输出CSS作为JS模块), 并可选地应用ExtractTextW ...
- Java多线程编程核心技术---Lock的基本概念和使用
Lock接口: ReentrantLock的基本功能: ReentrantLock的lock和unlock方法进行加锁,解锁.可以起到和synchronized关键字一样的效果: 选择性通知!!!: ...
- Activiti数据库表结构(表详细版)
http://blog.csdn.net/hj7jay/article/details/51302829 1 Activiti数据库表结构 1.1 数据库表名说明 Activiti工作流总 ...