-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMCMF(adjList)anurag.cpp
executable file
·142 lines (127 loc) · 3.71 KB
/
MCMF(adjList)anurag.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long int llu;
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define mem(a, v) memset(a, v, sizeof(a))
#define PI acos(-1)
#define S(a) scanf("%d",&a)
#define SL(a) scanf("%lld",&a)
#define S2(a, b) scanf("%d%d",&a,&b)
#define nl printf("\n")
#define deb(x) cout<<#x<<" : "<<x<<endl;
#define deb2(x, y) cout<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<endl;
#define debv(x) {cout<<#x<<" : "<<endl; for(int ii =0; ii < x.size(); ii++) cout<<x[ii]<<" "; cout<<endl; }
#define debarr(x, xs) {cout<<#x<<" : "<<endl; for(int ii =0; ii < xs; ii++) cout<<x[ii]<<" "; cout<<endl; }
const ll mod = 1000000007LL;
const int lmt = 1000005;
const int INF = 0x3f3f3f3f;
struct edge {
int u, v;
int flow;
int cost;
edge() {}
edge(int u, int v, int f, int c) : u(u), v(v), flow(f), cost(c) {}
};
struct MinCostMaxFlow {
int N;
vector < vector <int> > G;
vector <edge> E;
int numEdges;
vector <int> found, dad;
vector <int> dist;
MinCostMaxFlow(int N):
N(N), G(N), found(N), dist(N), dad(N), numEdges(0) {}
void addEdge(int from, int to, int capacity, int cost) {
// dbg(from), dbg(to), dbg(capacity), dbg(cost), dbn;
G[from].push_back(numEdges++);
E.push_back(edge(from, to, capacity, cost));
G[to].push_back(numEdges++);
E.push_back(edge(to, from, 0, int(-1) * cost));
}
bool spfa(int s, int t) {
fill(dad.begin(), dad.end(), -1);
fill(dist.begin(), dist.end(), INF);
fill(found.begin(), found.end(), 0);
queue <int> Q;
dist[s] = 0;
Q.push(s);
found[s] = true;
while(!Q.empty()) {
int u = Q.front(); Q.pop();
if(u == t) continue;
for (int i = 0; i < G[u].size(); ++i) {
edge &pres = E[G[u][i]];
int v = pres.v;
if(pres.flow <= 0) continue;
if(dist[u] + pres.cost < dist[v]) {
dad[v] = G[u][i];
dist[v] = dist[u] + pres.cost;
if(!found[v]) Q.push(v), found[v] = true;
}
}
found[u] = false;
}
return (dad[t] != -1);
}
int dfs(int s,int t) {
int flow = INF;
for(int i = dad[t]; i != -1; i = dad[E[i].u]) {
if(E[i].flow < flow) flow = E[i].flow;
}
for(int i = dad[t]; i != -1; i = dad[E[i].u]) {
E[i].flow -= flow;
E[i ^ 1].flow += flow;
}
return flow;
}
pair <int, int> getMaxFlow(int s, int t) {
int totflow = 0;
int totcost = 0;
while(spfa(s,t)) {
int amt = dfs(s,t);
totflow += amt;
totcost += dist[t] * (int)amt;
}
return make_pair(totflow, totcost);
}
};
int wt[105];
string str[105];
int main(){
int n, m, x;
S(n);
string s;
cin>>s;
S(m);
for(int i = 0; i < m; i++)
cin>>str[i]>>wt[i];
S(x);
int sor = n + 1, sink = n;
MinCostMaxFlow mcmf(n + 2);
mcmf.addEdge(sor, 0, x, 0);
for(int i = 0; i < n; i++) {
mcmf.addEdge(i, i+1, x, 0);
}
for(int i = 0; i < m; i++) {
int sz = str[i].size();
for(int j = 0; j < n - sz + 1; j++) {
bool flag = true;
for(int k = 0; k < sz; k++) {
if(str[i][k] != s[j+k]) {
flag = false;
break;
}
}
if(flag) {
mcmf.addEdge(j, j+sz, 1, -wt[i]);
}
}
}
pair<int, int> ans = mcmf.getMaxFlow(sor, n);
cout<<-1*ans.Y;nl;
return 0;
}