1. 程式人生 > >Codeforces 1082 毛毛蟲圖構造&最大權閉合子圖

Codeforces 1082 毛毛蟲圖構造&最大權閉合子圖

A

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 55, MAXM = 1000;
//int to[MAXM << 1], nxt[MAXM << 1], Head[MAXN], ed = 1;
//int cost[MAXM << 1];
//inline void addedge(int u, int v, int c)
//{
//    to[++ed] = v;
//    nxt[ed] = Head[u];
//    cost[ed] = c;
// Head[u] = ed; //} inline void read(ll &v) { v = 0; char c = 0; int p = 1; while (c < '0' || c > '9') { if (c == '-') { p = -1; } c = getchar(); } while (c >= '0' && c <= '9') { v = (v << 3) + (v << 1
) + c - '0'; c = getchar(); } v *= p; } int main() { ll T; read(T); while (T--) { ll n, x, y, d; read(n), read(x), read(y), read(d); ll cha = abs(x - y); if (cha % d == 0) { ll ans = cha / d; printf("%I64d\n", ans);
continue; } else { ll anser = LLONG_MAX; ll ans1, ans2; ans1 = ans2 = LLONG_MAX; if ((y - 1) % d == 0) { ans1 = (x - 1) / d; if ((x - 1) % d) { ans1++; } ans1 += (y - 1) / d; } if ((n - y) % d == 0) { ans2 = (n - x) / d; if ((n - x) % d) { ans2++; } ans2 += (n - y) / d; } anser = min(anser, min(ans1, ans2)); if (anser == LLONG_MAX) { printf("-1\n"); } else { printf("%I64d\n", anser); } } } }
View Code

B

注意一下坑點即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 55, MAXM = 1000;
//int to[MAXM << 1], nxt[MAXM << 1], Head[MAXN], ed = 1;
//int cost[MAXM << 1];
//inline void addedge(int u, int v, int c)
//{
//    to[++ed] = v;
//    nxt[ed] = Head[u];
//    cost[ed] = c;
//    Head[u] = ed;
//}
inline void read(int &v)
{
    v = 0;
    char c = 0;
    int p = 1;
    while (c < '0' || c > '9') {
        if (c == '-') {
            p = -1;
        }
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        v = (v << 3) + (v << 1) + c - '0';
        c = getchar();
    }
    v *= p;
}
vector<pair<int, char> > anss;
queue<pair<int, char> > que;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    string a;
    cin >> a;
    pair<int, char> cnt, zz;
    cnt.first = cnt.second = 0;
    int sum = 0;
    for (int i = 0; i <= n; i++) {
        if (i == n) {
            anss.push_back(cnt);
            break;
        }
        if (a[i] != cnt.second) {
            if (cnt.first != 0) {
                anss.push_back(cnt);
            }
            cnt.first = 1;
            cnt.second = a[i];
        } else {
            cnt.first++;
        }
    }
    int sz = anss.size();
    for (int i = 0; i < sz; i++) {
        if (anss[i].second == 'G') {
            sum += anss[i].first;
        }
    }
    int ans = 0;
    for (int i = 0; i < sz; i++) {
        cnt = anss[i];
        if (cnt.second == 'G') {
            if (cnt.first == sum) {
                ans = max(ans, cnt.first);
            } else {
                ans = max(ans, cnt.first + 1);
            }
        } else {
            if (cnt.first == 1) {
                if (i > 0 && i < sz - 1) {
                    ans = max(ans, anss[i - 1].first + anss[i + 1].first);
                    if (anss[i - 1].first + anss[i + 1].first != sum) {
                        ans = max(ans, anss[i - 1].first + anss[i + 1].first + 1);
                    }
                }
            }
        }
    }
    cout << ans << endl;
}
View Code

C

sort一下算下貢獻即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 55, MAXM = 1000;
//int to[MAXM << 1], nxt[MAXM << 1], Head[MAXN], ed = 1;
//int cost[MAXM << 1];
//inline void addedge(int u, int v, int c)
//{
//    to[++ed] = v;
//    nxt[ed] = Head[u];
//    cost[ed] = c;
//    Head[u] = ed;
//}
inline void read(int &v)
{
    v = 0;
    char c = 0;
    int p = 1;
    while (c < '0' || c > '9') {
        if (c == '-') {
            p = -1;
        }
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        v = (v << 3) + (v << 1) + c - '0';
        c = getchar();
    }
    v *= p;
}
int pre[100005];
vector<int> num[100005];
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    int u, v;
    for (int i = 1; i <= n; i++) {
        cin >> u >> v;
        num[u].push_back(v);
    }
    for (int i = 1; i <= m; i++) {
        if (num[i].size()) {
            sort(num[i].begin(), num[i].end());
        }
    }
    int ans = 0;
    for (int i = 1; i <= m; i++) {
        int len = num[i].size();
        if (len) {
            int sum = 0;
            for (int j = 0; j < len; j++) {
                sum += num[i][len - j - 1];
                //cout<<i<<" "<<j<<" "<<sum<<endl;
                if (sum > 0) {
                    pre[j + 1] += sum;
                } else {
                    break;
                }
            }
        }
    }
    for (int i = 1; i <= 100000; i++) {
        ans = max(ans, pre[i]);
    }
    cout << ans << endl;
}
View Code

