zoukankan      html  css  js  c++  java
  • (双端队列优化的SPFA) bzoj 2100

    Description

    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肯定会使总路程最小,输出最小值。

    Input

    * 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

    Output

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

    Sample Input

    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



    Sample Output

    12

    HINT

    求翻译.........站内PM我吧.........

    Source

     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    #include<deque>
    #include<vector>
    #include<set>
    #define INF 100000000
    using namespace std;
    vector<int> e[100005],w[100005];
    int c,p,x,xx,xxx,dist[100005];
    bool vis[100005];
    void spfa(int u)
    {
        deque<int> q;
        for(int i=1;i<=p;i++)
            dist[i]=INF;
        q.push_back(u);
        vis[u]=1;
        dist[u]=0;
        while(!q.empty())
        {
            int x=q.front();
            q.pop_front();
            vis[x]=0;
            for(int i=0;i<e[x].size();i++)
            {
                int v=e[x][i];
                if(dist[v]>dist[x]+w[x][i])
                {
                    dist[v]=dist[x]+w[x][i];
                    if(!vis[v])
                    {
                        vis[v]=1;
                        if(!q.empty())
                        {
                            if(dist[v]>dist[q.front()])
                                q.push_back(v);
                            else
                                q.push_front(v);
                        }
                        else
                            q.push_back(v);
                    }
                }
            }
        }
    }
    int main()
    {
        scanf("%d%d%d%d%d",&c,&p,&x,&xx,&xxx);
        for(int i=1;i<=c;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            e[x].push_back(y);
            e[y].push_back(x);
            w[x].push_back(z);
            w[y].push_back(z);
        }
        spfa(xx);
        int ans=dist[x]+dist[xxx];
        spfa(xxx);
        if(ans>dist[x]+dist[xx])
            ans=dist[x]+dist[xx];
        printf("%d
    ",ans);
        return 0;
    }
    

      

  • 相关阅读:
    Linux部署web项目
    bash: mysql: command not found(解决方法)
    mysql登录遇到ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
    mysqld:未被识别的服务(解决方法)
    Xshell4连接,Linux系统中文显示乱码解决办法
    F5双机冗余配置
    H3C S10512虚拟化配置
    cisco3900板卡sm-es3g-24-p使用方法
    详解Cisco ACS AAA认证-1(转)
    cisco路由器license的相关命令简单梳理(转)
  • 原文地址:https://www.cnblogs.com/water-full/p/4521639.html
Copyright © 2011-2022 走看看