zoukankan      html  css  js  c++  java
  • 6.12考试 T3 城市交通费

    n1~nipim1~mjajbj

    wjxy

    xyxy

    qxyxy+

      第n,m,qnp1~pnm3jAjBjWjQxy

      QQxy

    输入样例1:

    5 7 2
    2 5 3 3 4
    1 2 3
    1 3 2
    2 5 3
    5 3 1
    5 4 1
    2 4 3
    3 4 4
    1 4
    2 3

    输出样例1:

    8
    9

    该题其他输入输出数据:https://share.weiyun.com/5R3eFO1

    思路:

    Floyd,由于Floyd在寻找最短路时会更新之前的路径,所以无法单独建立一个数组记录从i到j的最大城市建设费。在更新map[]的同时更新cost_m[];

    解析:

    t[]数组表示城市建设费的升序排列;

    map[]表示当前最小的路费;

    cost_m[]表示当前的最小(路费+最大城市建设费);

    代码:

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    
    using namespace std;
    
    int n,m,q;
    int cost[258],t[258],cost_m[258][258];
    int map[258][258];
    
    long long read()
    {
        long long x=0,f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    
    int cmp(int x,int y)
    {
        return cost[x]<cost[y];
    }
    
    int main()
    {    
        freopen("road.in","r",stdin);
        freopen("road.out","w",stdout);
        n=read();m=read();q=read();
        for(int i=1;i<=n;++i)
            for(int j=1;j<=n;++j)
                map[i][j]=999999999;
        for(int i=1;i<=n;++i) cost[i]=read();
        for(int i=1;i<=m;++i)
        {
            int a,b,w;
            a=read();b=read();w=read();
            map[a][b]=min(map[a][b],w);
            map[b][a]=min(map[b][a],w);
        }
        for(int i=1;i<=n;++i)
        {
            map[i][i]=0;
            t[i]=i;
        }
        sort(t+1,t+n+1,cmp);//表示cost[]的升序 ,如果不sort会WA掉,因为是最小花费,所以我们用最小的城市建设费作为重中点来更新其他点 
        for(int i=1;i<=n;++i)
            for(int j=1;j<=n;++j)
                cost_m[i][j]=map[i][j]+max(cost[i],cost[j]);//预处理cost_m 
        for(int k=1;k<=n;++k)//中点要放在外面 
            for(int i=1;i<=n;++i)
                for(int j=1;j<=n;++j)
                {
                    map[i][j]=min(map[i][j],map[i][t[k]]+map[t[k]][j]);//更新当前的路费 
                    cost_m[i][j]=min(cost_m[i][j],map[i][j]+max(cost[i],max(cost[j],cost[t[k]])));//更新当前的总费用 
                }
        for(int i=1;i<=q;++i)
        {
            int u,v;
            u=read();v=read();
            printf("%d
    ",cost_m[u][v]);
        }
        return 0;
    }
  • 相关阅读:
    Django入门
    初识json
    回来了
    python学习
    JavaScript 中获取元素样式
    浏览器检测与特征检测
    DOM 节点的类型及判定
    浏览器的控制台工具
    .htaccess 配置文件的使用
    workLog:07001:补充0829 前
  • 原文地址:https://www.cnblogs.com/-hhs/p/11010643.html
Copyright © 2011-2022 走看看