• P3008 [USACO11JAN]Roads and Planes G (SPFA + SLF优化)


    题目如下:

    [USACO11JAN]Roads and Planes G

    题面翻译

    题面描述

    Farmer John 正在一个新的销售区域对他的牛奶销售方案进行调查。他想把牛奶送到 T T T 个城镇 ( 1 ≤ T ≤ 25 , 000 1 \le T \le 25,000 1T25,000 ),编号为 1 1 1 T T T 。这些城镇之间通过 R R R 条道路 ( 1 ≤ R ≤ 50 , 000 1 \le R \le 50,000 1R50,000 ,编号为 1 1 1 R R R ) 和 P P P 条航线 ( 1 ≤ P ≤ 50 , 000 1 \le P \le 50,000 1P50,000 ,编号为 1 1 1 P P P ) 连接。每条道路 i i i 或者航线 i i i 连接城镇 A i A_i Ai ( 1 ≤ A i ≤ T 1 \le A_i \le T 1AiT )到 B i B_i Bi ( 1 ≤ B i ≤ T 1 \le B_i \le T 1BiT ),花费为 C i C_i Ci

    对于道路 0 ≤ C i ≤ 10 , 000 0 \le C_i \le 10,000 0Ci10,000 ;然而航线的花费很神奇,花费 C i C_i Ci 可能是负数( − 10 , 000 ≤ C i ≤ 10 , 000 -10,000 \le C_i \le 10,000 10,000Ci10,000 )。道路是双向的,可以从 A i A_i Ai B i B_i Bi,也可以从 B i B_i Bi A i A_i Ai ,花费都是 C i C_i Ci 。然而航线与之不同,只可以从 A i A_i Ai B i B_i Bi

    事实上,由于最近恐怖主义太嚣张,为了社会和谐,出台 了一些政策保证:如果有一条航线可以从 A i A_i Ai B i B_i Bi,那么保证不可能通过一些道路和航线从 B i B_i Bi 回到 A i A_i Ai 。由于 F J FJ FJ 的奶牛世界公认十分给力,他需要运送奶牛到每一个城镇。他想找到从发送中心城镇 S S S ( 1 ≤ S ≤ T 1 \le S \le T 1ST) 把奶牛送到每个城镇的最便宜的方案,或者知道这是不可能的。

    输入格式

    R + P + 1 R+P+1 R+P+1

    1 1 1 行:四个整数 T T T , R R R , P P P S S S ,分别表示城镇的数量,道路的数量,航线的数量和中心城镇。

    2 2 2 R + 1 R+1 R+1 行:每行三个整数 A i A_i Ai , B i B_i Bi C i C_i Ci ,描述一条道路。

    R + 2 R+2 R+2 R + P + 1 R+P+1 R+P+1 行:每行三个整数 A i A_i Ai , B i B_i Bi C i C_i Ci ,描述一条航线。

    输出格式

    T T T 行,第 i i i 行输出城市 S S S 到城市 i i i 的最小花费。如果不能到达,输出NO PATH

    题目描述

    Farmer John is conducting research for a new milk contract in a new territory. He intends to distribute milk to T (1 <= T <= 25,000) towns conveniently numbered 1…T which are connected by up to R (1 <= R <= 50,000) roads conveniently numbered 1…R and/or P (1 <= P <= 50,000) airplane flights conveniently numbered 1…P.

    Either road i or plane i connects town A_i (1 <= A_i <= T) to town B_i (1 <= B_i <= T) with traversal cost C_i. For roads, 0 <= C_i <= 10,000; however, due to the strange finances of the airlines, the cost for planes can be quite negative (-10,000 <= C_i <= 10,000).

    Roads are bidirectional and can be traversed from A_i to B_i or B_i to A_i for the same cost; the cost of a road must be non-negative.

    Planes, however, can only be used in the direction from A_i to B_i specified in the input. In fact, if there is a plane from A_i to B_i it is guaranteed that there is no way to return from B_i to A_i with any sequence of roads and planes due to recent antiterror regulation.

    Farmer John is known around the world as the source of the world’s finest dairy cows. He has in fact received orders for his cows from every single town. He therefore wants to find the cheapest price for delivery to each town from his distribution center in town S (1 <= S <= T) or to know that it is not possible if this is the case.

    MEMORY LIMIT: 64MB

    输入格式

    * Line 1: Four space separated integers: T, R, P, and S

    * Lines 2…R+1: Three space separated integers describing a road: A_i, B_i and C_i

    * Lines R+2…R+P+1: Three space separated integers describing a plane: A_i, B_i and C_i

    输出格式

    * Lines 1…T: The minimum cost to get from town S to town i, or ‘NO PATH’ if this is not possible

    样例 #1

    样例输入 #1

    6 3 3 4 
    1 2 5 
    3 4 5 
    5 6 10 
    3 5 -100 
    4 6 -100 
    1 3 -10
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    样例输出 #1

    NO PATH 
    NO PATH 
    5 
    0 
    -95 
    -100
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    提示

    6 towns. There are roads between town 1 and town 2, town 3 and town 4, and town 5 and town 6 with costs 5, 5 and 10; there are planes from town 3 to town 5, from town 4 to town 6, and from town 1 to town 3 with costs -100, - 100 and -10. FJ is based in town 4.

    FJ’s cows begin at town 4, and can get to town 3 on the road. They can get to towns 5 and 6 using planes from towns 3 and 4. However, there is no way to get to towns 1 and 2, since they cannot go

    backwards on the plane from 1 to 3.

    思路:

    存在负权的最短路

    SPFA + SLF 可以卡过去!

    SPFA + SLF 板子如下:

    void SPFA()
    {
        deque<ll > q;
        d[s] = 0;
        q.push_back(s);
        vis[s] = 1;
        while(!q.empty())
        {
            ll x = q.front();
            q.pop_front();
            vis[x] = 0;
            for(int i=head[x]; i!=-1; i = nxt[i])
            {
                ll v = pnt[i];
                if(d[v] > d[x] + cost[i] )
                {
                    d[v] = d[x] + cost[i] ;
                    if(vis[v]) continue;
                    vis[v] = 1;
                    if(q.size()&&d[v]<d[q.front()])     //SLF,小的进队头,大的到队尾
                    q.push_front(v);
                    else
                    q.push_back(v);
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    AC代码如下:

    #include <bits/stdc++.h>
    #define buff                     \
        ios::sync_with_stdio(false); \
        cin.tie(0);
    //#define int long long
    using namespace std;
    
    const int N = 1e6 + 9;
    int n, m1, m2, gog;
    int h[N], e[N], ne[N], w[N], idx;
    int dist[N];
    bool vis[N];
    void add(int a, int b, int c)
    {
        idx++;
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx;
    }
    
    void spfa(int goal)
    {
        deque<int> q;
        for (int i = 1; i <= n; i++)
            dist[i] = 0x3f3f3f3f, vis[i] = 0;
        q.push_back(gog);
        dist[gog] = 0, vis[gog] = 1;
        while (!q.empty())
        {
            int t = q.front();
            q.pop_front();
            vis[t] = 0;
    
            for (int i = h[t]; i; i = ne[i])
            {
                int j = e[i];
                if (dist[j] > dist[t] + w[i])
                {
                    dist[j] = dist[t] + w[i];
                    if (vis[j])
                        continue;
                    vis[j] = 1;
                    if (q.size() && dist[j] < dist[q.front()])
                        q.push_front(j);
                    else
                        q.push_back(j);
                }
            }
        }
    }
    void solve()
    {
        cin >> n >> m1 >> m2 >> gog;
    
        int u, v, w;
        for (int i = 1; i <= m1; i++)
        {
            cin >> u >> v >> w;
            add(u, v, w);
            add(v, u, w);
        }
    
        for (int i = 1; i <= m2; i++)
        {
            cin >> u >> v >> w;
            add(u, v, w);
        }
        spfa(gog);
        for (int i = 1; i <= n; i++)
        {
            
            if (dist[i] == 0x3f3f3f3f)
                // puts("NO PATH");
                cout << "NO PATH" << '\n';
            else    
                cout << dist[i] << '\n';
        }
    }
    int main()
    {
        buff;
        solve();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
  • 相关阅读:
    【综合评价分析】topsis评价 原理+完整MATLAB代码+详细注释+操作实列
    css的gap设置元素之间的间隔
    cocoaPods源码之从入口Pod学起
    idea设置了maven会自动变回C盘那个
    【AGC】典型问题FAQ 5
    [附源码]计算机毕业设计JAVA新冠疫苗线上预约系统
    【老生谈算法】matlabBOOST电路的设计与仿真——BOOST电路
    Cadence23学习笔记(三)
    web前端网页设计期末课程大作业:企业网页主题网站设计——舞蹈培训11页HTML+CSS+JavaScript
    rk3588 编译内核遇到问题解决
  • 原文地址:https://blog.csdn.net/m0_60593173/article/details/125447766