其他分享
首页 > 其他分享> > AcWing 野餐规划

AcWing 野餐规划

作者:互联网

AcWing 野餐规划

Description

Input

Output

Sample Input

10
Alphonzo Bernardo 32
Alphonzo Park 57
Alphonzo Eduardo 43
Bernardo Park 19
Bernardo Clemenzi 82
Clemenzi Park 65
Clemenzi Herb 90
Clemenzi Eduardo 109
Park Herb 24
Herb Eduardo 79
3

Sample Output

Total miles driven: 183

题解:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#define N 35
#define M 100005
#define inf 0x7fffffff
using namespace std;

struct E {int next, to, dis;} e[M], edge[M];
struct Ed {int u, v, w;} ed[M];
int n = 1, m, S, T, num, num_edge, ans, tot, g;
int h[N], fat[N], head[N], Max[N];
bool vis[N], tag[N], fromOne[N];
map<string, int> mp;
map<string, bool> b;

void add(int u, int v, int w)
{
    e[++num].next = h[u];
    e[num].to = v;
    e[num].dis = w;
    h[u] = num;
}

void add_edge(int u, int v, int w)
{
    edge[++num_edge].next = head[u];
    edge[num_edge].to = v;
    edge[num_edge].dis = w;
    head[u] = num_edge;
}

bool cmp(Ed x, Ed y) {return x.w < y.w;}

void dfs(int x)
{
    vis[x] = 1, tag[x] = 1, tot++;
    for(int i = h[x]; i != 0; i = e[i].next)
        if(e[i].to != 1 && !vis[e[i].to]) dfs(e[i].to);
}

int getFat(int x)
{
    if(x == fat[x]) return x;
    return fat[x] = getFat(fat[x]);
}

void ddfs(int x, int fat)
{
    for(int i = head[x]; i != 0; i = edge[i].next)
        if(edge[i].to != fat)
        {
            Max[edge[i].to] = max(Max[x], edge[i].dis);
            ddfs(edge[i].to, x);
        }
}

void readIn()
{
    b["Park"] = 1, mp["Park"] = 1;
    for(int i = 1; i <= m; i++)
    {
        string t1, t2;
        int u, v, w;
        cin >> t1 >> t2 >> w;
        if(!b[t1]) b[t1] = 1, mp[t1] = ++n;
        if(!b[t2]) b[t2] = 1, mp[t2] = ++n;
        u = mp[t1], v = mp[t2];
        ed[i].u = u, ed[i].v = v, ed[i].w = w;
        add(u, v, w), add(v, u, w);
    }
    cin >> S;
}

int main()
{
    cin >> m;
    readIn();
    sort(ed + 1, ed + 1 + m, cmp);
    for(int i = 1; i <= n; i++) fat[i] = i;
    for(int i = 2; i <= n; i++)
        if(!vis[i])
        {
            T++, tot = g = 0;
            memset(tag, 0, sizeof(tag));
            dfs(i);
            for(int j = 1; j <= m; j++)
            {
                if(tag[ed[j].u] && tag[ed[j].v] && getFat(ed[j].u) != getFat(ed[j].v))
                {
                    fat[getFat(ed[j].u)] = getFat(ed[j].v);
                    g++, ans += ed[j].w;
                    add_edge(ed[j].u, ed[j].v, ed[j].w), add_edge(ed[j].v, ed[j].u, ed[j].w);
                }
                if(g == tot - 1) break;
            }
            int Min = inf, pos;
            for(int j = h[1]; j != 0; j = e[j].next)
                if(tag[e[j].to] && e[j].dis < Min) Min = e[j].dis, pos = e[j].to;
            ans += Min, fromOne[pos] = 1;
            add_edge(1, pos, Min), add_edge(pos, 1, Min);
        }
    Max[1] = -inf, ddfs(1, 0);
    for(int k = 1; k <= S - T; k++)
    {
        int flag = 0, cost = -inf, pos;
        for(int i = h[1]; i != 0; i = e[i].next)
            if(!fromOne[e[i].to] && cost < (Max[e[i].to] - e[i].dis) && Max[e[i].to] - e[i].dis > 0)
                cost = Max[e[i].to] - e[i].dis, pos = e[i].to, flag = 1;
        if(!flag) break;
        ans -= cost, fromOne[pos] = 1;
    }
    cout << "Total miles driven: " << ans;
    return 0;
}

标签:num,int,公园,Park,fat,edge,野餐,规划,AcWing
来源: https://www.cnblogs.com/BigYellowDog/p/11386358.html