• 第六章 图论 16 AcWing 1558. 加油站


    第六章 图论 16 AcWing 1558. 加油站

    原题链接

    AcWing 1558. 加油站

    算法标签

    图论 最短路 枚举

    思路

    枚举加油站位置,对于每个加油站位置进行dijkstra,选择符合要求1的最小距离最大值(要求2)与的距离和最小值(要求3)
    在这里插入图片描述
    方便起见,房屋编号设定为1至n, 加油站编号设定为n+1至n+m+1
    在这里插入图片描述

    计算出平均距离略小于答案平均距离

    由于浮点数存在精度丢失,例如计算结果为10,但是浮点数存储为9.99999999,因此计算出平均距离略小于答案平均距离。对于精度丢失,将计算结果加精度损失值即可

    时间复杂度

    10 10 10(枚举加油站位置) ∗ 1000 ∗ 1000 *1000*1000 10001000 (dijkstra求最短路)= O ( 1 0 7 ) O(10^7) O(107)

    代码

    #pragma GCC optimize(2)
    #pragma GCC optimize(3)
    #include
    // #define int long long
    #define x first
    #define y second
    #define ump unordered_map
    #define ums unordered_set
    #define pq priority_queue
    #define rep(i, a, b) for(int i=a;i=b;--i)
    using namespace std;
    typedef pair PII;
    const int N=1020, INF=0x3f3f3f3f;
    double exp1=1e-8;
    //int t, n, m, cnt, ans; 
    int n, m, dd, k, d[N][N], dis[N];
    char c1[5], c2[5];
    bool st[N];
    string p1, p2;
    int di;
    inline int rd(){
       int s=0,w=1;
       char ch=getchar();
       while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
       while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
       return s*w;
    }
    void put(int x) {
        if(x<0) putchar('-'),x=-x;
        if(x>=10) put(x/10);
        putchar(x%10^48);
    }
    int format(string a){
        int ans;
        if(a[0]=='G'){
            ans=n+stoi(a.substr(1));
        }else{
            ans=stoi(a);
        }
        return ans;
    }
    void dij(int S, int &mnd, int &sumd){
        memset(dis, 0x3f, sizeof dis);
        memset(st, 0, sizeof st);
        dis[S]=0;
        rep(i, 0, n+m){
            int t=-1;
            rep(j, 1, n+m+1){
                if(!st[j]&&(t==-1||dis[j]dd){
                mnd=-INF;
                return;
            }
        }
        mnd=INF, sumd=0;
        rep(i, 1, n+1){
            mnd=min(mnd, dis[i]);
            sumd+=dis[i];
        }
    }
    // signed main(){
    int main(){
    	ios::sync_with_stdio(false);
    	cin.tie(0);
    	cout.tie(0);
    // 	k=rd();
        memset(d, 0x3f, sizeof d);
        scanf("%d%d%d%d", &n, &m, &k, &dd);
    	while(k--){
    	    scanf("%s%s%d", c1, c2, &di);
    	    p1=c1, p2=c2; 
    	    d[format(p1)][format(p2)]=d[format(p2)][format(p1)]=min(d[format(p1)][format(p2)], di);
    	}
    	int res=-1, mnd=0, sumd=INF;
    	rep(i, n+1, n+m+1){
    	    int d1, d2;
    	    dij(i, d1, d2);
    	    if(d1>mnd){
    	        res=i, mnd=d1, sumd=d2; 
    	    }else if(d1==mnd&&d2
    • 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
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100

    参考文献

    AcWing 1558. 加油站(PAT甲级辅导课)y总视频讲解

    原创不易
    转载请标明出处
    如果对你有所帮助 别忘啦点赞支持哈
    在这里插入图片描述

  • 相关阅读:
    《DevOps 精要:业务视角》- 读书笔记(三)
    FFmpeg 中 Filters 使用文档介绍
    2023年CCF非专业级别软件能力认证第二轮 (CSP-S)提高级C++语言试题
    通信原理板块——脉冲编码调制(PCM)
    【Xml Tool】 裁剪某个区域重新生成xml
    【零基础入门SpringMVC】第一期——开篇导论
    独立站经营核心技能
    使用LogBack替换Log4j
    【华为OD机试真题 python】斗地主之顺子【2022 Q4 | 200分】
    SpringBoot项目添加2FA双因素身份认证
  • 原文地址:https://blog.csdn.net/T_Y_F_/article/details/127972759