裸最短路..

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

#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. android HTTP发送及MD5加密收集

    发送部分: public void MyFunction{ HttpClient httpclient = new DefaultHttpClient(); //你的URL HttpPost http ...

  2. Sharpui企业版-简单介绍【01】 【QQ群:206378966】

    1. 什么是Sharpui         Sharpui是居于DUI思想的一套界面引擎,採用纯c++开发,实现完整的消息处理机制.渲染引擎分离以及灵活的控件扩展,是一款c++真正意义上的界面和业务逻 ...

  3. No orientation specified, and the default is

    链接地址:http://jingyan.baidu.com/article/a24b33cd7722dc19fe002bd0.html No orientation specified, and th ...

  4. HDU1171-Big Event in HDU

    描述: Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don ...

  5. 解决python2.7.9以下版本requests访问https的问题

    在python2.7.9以下版本requests访问https连接后,总会报一些关于SSL warning. 解决法子可以参考:https://urllib3.readthedocs.io/en/la ...

  6. java集合分析(转载)

    参考文章:浅谈Java中的Set.List.Map的区别 Java 7 Collections详解 java中集合分为三类: Set(集) List(列表) Map(映射) Set和List继承自Co ...

  7. selenium + python自动化测试环境搭建--亲测

    环境准备: 1.下载所学安装包: setuptools https://pypi.python.org/packages/2.7/s/setuptools/ selenium https://pypi ...

  8. MVC-03 控制器(1)

    Controller(控制器)在ASP.NET MVC中负责控制所有客户端与服务器端的交互,并且负责协调Model与View之间的数据传递,是ASP.NET MVC整体运作的核心角色. 一.关于Con ...

  9. Oracle不能导入空表解决方案

    C:\Users\Administrator>sqlplus / as sysdba SQL*Plus: Release 11.2.0.1.0 Production on 星期日 8月 17 1 ...

  10. TreeView控件例子

    XmL文件代码: <?xml version="1.0" encoding="utf-8" ?> <Area> <Province ...