2015: [Usaco2010 Feb]Chocolate Giving
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 428 Solved: 273
[ 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
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
6
10
HINT
Source
最短路
练练dij
附代码:
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<climits>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define N 50005
#define M 200002
#define inf 0x3f3f3f3f
using namespace std;
int n,m,b;
int head[N],pos;
struct edge{int to,next,c;}e[M];
void add(int x,int y,int z)
{
pos++;
e[pos].to=y;
e[pos].next=head[x];
e[pos].c=z;
head[x]=pos;
}
struct node{int u,d;}s,t;
int d[N];bool done[N];
bool operator < (node a,node b){return a.d>b.d;}
priority_queue<node>Q;
void dij()
{
for(int i=1;i<=n;i++)d[i]=inf,done[i]=0;
d[1]=0;s.u=1,s.d=0;Q.push(s);
while(!Q.empty())
{
s=Q.top();Q.pop();
if(done[s.u])continue;done[s.u]=1;
for(int i=head[s.u];i;i=e[i].next)
{
int v=e[i].to;
if(d[v]>s.d+e[i].c)
{
d[v]=d[s.u]+e[i].c;
t.u=v,t.d=d[v];
Q.push(t);
}
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&b);
for(int i=1;i<=m;i++)
{
int xx,yy,zz;
scanf("%d%d%d",&xx,&yy,&zz);
add(xx,yy,zz);add(yy,xx,zz);
}dij();
for(int i=1,x,y;i<=b;i++)
{
scanf("%d%d",&x,&y);
printf("%d\n",d[x]+d[y]);
}
}