zoukankan      html  css  js  c++  java
  • 7-14 直捣黄龙 (30分)

    7-14 直捣黄龙 (30分)

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<algorithm>
    #include<cstring>
    #include<string>
    #include<map>
    #include<set>
    #include<queue>
    #include<iomanip>
    #include<stack>
    using namespace std;
    #define STDIN freopen("in.in", "r", stdin);freopen("out.out", "w", stdout);
    
    
    const int N = 205;
    int G[N][N];
    
    int n, k;
    map<string, int > ma1;
    map<int,string> ma2;
    string src, des;
    int cnt = 0;
    int nums[N];
    int dist[N];
    bool st[N];
    int step[N];
    int shadi[N];
    int pre[N];
    int lujing[N];
    vector<string> ans;
    void rele(string a, int b)
    {
        ma1[a] = b;
        ma2[b] = a;
    }
    int dijkstra(int str)
    {
        memset(dist, 0x3f, sizeof dist);
        dist[str] = 0;
        step[str] = 0;
        shadi[str] = 0;
        lujing[str] = 1;
        for (int i = 0; i  < cnt - 1; i++)
        {
            int t = -1;
            for (int j = 1; j <= cnt; j++)
            {
                if (!st[j] && (t == -1 || dist[t] > dist[j])) t=j;
            }
            for (int j = 1; j <= cnt ;j++)
            {
                if (dist[j] > dist[t] + G[t][j])
                {
                    dist[j] = dist[t] + G[t][j];
                    step[j] = step[t] + 1;
                    shadi[j] = shadi[t] + nums[j];
                    pre[j] = t;
                    lujing[j] = lujing[t];
                }
                if (dist[j] == dist[t] + G[t][j])
                {
                    if (pre[j] != t)
                        lujing[j] += lujing[t];
                    if (step[j] < step[t] + 1) {
                        step[j] = step[t] + 1;
                        shadi[j] = shadi[t] + nums[j];
                        pre[j] = t;
                    }
                    if (step[j] == step[t] + 1)
                    {
                        if (shadi[j] < shadi[t] + nums[j])
                        {
                            shadi[j] = shadi[t] + nums[j];
                            pre[j] = t;
                        }
                    }
                }
            }
            st[t] = true;
        }
    
        if (dist[ma1[des]] == 0x3f3f3f3f) return -1;
        return dist[ma1[des]];
    }
    void print(string u)
    {
        if (ma1[u] != 0)
        {
            print(ma2[pre[ma1[u]]]);
            ans.push_back(u);
        }
    }
    int main()
    {
    //     STDIN
        cin >> n >> k;
        memset(G, 0x3f, sizeof G);
        cin >> src >> des;
        rele(src, ++cnt);
        rele(des, ++cnt);
        for (int i = 1; i < n; i++)
        {
            string t; int x;
            cin >> t >> x;
            if (ma1.count(t) == 0){
                rele(t, ++cnt);
            }
            nums[ma1[t]] = x;
        }
        
        for (int i = 1;  i<= k; i++)
        {
            string a, b;
            int len;
            cin >> a >> b >> len;
            int ia = ma1[a], ib = ma1[b];
            G[ia][ib] = min(G[ia][ib], len);
            G[ib][ia] = min(G[ib][ia], len);
        }
        dijkstra(ma1[src]);
        print(des);
        int len = ans.size();
        for (int i = 0;  i< len;i++)
        {
            cout << ans[i];
            if (i == len-1) cout << endl;
            else cout << "->";
        }
        cout << lujing[ma1[des]] << " " << dist[ma1[des]] << " " << shadi[ma1[des]] << endl;
        return 0;
    }
  • 相关阅读:
    C# 以GZip解压缩
    C# 获取时间戳(支持毫秒)
    C#中 DateTime 转 DateTimeOffset
    C# WPF中 SecureString 转 String
    C# 根据文件头判断文件类型
    C#中 MD5 32位加密
    C#中 Stream转为byte[]
    C#中byte[]转BitmapImage
    sql按天分组
    eclispe报错PermGen space
  • 原文地址:https://www.cnblogs.com/hulian425/p/14049914.html
Copyright © 2011-2022 走看看