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

说明

输入格式:

第一行:三个用空格隔开的整数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的梦中情牛至少需要走的距离。

 

题解;原来想跑两遍最短路,后来发现,a-pj,pj-b这两次可以看作pj-a,pj-b,所以

只要跑一次spfa即可(注意无向边,边数*2,忘记了,RE了1个点,哭了我)

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
typedef long long ll;
using namespace std;
const int N=200005;
struct node{
    int to;
    int next;
    int val;
}e[N];
int head[N],dis[N],cnt,vis[N];
void add(int a,int b,int c){
    cnt++;
    e[cnt].to=b;
    e[cnt].val=c;
    e[cnt].next=head[a];
    head[a]=cnt;
} 
queue<int>q;
int u,v;
void spfa(){
    memset(dis,0x3f,sizeof(dis));
    q.push(1); dis[1]=0; vis[1]=1;
    while(!q.empty()){
        u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u];i;i=e[i].next){
            v=e[i].to;
            if(dis[v]>dis[u]+e[i].val){
                dis[v]=dis[u]+e[i].val;
                if(vis[v]==0){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
int n,m,b,x,y,z;
int main(){
    freopen("2984.in","r",stdin);
    freopen("2984.out","w",stdout);
    scanf("%d %d %d",&n,&m,&b);
    for(int i=1;i<=m;i++){
        scanf("%d %d %d",&x,&y,&z);
        add(x,y,z); add(y,x,z);
    }
    spfa();
    //int ans=0x3f3f3f3f;
    while(b--){
        scanf("%d %d",&x,&y);
        //ans=min(ans,dis[x]+dis[y]);
        cout<<dis[x]+dis[y]<<endl;
    }
    //cout<<ans;
    return 0;
}

 

转载于:https://www.cnblogs.com/wuhu-JJJ/p/11182176.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值