首页 > 其他分享 >NC24755 [USACO 2010 Dec S]Apple Delivery

NC24755 [USACO 2010 Dec S]Apple Delivery

时间:2023-01-03 19:55:59浏览次数:71  
标签:PA1 PA2 NC24755 Apple int USACO PB pq dis

题目链接

题目

题目描述

Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she travels the C (1 <= C <= 200,000)
cowpaths which are arranged as the usual graph which connects P (1 <= P <= 100,000) pastures conveniently numbered from 1..P: no cowpath leads from a pasture to itself, cowpaths are bidirectional, each cowpath has an associated distance, and, best of all, it is always possible to get from any pasture to any other pasture. Each cowpath connects two differing pastures \(P1_i\) (1 <= \(P1_i\) <= P) and \(P2_i\)​ (1 <= \(P2_i\) <= P) with a distance between them of \(D_i\). The sum of all the distances \(D_i\)​ does not exceed 2,000,000,000.
What is the minimum total distance Bessie must travel to deliver both apples by starting at pasture PB (1 <= PB <= P) and visiting pastures PA1 (1 <= PA1 <= P) and PA2 (1 <= PA2 <= P)
in any order. All three of these pastures are distinct, of course.

Consider this map of bracketed pasture numbers and cowpaths with distances:
                3        2       2
           [1]-----[2]------[3]-----[4]
             \     / \              /
             7\   /4  \3           /2
               \ /     \          /
               [5]-----[6]------[7]
                    1       2
If Bessie starts at pasture [5] and delivers apples to pastures [1] and [4], her best path is:
      5 -> 6-> 7 -> 4* -> 3 -> 2 -> 1*
with a total distance of 12.

输入描述

  • Line 1: Line 1 contains five space-separated integers: C, P, PB, PA1, and PA2
  • Lines 2..C+1: Line i+1 describes cowpath i by naming two pastures it connects and the distance between them: \(P1_i, P2_i, D_i\)

输出描述

  • Line 1: The shortest distance Bessie must travel to deliver both apples

示例1

输入

9 7 5 1 4 
5 1 7 
6 7 2 
4 7 2 
5 6 1 
5 2 4 
4 3 2 
1 2 3 
3 2 2 
2 6 3 

输出

12

题解

知识点:最短路。

从 \(PB\) 出发必须经过 \(PA1,PA2\) 的最短路,显然我们分别考虑 \(PA1,PA2\) 作为终点即可。

先以三个点为起点跑三次最短路,然后讨论两条路径 PB->PA1->PA2,PB->PA2->PA1 的最小值即可。

时间复杂度 \(O((C+P)\log C)\)

空间复杂度 \(O(C+P)\)

代码

#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int N = 100007, M = 200007 << 1;

template<class T>
struct Graph {
    struct edge {
        int v, nxt;
        T w;
    };
    int idx;
    vector<int> h;
    vector<edge> e;

    Graph(int n, int m) :idx(0), h(n + 1), e(m + 1) {}

    void clear(int n, int m) {//全局使用时清零,确定范围防止超时
        idx = 0;
        h.assign(n + 1, 0);
        e.assign(m + 1, { 0,0,0 });
    }

    void add(int u, int v, T w) {
        e[++idx] = edge{ v,h[u],w };
        h[u] = idx;
    }
};
Graph<int> g(N, M);

