裸最短路..

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

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
 
#define rep( i , n ) for( int i = 0 ; i < n ; i++ )
#define clr( x , c ) memset( x , c , sizeof( x ) )
 
using namespace std;
 
const int maxn = 50000 + 5;
const int maxm = 100000 + 5;
const int inf = 0x3f3f3f3f;
 
struct edge {
int to , dist;
edge* next;
};
 
edge* pt;
edge* head[ maxn ];
edge EDGE[ maxm << 1 ];
 
void init() {
pt = EDGE;
clr( head , 0 );
}
 
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 )
 
struct Node {
int x , d;
bool operator < ( const Node &o ) const {
return d > o.d;
}
};
 
priority_queue< Node > Q;
int d[ maxn ];
 
void dijkstra( int S ) {
clr( d , inf );
d[ S ] = 0;
Q.push( ( Node ) { S , 0 } );
while ( ! Q.empty() ) {
Node o = Q.top();
Q.pop();
int x = o.x , dist = o.d;
if( d[ x ] != dist ) 
   continue;
for( edge* e = head[ x ] ; e ; e = e -> next ) {
int to = e -> to;
if( d[ to ] > dist + e -> dist ) {
d[ to ] = dist + e -> dist;
Q.push( ( Node ) { to , d[ to ] } );
}
}
}
}
int main() {
// freopen( "test.in" , "r" , stdin );
init();
int n , m , b;
cin >> n >> m >> b;
while( m-- ) {
int u , v , w;
scanf( "%d%d%d" , &u , &v , &w );
--u , --v;
add_edge( u , v , w );
}
dijkstra( 0 );
while( b-- ) {
int u , v;
scanf( "%d%d" , &u , &v );
u-- , v--;
printf( "%d\n" , d[ u ] + d[ v ] );
}
return 0;
}

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

2015: [Usaco2010 Feb]Chocolate Giving

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 274  Solved: 188
[Submit][Status][Discuss]

Description

Farmer John有B头奶牛(1<=B<=25000),有N(2*B<=N<=50000)个农场,编号1-N,有M(N-1<=M<=100000)条双向边,第i条边连接农场R_i和S_i(1<=R_i<=N;1<=S_i<=N),该边的长度是L_i(1<=L_i<=2000)。居住在农场P_i的奶牛A(1<=P_i<=N),它想送一份新年礼物给居住在农场Q_i(1<=Q_i<=N)的奶牛B,但是奶牛A必须先到FJ(居住在编号1的农场)那里取礼物,然后再送给奶牛B。你的任务是:奶牛A至少需要走多远的路程?

Input

  第1行:三个整数:N,M,B。

 第2..M+1行:每行三个整数:R_i,S_i和L_i,描述一条边的信息。

  第M+2..M+B+1行:共B行,每行两个整数P_i和Q_i,表示住在P_i农场的奶牛送礼物给住在Q_i农场的奶牛。

  

Output

  样例输出:

  共B行,每行一个整数,表示住在P_i农场的奶牛送礼给住在Q_i农场的奶牛至少需要走的路程

 

Sample Input

6 7 3

  1 2 3

  5 4 3

  3 1 1

  6 1 9

  3 4 2

  1 4 4

  3 2 2

  2 4

  5 1

  3 6

Sample Output

 6

 6

10

HINT

Source

