裸最短路..

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

#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. python网络编程-01

    python网络编程 1.socket模块介绍 ①在网络编程中的一个基本组件就是套接字(socket),socket是两个程序之间的“信息通道”. ②套接字包括两个部分:服务器套接字.客户机套接字 ③ ...

  2. MySQL my.cnf 参数说明

    MySQL 5.5.13 参数说明: [client] character-set-server = utf8 port    = 3306 socket  = /data/mysql/3306/my ...

  3. Linux系统管理员:不要害怕升级内核

    Linux系统管理员平时很重要的一项工作就是负责系统内核升级.做好系统内核的升级工作,对于Linux系 统的稳定性具有至关重要的作用.但是很少有人敢贸然的对Linux系统的内核进行升级,担心会影响现有 ...

  4. liunx 同步时间

    ntpdate stdtime.gov.hk (美国) time.nist.gov (复旦)(国内用户推荐) ntp.fudan.edu.cn 微软公司授时主机(美国) time.windows.co ...

  5. CCNA实验(6) -- VLAN & SPT

    交换机的作用主要有两个:1.维护CAM(ContextAddress Memory)表,该表是MAC地址和交换机端口的映射表2.根据CAM进行数据帧的转发 交换机对数据帧的处理有三种:1.Forwar ...

  6. HDU 4957 Poor Mitsui

    题解:记答案为ans,已知,对一个确定的顺序,计算所用的时间长短就是从最后向前计算,计算方法如下: ans+=(p[i].b+ans*p[i].a)/(v-p[i].a) 那么,应该如何调整顺序使得答 ...

  7. C++读写文件的简单例子

    #include <iostream> #include <fstream> using namespace std; void main() { ofstream in; i ...

  8. hdu2141

    题目大意:输入三系列数A,B,C,输入一个数X,问是否在A,B,C中存在A[i]+B[j]+C[k]=X,存在输出YES,不存在输出NO. 本题若果采用暴力法那么复杂度为O(n3)显然会超时,如果把A ...

  9. 转帖Jmeter中的几个重要测试指标释义

    Aggregate Report 是 JMeter 常用的一个 Listener,中文被翻译为“聚合报告”.今天再次有同行问到这个报告中的各项数据表示什么意思,顺便在这里公布一下,以备大家查阅. 如果 ...

  10. android中像素单位dp、px、pt、sp的比较

    dp(dip): device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不依赖 ...