裸最短路..
------------------------------------------------------------------------------------
#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