zoukankan      html  css  js  c++  java
  • HDU 1224 Free DIY Tour

    传送门

    题目大意:

    一个有向图(n + 1相当于1),每个点有一个权值(可以认为1和n+1权值为0),求从1走到n+1(相当于走回1)的最大路径权值和是多少,输出方案。

    题目分析:

    最短路问题,输出方案只需在dijkstra更新时记录from数组,完成后倒推即可。

    code

    #include<bits/stdc++.h>
    using namespace std;
    
    typedef long long ll;
    
    namespace IO{
        inline ll read(){
            ll i = 0, f = 1; char ch = getchar();
            for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
            if(ch == '-') f = -1, ch = getchar();
            for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
            return i * f;
        }
        inline void wr(ll x){
            if(x < 0) putchar('-'), x = -x;
            if(x > 9) wr(x / 10);
            putchar(x % 10 + '0');
        }
    }using namespace IO;
    
    const int N = 105, OO = 0x3f3f3f3f;
    int n, dis[N], T, m, val[N], from[N], k;
    bool gra[N][N];
    typedef pair<int, int> P;
    priority_queue<P> que;
    
    inline void solve(){
        memset(dis, -OO, sizeof dis), dis[1] = 0;
        que.push(P(0, 1));
        while(!que.empty()){
            P t = que.top(); que.pop();
            for(int i = t.second + 1; i <= n + 1; i++){
                if(i == t.second || !gra[t.second][i]) continue;
                // cout<<t.second<<"->>"<<i<<endl;
                // cout<<dis[3]<<" "<<dis[1] + val[3]<<endl;
                if(dis[i] < t.first + val[i]){
                    dis[i] = t.first + val[i];
                    from[i] = t.second;
                    que.push(P(dis[i], i));
                }
            }
        }
        vector<int> ans; ans.push_back(n + 1); 
        int now = n + 1; while(from[now]) ans.push_back(from[now]), now = from[now];
        printf("CASE %d#
    points : %d
    circuit : ", ++k, dis[n + 1]);
        for(int i = ans.size() - 1; i > 0; i--) printf("%d->", ans[i]); wr(1);
        printf("
    ");
    }
    
    int main(){
        freopen("h.in", "r", stdin);
        T = read();
        while(T--){
            memset(gra, 0, sizeof gra), memset(from, 0, sizeof from), memset(val, 0, sizeof val);
            n = read(); for(int i = 1; i <= n; i++) val[i] = read();
            m = read(); for(int i = 1; i <= m; i++){
                int x = read(), y = read();
                if(x > y) swap(x, y);
                gra[x][y] = true;
            }
            solve();
            if(T) printf("
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    POJ 1860
    Notepad++ 经常使用快捷键 (MEMO)
    SpringMVC现实
    krpano漫游加方向性3D声音(这篇文章已被移到krpano中国网站 krpano360.com)
    DFS PKU 1562
    Java中间(三十五)-----Java详细设置(一个):请指定初始容量设置
    HDU 5052 Yaoge’s maximum profit 光秃秃的树链拆分 2014 ACM/ICPC Asia Regional Shanghai Online
    智课雅思词汇---二十五、-ate
    新东方雅思词汇---8.3、apt
    新东方雅思词汇---7.4、cap
  • 原文地址:https://www.cnblogs.com/CzYoL/p/7669015.html
Copyright © 2011-2022 走看看