• HDU-3790 最短路最小花费


    判断路径相等时的情况

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    using namespace std;
    
    #define pf printf
    #define sf scanf
    #define mp make_pair
    #define INF 0x3f3f3f3f
    
    const int MAXN = 1000 + 5;
    
    int n,m;
    
    struct node
    {
        int d,fee;
    }g[MAXN][MAXN];
    
    int dis[MAXN],f[MAXN],vis[MAXN];
    
    
    void dijkstra(int v)
    {
        int i,j,mind,minf,dir;
        for(i=1;i<=n;i++)
        {
            dis[i] = f[i] = INF;
            vis[i] = 0;
        }
        dis[v] = f[v] = 0;
        vis[v] = 1;
        for(i=1;i<=n;i++)
        {
            mind = minf = INF;
            for(j=1;j<=n;j++)
            {
                if(!vis[j] && (dis[j]<mind || (dis[j]==mind && f[j]<minf)))
                {
                    mind = dis[j];
                    minf = f[j];
                    dir = j;
                }
            }
            vis[dir] = 1;
            for(j=1;j<=n;j++)
            {
                if(!vis[j] && g[dir][j].d<INF && (dis[j] > (dis[dir]+g[dir][j].d) || (dis[j] == (dis[dir]+g[dir][j].d) && f[j]>f[dir]+g[dir][j].fee)))
                {
                    dis[j] = dis[dir]+g[dir][j].d;
                    f[j]=f[dir]+g[dir][j].fee;
                }
            }
        }
    
    }
    
    
    int main()
    {
        int i,j;
        int a,b,d,fee,st,ed;
        while(sf("%d %d",&n,&m)==2,m+n)
        {
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n;j++)
                {
                    g[i][j].d = g[i][j].fee = (i==j ? 0:INF);
                }
            }
            for(i=0;i<m;i++)
            {
                sf("%d%d%d%d",&a,&b,&d,&fee);
                if(d < g[a][b].d)
                {
                    g[a][b].d = g[b][a].d = d;
                    g[a][b].fee = g[b][a].fee = fee;
                }
                else if(d == g[a][b].d)
                    g[a][b].fee = g[b][a].fee = min(g[a][b].fee,fee);
            }
            sf("%d%d",&st,&ed);
            dijkstra(st);
            pf("%d %d
    ",dis[ed],f[ed]);
        }
        return 0;
  • 相关阅读:
    django-restframework使用
    django-xadmin使用
    python2.7.5升级到2.7.14或者直接升级到3.6.4
    mysql-5.7.25安装以及使用
    django1.9安装以及使用
    Algorithm negotiation failed
    ELK的搭建以及使用
    python 3.5 成功安装 scrapy 的步骤
    pandas基础-Python3
    C#命名规则和编码规范
  • 原文地址:https://www.cnblogs.com/qlky/p/5467053.html
Copyright © 2020-2023  润新知