P2984 [USACO10FEB]给巧克力Chocolate Giving
题目描述
Farmer John is distributing chocolates at the barn for Valentine's day, and B (1 <= B <= 25,000) of his bulls have a special cow in mind to receive a chocolate gift.
Each of the bulls and cows is grazing alone in one of the farm's N (2*B <= N <= 50,000) pastures conveniently numbered 1..N and connected by M (N-1 <= M <= 100,000) bidirectional cowpaths of various lengths. Some pastures might be directly connected by more than one cowpath. Cowpath i connects pastures R_i and S_i (1 <= R_i <= N; 1 <= S_i <= N) and has length L_i (1 <= L_i <= 2,000).
Bull i resides in pasture P_i (1 <= P_i <= N) and wishes to give a chocolate to the cow in pasture Q_i (1 <= Q_i <= N).
Help the bulls find the shortest path from their current pasture to the barn (which is located at pasture 1) and then onward to the pasture where their special cow is grazing. The barn connects, one way or another (potentially via other cowpaths and pastures) to every pasture.
As an example, consider a farm with 6 pastures, 6 paths, and 3 bulls (in pastures 2, 3, and 5) who wish to bestow chocolates on their love-objects:
*1 <-- Bull wants chocolates for pasture 1 cow
[4]--3--[5] <-- [5] is the pasture ID
/ |
/ |
4 2 <-- 2 is the cowpath length
/ | between [3] and [4]
[1]--1--[3]*6
/ \ /
9 3 2
/ \/
[6] [2]*4
* The Bull in pasture 2 can travel distance 3 (two different ways) to get to the barn then travel distance 2+1 to pastures [3] and [4] to gift his chocolate. That's 6 altogether.
* The Bull in pasture 5 can travel to pasture 4 (distance 3), then pastures 3 and 1 (total: 3 + 2 + 1 = 6) to bestow his chocolate offer.
* The Bull in pasture 3 can travel distance 1 to pasture 1 and then take his chocolate 9 more to pasture 6, a total distance of 10.
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至少需要走多远的路程?
输入格式
* Line 1: Three space separated integers: N, M, and B
* Lines 2..M+1: Line i+1 describes cowpath i with three
space-separated integers: R_i, S_i, and L_i
* Lines M+2..M+B+1: Line M+i+1 contains two space separated integers: P_i and Q_i
输出格式
* Lines 1..B: Line i should contain a single integer, the smallest distance that the bull in pasture P_i must travel to get chocolates from the barn and then award them to the cow of his dreams in pasture 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
说明/提示
输入格式:
第一行:三个用空格隔开的整数N,M和B。
第二到M+1行:第i+1行用R_i,S_i和L_i三个用空格隔开的整数描述双向边i。
第M+2到M+B+1行:第M+i+1行包含两个用空格隔开的整数P_i和Q_i。
输出格式:
第一到B行:第i行包括一个整数,居住在农场P_i的公牛从FJ那里取得情人节巧克力后送给他居住在农场Q_i的梦中情牛至少需要走的距离。
其实把一到各个点的最短路求出来就行了,我一开始还跑了n+1次SPFA ,TLE了4个点。
#include<cstdio> #include<queue> using namespace std; const long long inf=2147483647; const int maxn=200005; const int maxm=1000005; using namespace std; int n,m,s,num_edge=0,p,b; int dis[maxn],vis[maxn],head[maxm],lll[maxn]; struct Edge{ int next,to,dis; }edge[maxm]; inline int read(){ int s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-'){ w=-1; } ch=getchar(); } while(ch>='0'&&ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*w; } void add(int from,int to,int dis){ edge[++num_edge].next=head[from]; edge[num_edge].to=to; edge[num_edge].dis=dis; head[from]=num_edge; } void spfa(){ queue<int> q; for(int i=1; i<=n; i++) { dis[i]=inf; vis[i]=0; } q.push(s); dis[s]=0; vis[s]=1; while(!q.empty()){ int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u];i;i=edge[i].next){ int v=edge[i].to; if(dis[v]>dis[u]+edge[i].dis){ dis[v]=dis[u]+edge[i].dis; if(vis[v]==0){ vis[v]=1; q.push(v); } } } } } int main(){ n=read(); m=read(); b=read(); for(int i=1; i<=m; i++){ int f,g,w; f=read(); g=read(); w=read(); add(f,g,w); add(g,f,w); } s=1; spfa(); for(int i=1;i<=n;i++){ lll[i]=dis[i]; } for(int i=1;i<=b;i++){ long long ans=0; s=read(); p=read(); ans+=lll[s]; ans+=lll[p]; printf("%lld\n",ans); } return 0; }