xtu summer individual 1 C - Design the city
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Description
Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now, Cerror finds out that the main reason of them is the poor design of the roads distribution, and he want to change this situation.
In order to achieve this project, he divide the city up to N regions which can be viewed as separate points. He thinks that the best design is the one that connect all region with shortest road, and he is asking you to check some of his designs.
Now, he gives you an acyclic graph representing his road design, you need to find out the shortest path to connect some group of three regions.
Input
The input contains multiple test cases! In each case, the first line contian a interger N (1 < N < 50000), indicating the number of regions, which are indexed from 0 to N-1. In each of the following N-1 lines, there are three interger Ai, Bi, Li (1 < Li < 100) indicating there's a road with length Li between region Ai and region Bi. Then an interger Q (1 < Q < 70000), the number of group of regions you need to check. Then in each of the following Q lines, there are three interger Xi, Yi, Zi, indicating the indices of the three regions to be checked.
Process to the end of file.
Output
Q lines for each test case. In each line output an interger indicating the minimum length of path to connect the three regions.
Output a blank line between each test cases.
Sample Input
4
0 1 1
0 2 1
0 3 1
2
1 2 3
0 1 2
5
0 1 1
0 2 1
1 3 1
1 4 1
2
0 1 2
1 0 3
Sample Output
3
2 2
2 解题:LCA算法
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxv = ;
const int maxn = ;
struct arc{
int to,w;
};
struct Q{
int index,v;
};
vector<arc>g[maxv];
vector<Q>qy[maxn];
int ans[maxn],uf[maxv],dis[maxv],n,m;
bool vis[maxv];
int _find(int x){
if(uf[x] != x)
uf[x] = _find(uf[x]);
return uf[x];
}
void dfs(int u){
vis[u] = true;
uf[u] = u;
int i,j,k,v;
for(i = ; i < qy[u].size(); i++){
v = qy[u][i].v;
if(vis[v])
ans[qy[u][i].index] += dis[u]+dis[v]-*dis[_find(v)];
}
for(i = ; i < g[u].size(); i++){
v = g[u][i].to;
if(!vis[v]){
dis[v] = dis[u]+g[u][i].w;
dfs(v);
uf[v] = u;
}
}
}
int main(){
int i,j,u,v,w,t = ;
while(~scanf("%d",&n)){
if(t) printf("\n");
for(i = ; i <= n; i++){
g[i].clear();
qy[i].clear();
uf[i] = i;
vis[i] = false;
}
for(i = ; i < n; i++){
scanf("%d%d%d",&u,&v,&w);
g[u].push_back((arc){v,w});
g[v].push_back((arc){u,w});
}
scanf("%d",&m);
for(i = ; i <= m; i++){
scanf("%d %d %d",&u,&v,&w);
qy[u].push_back((Q){i,v});
qy[v].push_back((Q){i,u});
qy[u].push_back((Q){i,w});
qy[w].push_back((Q){i,u});
qy[v].push_back((Q){i,w});
qy[w].push_back((Q){i,v});
}
memset(ans,,sizeof(ans));
dis[] = ;
dfs();
for(i = ; i <= m; i++)
printf("%d\n",ans[i]>>);
t++;
}
return ;
}
xtu summer individual 1 C - Design the city的更多相关文章
- zoj——3195 Design the city
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- ZOJ Design the city LCA转RMQ
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- ZOJ3195 Design the city [2017年6月计划 树上问题04]
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- ZOJ 3195 Design the city (LCA 模板题)
Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terribl ...
- xtu summer individual 3 F - Opening Portals
Opening Portals Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 6 D - Checkposts
Checkposts Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Orig ...
- ZOJ3195 Design the city(LCA)
题目大概说给一棵树,每次询问三个点,问要把三个点连在一起的最少边权和是多少. 分几种情况..三个点LCA都相同,三个点有两对的LCA是某一点,三个点有两对的LCA各不相同...%……¥…… 画画图可以 ...
- ZOJ 3195 Design the city LCA转RMQ
题意:给定n个点,下面n-1行 u , v ,dis 表示一条无向边和边权值,这里给了一颗无向树 下面m表示m个询问,问 u v n 三点最短距离 典型的LCA转RMQ #include<std ...
- ZOJ - 3195 Design the city
题目要对每次询问将一个树形图的三个点连接,输出最短距离. 利用tarjan离线算法,算出每次询问的任意两个点的最短公共祖先,并在dfs过程中求出离根的距离.把每次询问的三个点两两求出最短距离,这样最终 ...
随机推荐
- DFS水题 URAL 1152 False Mirrors
题目传送门 /* 题意:一个圈,每个点有怪兽,每一次射击能消灭它左右和自己,剩余的每只怪兽攻击 搜索水题:sum记录剩余的攻击总和,tot记录承受的伤害,当伤害超过ans时,结束,算是剪枝吧 回溯写挫 ...
- ACM_求第k大元素(两次二分)
求第k大 Time Limit: 6000/3000ms (Java/Others) Problem Description: 给定两个数组A和B,大小为N,M,每次从两个数组各取一个数相乘放入数组C ...
- 生产环境中配置的samba
实验需求: 由于实验室纳新一帮新孩子,搭建samba主要是临时共享一些学习资源的,基本上大家用的都是windows.而且这个服务可以让他们在校园的里的个个角落都可以访问到,所以给挂了学校的公网,不过我 ...
- Ionic之增加样式会自动换行解决方案
设置样式的时候,引用自身的样式,能正常显示,但是引用自定义样式显示的时候,竟然或自动换行,好尴尬. 原本代码: $('.codeSuccess').css({'display':'block'}); ...
- C#中Json的简单处理
命名空间:Windows.Data.Json在Windows Runtime中,可以使用Json类对获取的Json字符串进行操作,相比DataContractJsonSerializer类操作更加直观 ...
- Jquery 中使用String.Format
第一种方法: String.format = function() { if (arguments.length == 0) return null; var str = arguments[0]; ...
- AndroidStudio碰到的各种问题
源码已经下载了,但是为毛关联不了? 我的源码默认是下载在Sdk\sources\android-23\目录下面的,以前开发的时候都是自动关联的,今天碰到了怎么刷新,怎么关联都不行. 解决方式为: 1. ...
- 网页尺寸scrollHeight/offsetHeight
scrollHeight和scrollWidth,获取网页内容高度和宽度. 一.针对IE.Opera: scrollHeight 是网页内容实际高度,可以小于 clientHeight. 二.针对NS ...
- 【经验总结】OSG 安装配置
对于普通用户推荐直接下载安装包配置.如有特殊需求或想了解编译过程可参考网上文章自己编译后配置.(通常建议使用第一种方法即可) 本人安装经验: 失败:自己系统64位,VS2010 32位,开始自己动手编 ...
- ssl证书过期问题解决
1,ssl证书失效现象 小程序debug有如下证书无效信息: 浏览器访问https://ic-park.net:30001/indoornav/callFunction1.php 提示证书风险. 2, ...