zoukankan      html  css  js  c++  java
  • Zoj 2027 Travelling Fee 最短路变形

    给你一个图和AB,问你从A到B的路径中,当每条路径的最长的边长度忽略的情况下,A到B的最短路.

    建立两个矩阵,一个记录最大长度,一个是最短路,同步更新即可.

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <string>
    #include <iostream>
    #include <map>
    #include <cstdlib>
    #include <list>
    #include <set>
    #include <queue>
    #include <stack>
    
    using namespace std;
    
    typedef long long LL;
    const int maxn = 105;
    map<string,int> mp;
    int maxcost[maxn][maxn],cost[maxn][maxn];
    int n,cnt,str,ed;
    string a,b;
    
    int id(string &s) {
        if(mp.count(s) == 0) mp[s] = ++cnt;
        return mp[s];
    }
    
    int main() {
        while(cin >> a >> b) {
            mp.clear();
            cnt = 0;
            str = id(a); ed = id(b);
            cin >> n;
            memset(maxcost,-1,sizeof(maxcost));
            memset(cost,-1,sizeof(cost));
            for(int i = 1;i <= n;i++) {
                int cc;
                cin >> a >> b >> cc;
                cost[id(a)][id(b)] = cost[id(b)][id(a)] = 0;
                maxcost[id(a)][id(b)] = maxcost[id(a)][id(b)] = cc;
            }
            for(int k = 1;k <= cnt;k++) {
                for(int i = 1;i <= cnt;i++) {
                    for(int j = 1;j <= cnt;j++) if(cost[i][k] != -1 && cost[k][j] != -1) {
                        int ca = cost[i][j];
                        int cb = cost[i][k] + cost[k][j] + min(maxcost[i][k],maxcost[k][j]);
                        if(cb < ca || ca == -1) {
                            cost[i][j] = cb;
                            maxcost[i][j] = max(maxcost[i][k],maxcost[k][j]);
                        }
                    }
                }
            }
            printf("%d
    ",cost[str][ed]);
        }
        return 0;
    }
  • 相关阅读:
    计算机的时空观以及俩者间的相互转换
    发送一个记录数据包
    流的压缩与解压缩函数
    通过自定义消息调用主窗体的菜单项
    多标签窗体的释放一法
    记录数组存出到文件和从文件导入
    变体记录
    内存流的使用
    用流读写结构化文件
    下拉框、下拉控件之Select2
  • 原文地址:https://www.cnblogs.com/rolight/p/3857318.html
Copyright © 2011-2022 走看看