D

構造題 毛毛蟲圖

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1000005;
inline void read(int &v) {
        v = 0;
        char c = 0;
        int p = 1;
        while (c < '0' || c > '9') {
                if (c == '-') {
                        p = -1;
                }
                c = getchar();
        }
        while (c >= '0' && c <= '9') {
                v = (v << 3) + (v << 1) + c - '0';
                c = getchar();
        }
        v *= p;
}
int n, m, a[MAXN], b[MAXN], f[MAXN], s[MAXN], t[MAXN], an, sm, T, z;
int main() {
        int i, j = 1, a1, a2, e = 0;
        read(n);
        for (int i = 1; i <= n; i++) {
                read(a[i]);
                sm += a[i];
        }
        if (sm < n * 2 - 2) {
                cout << "NO" << endl;
                return 0;
        }
        for (int i = 1; i <= n; i++) {
                if (a[i] >= 2) {
                        f[i] = 1;
                        b[++z] = i;
                        if (z >= 2) {
                                s[z] = b[z - 1];
                                t[z] = i;
                        }
                }
        }
        for (int i = 1; i <= z; i++) {
                a[b[i]] -= (i != 1) + (i != z);
        }
        cout << "YES " << min(z + 1, n - 1) << endl << n - 1 << endl;
        for (int i = 1; i <= n; i++) {
                if (a[i] == 1 && !f[i]) {
                        if (!e) {
                                e = 1;
                                a[b[z]]--;
                                s[++z] = i;
                                t[z] = b[z - 1];
                        } else {
                                for (; !a[b[j]]; j++);
                                a[b[j]]--;
                                s[++z] = i;
                                t[z] = b[j];
                        }
                }
        }
        for (int i = 2; i <= n; i++) {
                cout << s[i] << " " << t[i] << endl;
        }
}
View Code

G

把每條邊當做一個點 搞最大權閉合子圖即可

//Netflow dumpling
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 10050;
const int MAXM = 20000;
const ll INF = 100000000000000000;
ll Head[MAXN], cur[MAXN], lev[MAXN], to[MAXM << 1], nxt[MAXM << 1], ed, S, T;
ll f[MAXM << 1];
inline void addedge(int u, int v, ll cap)
{
    to[++ed] = v;
    nxt[ed] = Head[u];
    Head[u] = ed;
    f[ed] = cap;
    to[++ed] = u;
    nxt[ed] = Head[v];
    Head[v] = ed;
    f[ed] = 0;
    return;
}
inline bool BFS()
{
    int u;
    memset(lev, -1, sizeof(lev));
    queue<int>q;
    lev[S] = 0;
    q.push(S);
    while (q.size()) {
        u = q.front();
        q.pop();
        for (int i = Head[u]; i; i = nxt[i])
            if (f[i] && lev[to[i]] == -1) {
                lev[to[i]] = lev[u] + 1;
                q.push(to[i]);
                /*
                if (to[i] == T)
                {
                        return 1;
                }
                magic one way optimize
                */
            }
    }
    memcpy(cur, Head, sizeof Head);
    return lev[T] != -1;
}
inline ll DFS(int u, ll maxf)
{
    if (u == T || !maxf) {
        return maxf;
    }
    ll cnt = 0;
    for (ll &i = cur[u], tem; i; i = nxt[i])
        if (f[i] && lev[to[i]] == lev[u] + 1) {
            tem = DFS(to[i], min(maxf, f[i]));
            maxf -= tem;
            f[i] -= tem;
            f[i ^ 1] += tem;
            cnt += tem;
            if (!maxf) {
                break;
            }
        }
    if (!cnt) {
        lev[u] = -1;
    }
    return cnt;
}
ll Dinic()
{
    ll ans = 0;
    while (BFS()) {
        ans += DFS(S, INF);
    }
    return ans;
}
void init(int SS, int TT)
{
    memset(Head, 0, sizeof(Head));
    ed = 1;
    S = SS;
    T = TT;
    return;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    ll u, v, c;
    cin >> n >> m;
    init(0, n + m + 1);
    for (int i = 1; i <= n; i++) {
        cin >> c;
        addedge(m + i, T, c);
    }
    ll summ = 0;
    for (int i = 1; i <= m; i++) {
        cin >> u >> v >> c;
        summ += c;
        addedge(S, i, c);
        addedge(i, m + u, INF);
        addedge(i, m + v, INF);
    }
    cout << summ - Dinic() << endl;
    return 0;
}
View Code