void dijkstra(int st, vector<int> &dis) {
    dis.assign(dis.size(), 0x3f3f3f3f);
    vector<bool> vis(dis.size(), 0);
    struct node {
        int v, w;
        bool operator<(node a) const {
            return w > a.w;
        }
    };
    priority_queue<node> pq;
    dis[st] = 0;
    pq.push({ st,0 });
    while (!pq.empty()) {
        int u = pq.top().v;
        pq.pop();
        if (vis[u]) continue;
        vis[u] = 1;
        for (int i = g.h[u];i;i = g.e[i].nxt) {
            int v = g.e[i].v, w = g.e[i].w;
            if (dis[v] > dis[u] + w) {
                dis[v] = dis[u] + w;
                pq.push({ v, dis[v] });
            }
        }
    }
}

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int C, P, PB, PA1, PA2;
    cin >> C >> P >> PB >> PA1 >> PA2;
    for (int i = 1;i <= C;i++) {
        int P1, P2, D;
        cin >> P1 >> P2 >> D;
        g.add(P1, P2, D);
        g.add(P2, P1, D);
    }
    vector<int> disB(P + 1), disA1(P + 1), disA2(P + 1);
    dijkstra(PB, disB);
    dijkstra(PA1, disA1);
    dijkstra(PA2, disA2);
    cout << min(disB[PA1] + disA1[PA2], disB[PA2] + disA2[PA1]) << '\n';
    return 0;
}

标签:PA1,PA2,NC24755,Apple,int,USACO,PB,pq,dis
From: https://www.cnblogs.com/BlankYang/p/17023217.html

相关文章

  • NC24858 [USACO 2009 Nov S]Job Hunt
    题目链接题目题目描述Bessieisrunningoutofmoneyandissearchingforjobs.FarmerJohnknowsthisandwantsthecowstotravelaroundsohehasimposeda......
  • 牛客寒假算法基础集训营4-J-Applese 的减肥计划
    链接:​​https://ac.nowcoder.com/acm/contest/330/J​​牛客网 已知Applese两只手分别产生的力的大小,以及它们之间的夹角,试求两力合力的大小。输入描述:仅一行三个整......
  • 牛客寒假算法基础集训营4-B-Applese 走方格
    链接:​​https://ac.nowcoder.com/acm/contest/330/B​​​牛客网 在这个游戏中,它位于一个n行m列的方阵中的左上角(坐标为(0,0),行的序号为0∼n−10∼n−1,列的序号为0......
  • P1217 [USACO1.5]回文质数 Prime Palindromes
    题目题目描述因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以151是回文质数。写一个程序来找出范围[a,b](5<=a<b<=100,000,000)(一亿)间的......
  • USACO 2019 January Contest, Silver
    2019JanuarySilverT1.GrassPlanting这道题是一道结论题:我们发现点的度数越多,答案越多,所以我们可以发现答案为最大的点的度数+1。#include<bits/stdc++.h>usingnam......
  • AcWing1170. 排队布局[USACO05]
    解题思路\(\qquad\)这题也是一个比较裸的差分约束:多了的那个输出\(-2\)的其实就是在差分约束系统中\(1\)号点和\(n\)号点没有约束关系,也就是\(1\)和\(n\)号不连通。由于这......
  • P2894 [USACO08FEB]Hotel G
    \(P2894\)[\(USACO08FEB\)]\(Hotel\)\(G\)一、题目描述参考样例,第一行输入\(n,m\),\(n\)代表有\(n\)个房间,编号为\(1-\simn\),开始都为空房,\(m\)表示以下有\(m\)行操作......
  • [USACO22DEC] Cow College B 题解
    洛谷P8897AcWing4821题目描述有\(n\)头奶牛,每头奶牛愿意交的最大学费位\(c_i\),问如何设置学费,可以使赚到的钱最多。\(1\len\le10^5,1\lec_i\le10^6\)做法分析......
  • [USACO22FEB] Sleeping in Class B
    好题分享——[USACO22FEB]SleepinginClassB洛谷题目链接题面翻译\(T\)组数据,每组给定一个长度为\(N\)的数组\(a_1,a_2,\dotsb,a_n\)。每次操作可选择两个......
  • P3128 [USACO15DEC]Max Flow P 树上点差分
    //题意:给出一棵树,现在有一操作:给出两点,将两点之间的路径都加1,最后统计整棵树中值最大的点是谁//思路:树上路径问题,树剖+线段树可以解决,但是因为只是简单的维护区间加减,用......