[BZOJ2015][Usaco2010 Feb]Chocolate Giving

来源:互联网 发布:万达网络金融 编辑:程序博客网 时间:2024/04/20 03:26

[Usaco2010 Feb]Chocolate Giving

时间限制: 1 Sec 内存限制: 128 MB

题目描述

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至少需要走多远的路程?

输入

第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农场的奶牛。  

输出

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

样例输入

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

样例输出

6
6
10

var w:array[0..200005,1..3]of longint; t:array[0..100005]of longint; dist,p:array[0..50005]of longint; i,j,k:longint; n,m,s:longint; a,b,c:longint; tt,ans,head,tail,x,y:longint;procedure spfa;var a,i:longint;begin a:=1; for i:=1 to n do  dist[i]:=50000000; dist[a]:=0; p[a]:=1; head:=1; tail:=2; t[1]:=a; while head<tail do  begin   x:=head; y:=tail;   for i:=x to y-1 do    begin     tt:=w[t[i],3];     while tt<>0 do      begin       if dist[t[i]]+w[tt,2]<dist[w[tt,1]]       then        begin         dist[w[tt,1]]:=dist[t[i]]+w[tt,2];         if p[w[tt,1]]=0 then begin p[w[tt,1]]:=1; t[tail]:=w[tt,1]; inc(tail); end;        end;       tt:=w[tt,3];      end;     inc(head); p[t[i]]:=0;    end;   x:=head; y:=tail;  end;end;begin readln(n,m,s); w[0,1]:=n+1; for i:=1 to m do  begin   readln(a,b,c);   w[w[0,1],1]:=b; w[w[0,1],2]:=c;   if w[a,3]=0   then w[a,3]:=w[0,1] else w[w[a,1],3]:=w[0,1];   w[a,1]:=w[0,1]; inc(w[0,1]);   w[w[0,1],1]:=a; w[w[0,1],2]:=c;   if w[b,3]=0   then w[b,3]:=w[0,1] else w[w[b,1],3]:=w[0,1];   w[b,1]:=w[0,1]; inc(w[0,1]);  end; spfa; for i:=1 to s do  begin   readln(a,b);   ans:=dist[a]+dist[b];   writeln(ans);  end;end.
0 0
原创粉丝点击