1087 All Roads Lead to Rome (30 分)

码农题

#include <bits/stdc++.h>
#define LOCAL
using namespace std;

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

#define vec vector
#define ll long long
#define ld long double
#define sza(x) ((int)x.size())
#define all(a) (a).begin(), (a).end()

const int MAX_N = 1e5 + 5;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;

int n, k;
string start;
map<string, int> ha;
map<int, string> inv;
int happy[210];
vec<pair<int, int>> h[210];
int Min;
vec<int> path;
int cnt;
double happ;
bool st[210];

// dijkstra
int dist[210];
bool vis[210];
void dijkstra(int start){
    memset(dist, 0x3f, sizeof dist);
    dist[start] = 0;
    for(int i = 0; i < n; i ++){
        int k = -1;
        for(int j = 0; j < n; j ++)
            if(vis[j] == 0 && (k == -1 || dist[k] > dist[j]))
                k = j;
        vis[k] = 1;
        for(int j = 0; j < h[k].size(); j ++){
            auto u = h[k][j];
            if(dist[u.first] > dist[k] + u.second)
                dist[u.first] = dist[k] + u.second;
        }
    }
}

void dfs(int u, int w, double hp, vec<int> &p){
    st[u] = 1;
    p.push_back(u);
    if(w > Min) return;
    if(u == ha["ROM"]){
        if(w == Min){
            cnt ++;
            if(hp > happ){
                happ = hp;
                path = p;
            }else if(hp == happ){
                if(hp / p.size() > happ / path.size()){
                    happ = hp;
                    path = p;
                }
            }
        }
        return;
    }
    for(auto v : h[u]){
        if(st[v.first] == 0){
            dfs(v.first, w + v.second, hp + happy[v.first], p);
            st[v.first] = 0;
            p.pop_back();
        }
    }
}

void solve() {
    cin >> n >> k >> start;
    for(int i = 0; i < n - 1; i ++){
        string name;
        int w;
        cin >> name >> w;
        ha[name] = i + 1;
        inv[i + 1] = name;
        happy[ha[name]] = w;
    }
    inv[0] = start;
    ha[start] = 0;
    while(k --){
        string a, b;
        int w;
        cin >> a >> b >> w;
        h[ha[a]].push_back({ha[b], w});
        h[ha[b]].push_back({ha[a], w});
    }
    vec<int> p;
    dijkstra(0);
    Min = dist[ha["ROM"]];
    dfs(0, 0, 0, p);
    cout << cnt << ' ' << Min << ' ' << happ << ' ' << (int) (happ / (path.size() - 1)) << endl;
    // cout << path.size() << endl;
    for(int i = 0; i < path.size() - 1; i ++){
        cout << inv[path[i]] << "->";
    }
    cout << inv[path.back()] << endl;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tc = 1;
    // cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case #" << t << ": ";
        solve();
    }
}
posted @ 2022-02-04 18:14  yys_c  阅读(13)  评论(0编辑  收藏  举报