洛谷 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

输入输出样例

输入样例#1:

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:

6
6
10

最短路

堆优化dijkstra练习

但是 !

啊啊啊啊啊 堆优化dijkstra没过

就写了个spfa。。

屠龙宝刀点击就送

#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <queue>
#define M 500005
using namespace std;
struct node
{
    int x,y;
    bool operator<(node a)const
    {
        return y>=a.y;
    }
};
priority_queue<node>q;
void read(int &x)
{
    x=0;bool f=0;
    register char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch==‘-‘) f=1;
    for(; isdigit(ch);ch=getchar()) x=x*10+ch-‘0‘;
    x=f?-x:x;
}
bool vis[M];
int head[M],Next[M],to[M],dist[M],cnt,dis[M],n,m,b;
int main(int argc,char *argv[])
{
    read(n);
    read(m);
    read(b);
    for(int x,y,z;m--;)
    {
        read(x);
        read(y);
        read(z);
        Next[++cnt]=head[x];to[cnt]=y;dist[cnt]=z;head[x]=cnt;
        Next[++cnt]=head[y];to[cnt]=x;dist[cnt]=z;head[y]=cnt;
    }
    memset(dis,1,sizeof(dis));
    dis[1]=0;
    node a;
    a.x=1;
    a.y=dis[1];
    q.push(a);
    for(;!q.empty();)
    {
        node b=q.top();q.pop();
        if(vis[b.x]) continue;
        vis[b.x]=1;
        for(int i=head[b.x];i;i=Next[i])
        {
            int v=to[i];
            if(dis[v]>dis[b.x]+dist[i])
            {
                dis[v]=dis[b.x]+dist[i];
                node a;
                a.x=v;
                a.y=dis[v];
                q.push(a);
            }
        }
    }
    for(int x,y;b--;)
    {
        read(x);
        read(y);
        printf("%d\n",dis[x]+dis[y]);
    }
    return 0;
}

90分的堆优化

#include <cstdio>
#include <queue>
#define N 400005

using namespace std;
queue<int>q;
bool vis[N];
int n,m,b,cnt,Next[N],to[N],head[N],dist[N],dis[N];
void ins(int u,int v,int w)
{
    Next[++cnt]=head[u];to[cnt]=v;dist[cnt]=w;head[u]=cnt;
    Next[++cnt]=head[v];to[cnt]=u;dist[cnt]=w;head[v]=cnt;
}
void spfa(int s)
{
    for(int i=1;i<=n;i++) dis[i]=0x7fffffff;
    dis[s]=0;
    vis[s]=1;
    q.push(s);
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        vis[now]=0;
        for(int i=head[now];i;i=Next[i])
        {
            int v=to[i];
            if(dis[v]>dis[now]+dist[i])
            {
                dis[v]=dis[now]+dist[i];
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&b);
    for(int x,y,z;m--;)
    {
        scanf("%d%d%d",&x,&y,&z);
        ins(x,y,z);
    }
    spfa(1);
    for(int x,y;b--;)
    {
        scanf("%d%d",&x,&y);
        printf("%d\n",dis[x]+dis[y]);
    }
    return 0;
}
时间: 2024-08-07 04:15:26

洛谷 P2984 [USACO10FEB]给巧克力Chocolate Giving的相关文章

洛谷——P2984 [USACO10FEB]给巧克力Chocolate Giving

https://www.luogu.org/problem/show?pid=2984 题目描述 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 graz

洛谷—— P2983 [USACO10FEB]购买巧克力Chocolate Buying

https://www.luogu.org/problem/show?pid=2983 题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Store features N (1 <= N <= 100,000) kinds of chocolate in essentially unlimited quantities. Each type i of

洛谷P2983 [USACO10FEB]购买巧克力Chocolate Buying

题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Store features N (1 <= N <= 100,000) kinds of chocolate in essentially unlimited quantities. Each type i of chocolate has price P_i (1 <= P_i <= 1

【luogu P2984 [USACO10FEB]给巧克力Chocolate Giving】 题解

题目链接:https://www.luogu.org/problemnew/show/P2984 练习SPFA,把FJ当做起点,求出到所有牛的最短路,再把两个牛的相加. 1 #include <cstdio> 2 #include <queue> 3 #include <iostream> 4 #include <algorithm> 5 #include <cstring> 6 using namespace std; 7 const int

[USACO10FEB]吃巧克力Chocolate Eating

题目:洛谷P2985. 题目大意:有n块巧克力要吃d天,并且只能按顺序吃.一块巧克力有一个开心值,吃了就能增加开心值.一个人初始开心值为0,且每天早上开心值变为原来的一半.问如何吃巧克力才能使开心值最小的一天开心值最大(每天都按吃完巧克力后计算),且需要输出方案. 解题思路:最大化最小值问题,用二分答案. 贪心地扫描,对于一个答案,如果开心值不到这个答案,就一直吃巧克力即可. 最后输出吃的方案时也用此种贪心法. 注意如果最后巧克力没有吃完,则在最后一天全部吃掉. C++ Code: #inclu

洛谷P2982 [USACO10FEB]慢下来Slowing down(线段树 DFS序 区间增减 单点查询)

To 洛谷.2982 慢下来Slowing down 题目描述 Every day each of Farmer John's N (1 <= N <= 100,000) cows conveniently numbered 1..N move from the barn to her private pasture. The pastures are organized as a tree, with the barn being on pasture 1. Exactly N-1 cow

luogu P2985 [USACO10FEB]吃巧克力Chocolate Eating

题目描述 Bessie拿到了N (1 <= N <= 50,000)块巧克力.她决定想个办法吃掉这些巧克力,使得它在吃巧克力的这段时间里,最不开心的一天尽可能的开心.并且一共吃D (1 <= D <= 50,000)天. 每块巧克力有一个开心值H_i (1 <= H_i <= 1,000,000),当某天你吃下那块巧克力时,你将获得那块巧克力的开心值.每一天的开心值是所有当天吃掉的巧克力的总开心值之和.每天晚上Bessie睡觉之后,它的开心值会减半.也就是说,比如昨天B

洛谷 P2982 [USACO10FEB]慢下来Slowing down

P2982 [USACO10FEB]慢下来Slowing down 题目描述 Every day each of Farmer John's N (1 <= N <= 100,000) cows conveniently numbered 1..N move from the barn to her private pasture. The pastures are organized as a tree, with the barn being on pasture 1. Exactly N

洛谷P2982 [USACO10FEB]慢下来Slowing down

题目描述 Every day each of Farmer John's N (1 <= N <= 100,000) cows conveniently numbered 1..N move from the barn to her private pasture. The pastures are organized as a tree, with the barn being on pasture 1. Exactly N-1 cow unidirectional paths connec