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;
}
原文地址:https://www.cnblogs.com/CzYoL/p/7669015.html