C/C++教程

[USACO10DEC]Apple Delivery S

本文主要是介绍[USACO10DEC]Apple Delivery S,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

题目链接

题目描述

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

题意翻译

贝西有两个又香又脆的红苹果要送给她的两个朋友。当然她可以走的C(1<=C<=200000)条“牛路”都被包含在一种常用的图中,包含了PP(1<=P<=1000001)个牧场,分别被标为1..P。没有“牛路”会从一个牧场又走回它自己。“牛路”是双向的,每条牛路都会被标上一个距离。最重要的是,每个牧场都可以通向另一个牧场。每条牛路都连接着两个不同的牧场P1_i和P2_i(1<=P1_i,p2_i<=P),距离为D_i。所有“牛路”的距离之和不大于2000000000。

现在,贝西要从牧场PBPB开始给PA_1和PA_2牧场各送一个苹果(PA_1和PA_2顺序可以调换),那么最短的距离是多少呢?当然,PB、PA_1和PA_2各不相同。

 

题解

题意简化:有a,b,c三个点,从a出发,求先到b再到c最短,还是先到c再到b最短

跑2遍dijkstra求最小值即可

以下代码竟然能跑到luogu最优解第4,震惊到我了

#include<bits/stdc++.h>
using namespace std;
int d[570000],v[570000];
int pre[570000],son[570000],dis[570000],now[570000],tot;
int n,m,a,b,c,ans,sum;
void read(int &x)
{
    char c=getchar();int f=0;x=0;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    if(f==-1)x=-x;
}
void add(int x,int y,int z)
{
    pre[++tot]=now[x];
    son[tot]=y;
    now[x]=tot;
    dis[tot]=z;
}
void dij(int s)
{
    memset(d,63,sizeof(d));
    memset(v,0,sizeof(v));
    priority_queue<pair<int,int> >q;
    d[s]=0;q.push(make_pair(0,s));
    while(!q.empty())
    {
        int x=q.top().second;q.pop();
        if(v[x])continue;
        v[x]=1;
        for(int i=now[x];i;i=pre[i])
        {
            int y=son[i];
            if(d[y]>d[x]+dis[i])
            {
                d[y]=d[x]+dis[i];
                q.push(make_pair(-d[y],y));
            }
        }
    }
}
int main()
{
    read(m);read(n);read(a);read(b);read(c);
    for(int i=1,x,y,z;i<=m;i++)
    {
        read(x);read(y);read(z);
        add(x,y,z);add(y,x,z);
    }
    dij(a);ans+=d[b];sum+=d[c];
    dij(b);ans+=d[c];sum+=d[c];
    printf("%d",min(ans,sum));
    return 0;
}

 

这篇关于[USACO10DEC]Apple Delivery S的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!