USACO Chocolate Giving

Luo Gu P2984 [USACO10FEB] to Chocolate Chocolate Giving

Luo Gu Portal

JDOJ 2680: USACO 2010 Feb Silver 2.Chocolate Giving

JDOJ Portal

Description

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.

Input

* 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

Output

* 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

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

Title Translation:

Farmer John has B cows (1 <= B <= 25000), there are N (2 * B <= N <= 50000) farms, numbered 1-N, with a M (N-1 <= M <= 100000) bi-directional edges, i th edge connector and farm R_i S_i (1 <= R_i <= N; 1 <= S_i <= N), the length of the edge is L_i (1 <= L_i <= 2000). P_i living on the farm cows A (1 <= P_i <= N), it wants to send a New Year gift to live on a farm Q_i (1 <= Q_i <= N) cow B, but A cow must first FJ ( living on a farm number 1) there take the gift, then sent to the cow B. Your task is: A dairy cow needs to go at least how far away?

Input formats:

First line: space-separated three integers N, M and B.

The second to M + 1: Line i + 1 row by R_i, S_i and L_i three space separated integers described bidirectional edge i.

The first to M + 2 M + B + 1: Line M + i + 1 comprises two rows separated by a space and P_i integer Q_i.

Output formats:

The first to the B line: the i-th line contains an integer, the Bulls live on a farm P_i of Valentine's Day chocolates made from the FJ there after he gave away to live on the farm Q_i dream love cow needs to go at least.

answer:

I think probably the most naked shorted.

This amount of data dij or SPFA are excellent.

The variable name is very unfriendly ...

NMB ??

do not mind the details.

We can deal with it again SPFA the dist array processing it, and then asked to call when you can.

Code:

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m,b;
int tot,to[200001],val[200001],nxt[200001],head[100001];
int v[100001],dist[100001];
void add(int x,int y,int z)
{
    to[++tot]=y;
    val[tot]=z;
    nxt[tot]=head[x];
    head[x]=tot;
}
void spfa()
{
    memset(dist,0x3f,sizeof(dist));
    memset(v,0,sizeof(v));
    queue<int> q;
    q.push(1);
    v[1]=1;
    dist[1]=0;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        v[x]=0;
        for(int i=head[x];i;i=nxt[i])
        {
            int y=to[i];
            if(dist[y]>dist[x]+val[i])
            {
                dist[y]=dist[x]+val[i];
                if(v[y]==0)
                    q.push(y),v[y]=1;
            }
        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&b);
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);
        add(y,x,z);
    }
    spfa();
    for(int i=1;i<=b;i++)
    {
        int p,q;
        scanf("%d%d",&p,&q);
        int ans=dist[p]+dist[q];
        printf("%d\n",ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11287988.html