首页 > 移动平台 > 详细

【bzoj2100】[Usaco2010 Dec]Apple Delivery

时间:2017-03-03 21:07:43      阅读:322      评论:0      收藏:0      [点我收藏+]

题目描述

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:

 技术分享 

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.

一张P个点的无向图,C条正权路。
CLJ要从Pb点(家)出发,既要去Pa1点NOI赛场拿金牌,也要去Pa2点CMO赛场拿金牌。(途中不必回家)
可以先去NOI,也可以先去CMO。
当然神犇CLJ肯定会使总路程最小,输出最小值。

输入

* 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

样例输入

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


题解

最短路

分先到pa1和先到pa2两种情况作讨论。

由于是无向图,所以可以以pa1和pa2分别为起点跑Spfa(当然需要双头队列优化)

然后取一下min即可。

 
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
deque<int> q;
int head[100010] , to[400010] , len[400010] , next[400010] , cnt , dis[100010][2] , inq[100010] , n;
inline int read()
{
    int ret = 0; char ch = getchar();
    while(ch < ‘0‘ || ch > ‘9‘) ch = getchar();
    while(ch >= ‘0‘ && ch <= ‘9‘) ret = (ret << 3) + (ret << 1) + ch - ‘0‘ , ch = getchar();
    return ret;
}
void add(int x , int y , int z)
{
    to[++cnt] = y;
    len[cnt] = z;
    next[cnt] = head[x];
    head[x] = cnt;
}
void spfa(int s , int p)
{
    int i , x;
    for(i = 1 ; i <= n ; i ++ )
        dis[i][p] = 0x3fffffff;
    dis[s][p] = 0;
    q.push_front(s);
    while(!q.empty())
    {
        x = q.front();
        q.pop_front();
        inq[x] = 0;
        for(i = head[x] ; i ; i = next[i])
        {
            if(dis[to[i]][p] > dis[x][p] + len[i])
            {
                dis[to[i]][p] = dis[x][p] + len[i];
                if(!inq[to[i]])
                {
                    inq[to[i]] = 1;
                    int t;
                    if(!q.empty()) t = q.front();
                    if(!q.empty() && dis[to[i]][p] < dis[t][p]) q.push_front(to[i]);
                    else q.push_back(to[i]);
                }
            }
        }
    }
}
int main()
{
    int m , s , t1 , t2 , i , x , y , z;
    m = read() , n = read() , s = read() , t1 = read() , t2 = read();
    for(i = 1 ; i <= m ; i ++ )
        x = read() , y = read() , z = read() , add(x , y , z) , add(y , x , z);
    spfa(t1 , 0);
    spfa(t2 , 1);
    printf("%d\n" , min(dis[s][0] + dis[t2][0] , dis[s][1] + dis[t1][1]));
    return 0;
}

 

【bzoj2100】[Usaco2010 Dec]Apple Delivery

原文:http://www.cnblogs.com/GXZlegend/p/6498289.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!