BZOJ 2015: [Usaco2010 Feb]Chocolate Giving( 最短路 )的更多相关文章

  1. bzoj 2015: [Usaco2010 Feb]Chocolate Giving【spfa】

    因为是双向边,所以相当于两条到1的最短路和,先跑spfa然后直接处理询问即可 #include<iostream> #include<cstdio> #include<q ...

  2. 2015: [Usaco2010 Feb]Chocolate Giving

    2015: [Usaco2010 Feb]Chocolate Giving Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 269  Solved: 1 ...

  3. 【BZOJ】2015: [Usaco2010 Feb]Chocolate Giving(spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2015 这种水题真没啥好说的.. #include <cstdio> #include & ...

  4. bzoj2015 [Usaco2010 Feb]Chocolate Giving

    Description Farmer John有B头奶牛(1<=B<=25000),有N(2*B<=N<=50000)个农场,编号1-N,有M(N-1<=M<=10 ...

  5. [bzoj 1782] [Usaco2010 Feb]slowdown慢慢游

    [bzoj 1782] [Usaco2010 Feb]slowdown慢慢游 Description 每天Farmer John的N头奶牛(1 <= N <= 100000,编号1-N)从 ...

  6. BZOJ 1782: [Usaco2010 Feb]slowdown 慢慢游( BIT + dfs )

    orz...hzwer 对着大神的 code 看 , 稍微理解了. 考虑一只牛到达 , 那它所在子树全部 +1 , 可以用BIT维护 --------------------------------- ...

  7. 【BZOJ】2014: [Usaco2010 Feb]Chocolate Buying(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2014 这应该是显然的贪心吧,先排序,然后按花费取 #include <cstdio> # ...

  8. BZOJ 2100: [Usaco2010 Dec]Apple Delivery( 最短路 )

    跑两遍最短路就好了.. 话说这翻译2333 ---------------------------------------------------------------------- #includ ...

  9. BZOJ 1631: [Usaco2007 Feb]Cow Party( 最短路 )

    这道题和蔡大神出的今年STOI初中组的第二题几乎一模一样... 先跑一遍最短路 , 再把所有边反向 , 再跑一遍 , 所有点两次相加的最大值即为answer --------------------- ...

随机推荐

  1. 解决:eclipse导入android时工程下没有R文件的问题,以及style.xml文件报错

    解决:eclipse导入android时工程下没有R文件的问题,以及style.xml文件报错

  2. 树型动态规划(树形dp)

    树型动态规划就是在“树”的数据结构上的动态规划,树型动态规划是建立在树上的,所以有二个方向: 1.根—>叶:这种题目基本上碰不到 2.叶->根:根的子节点传递有用的信息给根,完后根得出最优 ...

  3. centos6.4-x86-64系统更新系统自带Apache Http Server

    系统自带Apache Http Server 版本比较老,有漏洞.现在对Apache Http Server进行升级.总体思路:先删除老的,再安装新的.详细步骤如下: 1 删除老版本 1.1 删除老A ...

  4. Oracle的TPCC测试,原来也是个作弊的东西...

    http://www.oaktable.net/content/sorted-hash-clusters-rip 根据Jonathan Lewis老先生的测试实例,发觉cluster 的sort功能, ...

  5. Dialog with HTML skin using CDHtmlDialog and SetWindowRgn

    Introduction This program demonstrates how to use CDHtmlDialog and SetWindowRgn functions to give a ...

  6. vs2010 项目编译 ResGen.exe”已退出,代码为 -1073741701

    解决的办法如下: 1.关闭所有Visual Studio: 2.以管理员的身份打开命令提示窗口:(开始-运行-cmd) 3.更改目录至"CD C:\Program Files (x86)\M ...

  7. ExtJs004define定义类

    Ext.onReady(function () { //在Ext中如何去定义一个类: Ext.define(className , properties , callback) Ext.define( ...

  8. C++知识点整理——持续更新

    virtual是C++的一个关键字,virtual修饰的函数可以被子类重写.   用法:在返回值类型的前面添加关键字即可. override是C++的保留字(注意不是关键字),表示当前函数重写了基类的 ...

  9. ie6,ie7下设置overflow:auto下滚动条不起作用

    今天遇到一个比较特殊的情况:ie6,ie7下设置overflow:auto下滚动条出来了但是滚动条不起任何作用,但在火狐,ie8,ie9,谷歌等浏览器下正常显示,解决方案:只需要加一个position ...

  10. 02-C语言执行过程

    目录: 一.MACOS系统操作 二.C语言的使用方式 三.编码 四.编译 五.运行 六.分析第一个C程序 七.预处理指令#include 八.完整执行过程 回到顶部 一.MACOS系统操作 操作计算机 ...