HDU 3720

    沒什麼好說的,最近太水,各種犯2...
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <iostream>
#include <sstream>

using namespace std;

const int inf = 1000000000;

struct Player {
    string name;
    int ability;
    string position;
    
    bool operator < (const Player &oth) const {
        return position < oth.position;
    }
} player[23];

int additional[23][23];

int find(const string &name) {
    for (int i = 0; ; i++)
        if (player[i].name == name) return i;
}

int pd, pg, pm, ps;     // 分別是第一個後衛、第一個守門員、第一個中場、第一個前鋒在player中的位置。
vector<int> select;
int ans;

void dfs(int x) {
    if (select.size() >= 11) {
        int tmp = 0;
        for (int i = 0; i < 11; i++) {
            tmp += player[select[i]].ability;
            for (int j = i + 1; j < 11; j++)
                tmp += additional[select[i]][select[j]];
        }
        if (tmp > ans) ans = tmp;
        return;
    }
    if (x >= 23) return;
    int y = x + 1;
    if (select.size() >= 9) {
        if (23 - y >= 11 - select.size()) dfs(y);   // 如果可以不選x
        select.push_back(x);
        if (select.size() == 11) y = 23;            // 如果選了x,已經有11人被選
        dfs(y);
        select.pop_back();
    } else if (select.size() >= 5) {
        if (ps - y >= 9 - select.size()) dfs(y);    // 如果可以不選x
        select.push_back(x);
        if (select.size() == 9) y = ps;             // 如果選了x,已經有9人被選(不需要中場了)
        dfs(y);
        select.pop_back();
    } else if (select.size() >= 4) {
        if (pm - y >= 5 - select.size()) dfs(y);    // 如果可以不選x
        select.push_back(x);
        if (select.size() == 5) y = pm;             // 如果選了x,已經有5人被選(不需要守門員了)
        dfs(y);
        select.pop_back();
    } else {
        if (pg - y >= 4 - select.size()) dfs(y);    // 如果可以不選x
        select.push_back(x);
        if (select.size() == 4) y = pg;             // 如果選了x,已經有4人被選(不需要後衛了)
        dfs(y);
        select.pop_back();
    }
}

int main() {
    int i, m, a, b, profit;
    string namea, nameb;
    while (cin >> player[0].name >> player[0].ability >> player[0].position) {
        for (i = 1; i < 23; i++)
            cin >> player[i].name >> player[i].ability >> player[i].position;
        sort(player, player + 23);
        cin >> m;
        memset(additional, 0, sizeof(additional));
        for (i = 0; i < m; i++) {
            cin >> namea >> nameb >> profit;
            a = find(namea);
            b = find(nameb);
            additional[a][b] =
            additional[b][a] = profit;
        }
        pd = pg = pm = ps = -1;
        for (i = 0; i < 23; i++) {
            switch (player[i].position[0]) {
            case 'd': if (pd == -1) pd = i; break;
            case 'g': if (pg == -1) pg = i; break;
            case 'm': if (pm == -1) pm = i; break;
            case 's': if (ps == -1) ps = i; break;
            }
        }
        if (pd == -1 || pg == -1 || pm == -1 || ps == -1 ||
            pg - pd < 4 || pm - pg < 1 || ps - pm < 4 || 23 - ps < 2) {
            cout << "impossible" << endl;
            continue;
        }
        ans = -inf;
        dfs(0);
        cout << ans << endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章