zoukankan      html  css  js  c++  java
  • P3003 [USACO10DEC]苹果交货Apple Delivery

    题目描述

    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.

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

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

    输入输出格式

    输入格式:

    • 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 
    
    输出样例#1: 
     1 /*这道题很好做,直接套slf优化的模板,跑两次取最小值,就好了,本人先开始把slf和lll优化一起加了进去,但是由于lll被常数卡爆了,卡到指数级去了,然后怎么优化都会T掉第二个点,如果用lll优化会T掉4个点(本人亲手实验),用slf可以A掉,而且很快。直接上代码吧。*/
     2 
     3 #include<algorithm>
     4 #include <iostream>
     5 #include  <cstdlib>
     6 #include  <cstring>
     7 #include  <climits>
     8 #include   <cstdio>
     9 #include   <string>
    10 #include    <cmath>
    11 #include    <stack>
    12 #include    <queue>
    13 #include    <deque>
    14 using namespace std;
    15 const int gg=450000;
    16 const int INF=1e9;
    17 int head[gg];
    18 struct node
    19 {
    20     int next;
    21     int w;
    22     int to;
    23 } a[gg];
    24 int dis[gg];
    25 bool vis[gg];
    26 int c,p,pb,pa1,pa2,cnt;
    27 int ans;
    28 int ans2;
    29 int sum,tot;
    30 int rans;
    31 inline void add(int i,int j,int w)
    32 {
    33     a[++cnt].to=j;
    34     a[cnt].next=head[i];
    35     a[cnt].w=w;
    36     head[i]=cnt;
    37 }
    38 inline void spfa(int s)
    39 {
    40     deque<int>q;
    41     memset(vis,false,sizeof(vis));
    42     memset(dis,0x7f,sizeof(dis));
    43     dis[s]=0;
    44     vis[s]=true;
    45     q.push_back(s);
    46     while(!q.empty())
    47     {
    48         int u=q.front();
    49         q.pop_front();
    50         vis[u]=false;
    51         for(register int i=head[u]; i; i=a[i].next)
    52         {
    53             int v=a[i].to;
    54             if(dis[v]>dis[u]+a[i].w)
    55             {
    56                 dis[v]=dis[u]+a[i].w;
    57                 if(!vis[v])
    58                 {
    59                     vis[v]=true;
    60                     if(q.empty()||dis[v]>dis[q.front()])
    61                     {
    62                         q.push_back(v);
    63                     }
    64                     else
    65                         q.push_front(v);
    66                 }
    67             }
    68         }
    69     }
    70 }
    71 int main()
    72 {
    73     scanf("%d%d%d%d%d",&c,&p,&pb,&pa1,&pa2);
    74     for(register int i=1; i<=c; i++)
    75     {
    76         int x,y,z;
    77         scanf("%d%d%d",&x,&y,&z);
    78         add(x,y,z);
    79         add(y,x,z);
    80     }
    81     spfa(pa1);
    82     ans+=dis[pa2]+dis[pb];
    83     spfa(pa2);
    84     ans2+=dis[pa1]+dis[pb];
    85     rans=min(ans,ans2);
    86     printf("%d
    ",rans);
    87     return 0;
    88 }

    12 
  • 相关阅读:
    delphi string 到excel
    VS 快捷键
    delphi Tform 释放
    cxSplitter 收缩和展开
    delphi 加载inc文件
    delphi TcxPageControl 动态嵌入窗体
    修改tomcat-users.xml 失效的问题
    TCXGRID 属性解释
    suse 设置ftp服务器
    用正则表达式修改html字符串的所有div的style样式
  • 原文地址:https://www.cnblogs.com/Hammer-cwz-77/p/7810687.html
Copyright © 2011-2022 走看看