Algoogle

Algorithm for Programming Contest

AOJ 2328 Mobile Network

Category: AOJ Tag: max-flow, parsing

Mobile Network

問題概要


容量がx>=0で単調非減少なxについての多項式のグラフが与えられる. 頂点1から頂点nへの最大流を求めよ.

解法


多項式の比較は問題に書いてない気がするけどxが十分に大きな値をとったときの多項式の値で比較,つまり次数が大きい方から係数を比較して通った.
あとはそれで最大流を求めるだけ.

コード


(2328.cpp) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <bits/stdc++.h>
using namespace std;

struct capacity
{
        int pl[64];
        capacity() { memset(pl,0,sizeof(pl));}
        capacity(int v) { memset(pl,0,sizeof(pl)); pl[0] = v;}
        capacity(const string &s) {
                memset(pl,0,sizeof(pl));
                int p = 0, n = s.size();
                while(p < n) {
                        int a = 0, b = 0;
                        while(p < n and s[p] != 'x') a = 10*a+s[p++]-'0';
                        if(p == n) {
                                pl[0] = a;
                                break;
                        }
                        p++;
                        if(!a) a++;
                        if(p == n or s[p] != '^') pl[1] = a;
                        else {
                                p++;
                                while(p < n and s[p] != '+') b = 10*b+s[p++]-'0';
                                pl[b] = a;
                        }
                        p++;
                }
        }

        string to_s()
        {
                string ret = "";
                for (int i = 51; i > 1; i--)
                        if(pl[i]) ret += (pl[i]>1? to_string(pl[i]): "") + "x^" + to_string(i) + "+";
                if(pl[1]) ret += (pl[1]>1? to_string(pl[1]): "") + "x+";
                if(pl[0]) ret += to_string(pl[0])+"+";
                if(ret.size()) return ret.substr(0,ret.size()-1);
                return "0";
        }

        capacity operator-=(const capacity &c) { for (int i = 0; i < 52; i++) pl[i] -= c.pl[i]; return *this; }
        capacity operator-(const capacity &c) const { return capacity(*this) -= c; }
        capacity operator+=(const capacity &c) { for (int i = 0; i < 52; i++) pl[i] += c.pl[i]; return *this; }
        capacity operator+(const capacity &c) const { return capacity(*this) += c; }

        bool operator<(const capacity &c) const {
                int k = 51;
                while(k and pl[k] == c.pl[k]) k--;
                return pl[k] < c.pl[k];
        }
        bool operator>(const capacity &c) const {
                int k = 51;
                while(k and pl[k] == c.pl[k]) k--;
                return pl[k] > c.pl[k];
        }
};

const capacity inf("x^51"), zero;

struct max_flow
{
        struct edge
        {
                int to, rev;
                capacity cap;
                edge(int to, int rev, capacity cap) : to(to), rev(rev), cap(cap) {}
        };
        int V;
        vector<vector<edge>> G;
        vector<int> itr, level;

        max_flow(){}
        max_flow(int V) : V(V) { G.assign(V,vector<edge>());}

        void add_edge(int from, int to, capacity cap)
        {
                G[from].push_back(edge(to, G[to].size(), cap));
                G[to].push_back(edge(from, G[from].size()-1, 0));
        }

        void bfs(int s)
        {
                level.assign(V,-1);
                queue<int> q;
                level[s] = 0; q.push(s);
                while (!q.empty()) {
                        int v = q.front(); q.pop();
                        for(auto &e: G[v]){
                                if (e.cap > zero and level[e.to] < 0) {
                                        level[e.to] = level[v] + 1;
                                        q.push(e.to);
                                }
                        }
                }
        }

        capacity dfs(int v, int t, capacity f)
        {
                if (v == t) return f;
                for (int& i = itr[v]; i < (int) G[v].size(); ++i) {
                        edge& e = G[v][i];
                        if (e.cap > zero and level[v] < level[e.to]) {
                                auto d = dfs(e.to, t, min(f, e.cap));
                                if (d > 0) {
                                        e.cap -= d;
                                        G[e.to][e.rev].cap += d;
                                        return d;
                                }
                        }
                }
                return zero;
        }

        capacity run(int s, int t)
        {
                capacity ret = 0, f;
                while (bfs(s), level[t] >= 0) {
                        itr.assign(V,0);
                        while ((f = dfs(s, t, inf)) > zero) ret += f;
                }
                return ret;
        }
};

int n, m;
max_flow mf;

bool input()
{
        cin >> n >> m;
        mf = max_flow(n);
        for (int i = 0; i < m; i++) {
                int a, b; string s;
                cin >> a >> b >> s;
                a--; b--;
                capacity c(s);
                mf.add_edge(a,b,c);
                mf.add_edge(b,a,c);
        }
        return n or m;
}

int main()
{
        while(input()) cout << mf.run(0,n-1).to_s() << endl;
        return 0;
}

Comments