标签:blog sid org problem next return hat wpa nec
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.
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
space-separated integers: R_i, S_i, and L_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
水题磨时间、、
1 #include <algorithm> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 6 using namespace std; 7 8 const int N(50000+15); 9 const int M(100000+5); 10 int n,m,b; 11 12 int head[N],sumedge; 13 struct Edge 14 { 15 int v,next,w; 16 Edge(int v=0,int next=0,int w=0): 17 v(v),next(next),w(w){} 18 }edge[M<<1]; 19 void ins(int u,int v,int w) 20 { 21 edge[++sumedge]=Edge(v,head[u],w); 22 head[u]=sumedge; 23 } 24 25 queue<int>que; 26 bool inq[N]; 27 int dis[N]; 28 void SPFA() 29 { 30 memset(dis,127/3,sizeof(dis)); 31 inq[1]=1; que.push(1); dis[1]=0; 32 for(int u,v;!que.empty();) 33 { 34 u=que.front(); que.pop(); inq[u]=0; 35 for(int i=head[u];i;i=edge[i].next) 36 { 37 v=edge[i].v; 38 if(dis[v]>dis[u]+edge[i].w) 39 { 40 dis[v]=dis[u]+edge[i].w; 41 if(!inq[v]) inq[v]=1,que.push(v); 42 } 43 } 44 } 45 } 46 47 int main() 48 { 49 scanf("%d%d%d",&n,&m,&b); 50 for(int u,v,w;m--;) 51 { 52 scanf("%d%d%d",&u,&v,&w); 53 ins(u,v,w); ins(v,u,w); 54 } 55 SPFA(); 56 for(int u,v;b--;) 57 { 58 scanf("%d%d",&u,&v); 59 printf("%d\n",dis[u]+dis[v]); 60 } 61 return 0; 62 }
洛谷——P2984 [USACO10FEB]给巧克力Chocolate Giving
标签:blog sid org problem next return hat wpa nec
原文地址:http://www.cnblogs.com/Shy-key/p/7354228.html