一棵树..或许用LCA比较好吧...但是我懒...写了个dijkstra也过了..

----------------------------------------------------------------------------

#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<iostream>
 
#define rep( i , n ) for( int i = 0 ; i < n ; ++i )
#define clr( x , c ) memset( x , c , sizeof( x ) )
 
using namespace std;
 
typedef pair< int , int > pii;
 
const int maxn = 1000 + 5;
const int inf = 0x3f3f3f3f;
 
struct edge {
int to , dist;
edge* next;
};
 
edge* pt;
edge* head[ maxn ];
edge EDGE[ maxn << 1 ];
 
void init() {
pt = EDGE;
clr( head , 0 );
 
inline void add( int u , int v , int d ) {
pt -> to = v;
pt -> dist = d;
pt -> next = head[ u ];
head[ u ] = pt++;
}
 
#define add_edge( u , v , d ) add( u , v , d ) , add( v , u , d )
 
int d[ maxn ][ maxn ];
 
void dijkstra( int S ) {
clr( d[ S ] , inf );
d[ S ][ S ] = 0;
priority_queue< pii , vector< pii > , greater< pii > > Q;
Q.push( make_pair( 0 , S ) );
while( ! Q.empty() ) {
int x = Q.top().second , dist = Q.top().first;
Q.pop();
if( dist != d[ S ][ x ] ) continue;
for( edge* e = head[ x ] ; e ; e = e -> next )
   if( d[ S ][ e -> to ] > dist + e -> dist ) {
   
    d[ S ][ e -> to ] = dist + e -> dist;
   
    Q.push( make_pair( d[ S ][ e -> to ] , e -> to ) );
   
   }
   
}
}
 
int main() {
init();
int n , q;
cin >> n >> q;
rep( i , n - 1 ) {
int u , v , d;
scanf( "%d%d%d" , &u , &v , &d );
add_edge( u - 1 , v - 1 , d );
}
rep( i , n ) dijkstra( i );
while( q-- ) {
int u , v;
scanf( "%d%d" , &u , &v );
printf( "%d\n" , d[ u - 1 ][ v - 1 ] );
}
return 0;
}

----------------------------------------------------------------------------

1602: [Usaco2008 Oct]牧场行走

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 1351  Solved: 684
[Submit][Status][Discuss]

Description

N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草。 这n块土地被n-1条边连接。 奶牛可以在边上行走,第i条边连接第Ai,Bi块牧场,第i条边的长度是Li(1<=Li<=10000)。 这些边被安排成任意两头奶牛都可以通过这些边到达的情况,所以说这是一棵树。 这些奶牛是非常喜欢交际的,经常会去互相访问,他们想让你去帮助他们计算Q(1<=q<=1000)对奶牛之间的距离。

Input

*第一行:两个被空格隔开的整数:N和Q

*第二行到第n行:第i+1行有两个被空格隔开的整数:AI,BI,LI

*第n+1行到n+Q行:每一行有两个空格隔开的整数:P1,P2,表示两头奶牛的编号。

Output

*第1行到第Q行:每行输出一个数,表示那两头奶牛之间的距离。

Sample Input

4 2
2 1 2
4 3 2
1 4 3
1 2
3 2

Sample Output

2
7

HINT

Source

BZOJ 1602: [Usaco2008 Oct]牧场行走( 最短路 )的更多相关文章

  1. bzoj 1602 [Usaco2008 Oct]牧场行走(LCA模板)

    1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 379  Solved: 216[Submit][Sta ...

  2. BZOJ——1602: [Usaco2008 Oct]牧场行走 || 洛谷—— P2912 [USACO08OCT]牧场散步Pasture Walking

    http://www.lydsy.com/JudgeOnline/problem.php?id=1602 || https://www.luogu.org/problem/show?pid=2912 ...

  3. BZOJ 1602: [Usaco2008 Oct]牧场行走 倍增裸题

    Description N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草. 这n块土地被n-1条边连接. 奶牛可以在边上行走, ...

  4. LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking

    题面:[USACO08OCT]牧场散步Pasture Walking 题解:LCA模版题 代码: #include<cstdio> #include<cstring> #inc ...

  5. BZOJ 1602 [Usaco2008 Oct]牧场行走 dfs

    题意:id=1602">链接 方法:深搜暴力 解析: 这题刚看完还有点意思,没看范围前想了想树形DP,只是随便画个图看出来是没法DP的,所以去看范围. woc我没看错范围?果断n^2暴 ...

  6. BZOJ 1602 USACO2008 Oct 牧场行走

    翻翻吴大神的刷题记录翻到的... 乍一看是一个树链剖分吓瓜我...难不成吴大神14-10-28就会了树剖?orz... 再一看SB暴力都可过... 然后一看直接树上倍增码个就好了... 人生真是充满着 ...

  7. bzoj 1602: [Usaco2008 Oct]牧场行走【瞎搞】

    本来想爆手速写个树剖,然而快下课了就手残写了了个n方的短小-- 暴力把查询的两个点中深的一个跳上来,加上边权,然后一起跳加边权就行了 #include<iostream> #include ...

  8. 1602: [Usaco2008 Oct]牧场行走

    1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1211  Solved: 616 [Submit][ ...

  9. 【BZOJ】1602: [Usaco2008 Oct]牧场行走(lca)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1602 一开始以为直接暴力最短路,但是n<=1000, q<=1000可能会tle. 显然 ...

随机推荐

  1. HDU 5716 带可选字符的多字符串匹配(ShiftAnd)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5716 [题目大意] 给出一个字符串,找出其中所有的符合特定模式的子串位置,符合特定模式是指,该子串 ...

  2. poj2013---二维数组指针使用

    #include <stdio.h> #include <stdlib.h> #include<string.h> int main() { ; ][],arr2[ ...

  3. poj1580---欧几里得算法(辗转相除法)

    #include<stdio.h> #include<string.h> #include<string.h> ],str2[]; int len; int cal ...

  4. openStack use

    <1,project security> security groyps Security groups--> are sets of IP filter rules() that ...

  5. 如何制作一个类似Tiny Wings的游戏(2) Cocos2d-x 2.1.4

    在第二篇<如何制作一个类似Tiny Wings的游戏>基础上,增加添加主角,并且使用Box2D来模拟主角移动,原文<How To Create A Game Like Tiny Wi ...

  6. USB OTG简单介绍

    1 引言 随着USB2.0版本号的公布,USB越来越流行,已经成为一种标准接口.如今,USB支持三种传输速率:低速(1.5Mb/s).全速(12Mb/s)和快速(480Mb/s),四种传输类型:块传输 ...

  7. a中国天气网pi(json格式)

    http://m.weather.com.cn/data/101050101.html 此接口的回报格式例如以下 { "weatherinfo": { "city&quo ...

  8. asp.net ToString()方法介绍

      C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToStri ...

  9. svm评价指标公式

    在做svm分类试验时,对于结果的处理,仅用一种指标很难得到正确评估算法的效果.所以,一般要用到precision(精确率),recall(召回率),F-measure.accuracy(准确率)四个指 ...

  10. 一键搞定Java桌面应用安装部署 —— exe4j + Inno Setup 带着JRE, 8M起飞

    转载自:http://www.blogjava.net/huliqing/archive/2008/04/18/193907.html 对于作Java桌面应用来说,比较烦人的就是安装部署问题,客户端是 ...