0%

Code

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
/*
ID: wcr19961
PROG: frameup
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cctype>
using namespace std;

typedef long long LL;
const int maxn = 33;
char s[maxn][maxn], ans[maxn];
bool vis[maxn];
bool g[maxn][maxn];
int d[maxn], cnt;
int L[maxn], R[maxn], U[maxn], D[maxn];

void proc(char c, int x) {
if (isalpha(c) && c - 'A' != x) {
g[x][c - 'A'] = true;
}
}

void build(int x) {
for (int i = L[x]; i <= R[x]; ++i) {
proc(s[U[x]][i], x);
proc(s[D[x]][i], x);
}
for (int i = U[x]; i <= D[x]; ++i) {
proc(s[i][L[x]], x);
proc(s[i][R[x]], x);
}
}

void dfs(int dep) {
if (dep == cnt) {
ans[dep] = 0;
puts(ans);
return;
}
for (int i = 0; i < maxn; ++i) {
if (vis[i] && d[i] == 0) {
vis[i] = false;
ans[dep] = 'A' + i;
for (int j = 0; j < maxn; ++j) {
if (g[i][j]) {
--d[j];
}
}
dfs(dep + 1);
vis[i] = true;
for (int j = 0; j < maxn; ++j) {
if (g[i][j]) {
++d[j];
}
}
}
}
}

int main() {
freopen("frameup.in", "r", stdin);
freopen("frameup.out", "w", stdout);
int n, m;
scanf("%d%d", &n, &m);
memset(L, 0x3f, sizeof L);
memset(U, 0x3f, sizeof U);
for (int i = 0; i < n; ++i) {
scanf("%s", s[i]);
for (int j = 0; j < m; ++j) {
if (isalpha(s[i][j])) {
int c = s[i][j] - 'A';
vis[c] = true;
L[c] = min(L[c], j);
R[c] = max(R[c], j);
U[c] = min(U[c], i);
D[c] = max(D[c], i);
}
}
}
for (int i = 0; i < maxn; ++i) {
if (vis[i]) {
++cnt;
build(i);
}
}
for (int i = 0; i < maxn; ++i) {
for (int j = 0; j < maxn; ++j) {
if (g[i][j] && i != j) {
++d[j];
}
}
}
dfs(0);
return 0;
}

Code

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
 ID: wcr19961
PROG: milk6
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

typedef long long LL;
const LL inf = 0x3f3f3f3f3f3f3f3fLL;
const int maxn = 110;
const int maxm = 1010;
int ans[maxm];
int u[maxm], v[maxm], c[maxm];
LL cap[maxm];

struct Edge {
int from, to;
LL cap, flow;
Edge(int u, int v, LL c, LL f): from(u), to(v), cap(c), flow(f) {}
};

bool operator < (const Edge& a, const Edge& b) {
return a.from < b.from || (a.from == b.from && a.to < b.to);
}

struct ISAP {
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn]; // 邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
bool vis[maxn]; // BFS使用
int d[maxn]; // 从起点到i的距离
int cur[maxn]; // 当前弧指针
int p[maxn]; // 可增广路上的上一条弧
int num[maxn]; // 距离标号计数

void AddEdge(int from, int to, LL cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
m = int(edges.size());
G[from].push_back(m-2);
G[to].push_back(m-1);
}

bool BFS() {
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(t);
vis[t] = 1;
d[t] = 0;
while(!Q.empty()) {
int x = Q.front(); Q.pop();
for(int i = 0; i < G[x].size(); i++) {
Edge& e = edges[G[x][i]^1];
if(!vis[e.from] && e.cap > e.flow) {
vis[e.from] = 1;
d[e.from] = d[x] + 1;
Q.push(e.from);
}
}
}
return vis[s];
}

void ClearAll(int n) {
this->n = n;
for(int i = 0; i < n; i++) G[i].clear();
edges.clear();
}

void ClearFlow() {
for(int i = 0; i < edges.size(); i++) edges[i].flow = 0;
}

LL Augment() {
LL x = t, a = inf;
while(x != s) {
Edge& e = edges[p[x]];
a = min(a, e.cap-e.flow);
x = edges[p[x]].from;
}
x = t;
while(x != s) {
edges[p[x]].flow += a;
edges[p[x]^1].flow -= a;
x = edges[p[x]].from;
}
return a;
}

LL Maxflow(int s, int t) {
this->s = s; this->t = t;
LL flow = 0;
BFS();
memset(num, 0, sizeof(num));
for(int i = 0; i < n; i++) num[d[i]]++;
int x = s;
memset(cur, 0, sizeof(cur));
while(d[s] < n) {
if(x == t) {
flow += Augment();
x = s;
}
int ok = 0;
for(int i = cur[x]; i < G[x].size(); i++) {
Edge& e = edges[G[x][i]];
if(e.cap > e.flow && d[x] == d[e.to] + 1) { // Advance
ok = 1;
p[e.to] = G[x][i];
cur[x] = i; // 注意
x = e.to;
break;
}
}
if(!ok) { // Retreat
int m = n-1; // 初值注意
for(int i = 0; i < G[x].size(); i++) {
Edge& e = edges[G[x][i]];
if(e.cap > e.flow) m = min(m, d[e.to]);
}
if(--num[d[x]] == 0) break;
num[d[x] = m+1]++;
cur[x] = 0; // 注意
if(x != s) x = edges[p[x]].from;
}
}
return flow;
}
} isap;


int main() {
freopen("milk6.in", "r", stdin);
freopen("milk6.out", "w", stdout);
int n, m;
scanf("%d%d", &n, &m);
isap.ClearAll(n + 1);
for (int i = 0; i < m; ++i) {
scanf("%d%d%d", &u[i], &v[i], &c[i]);
cap[i] = LL(c[i]) * 1001 + 1;
isap.AddEdge(u[i], v[i], cap[i]);
}
LL aim = isap.Maxflow(1, n);
int cost = aim / 1001, num = aim % 1001;
printf("%d %d\n", cost, num);
int cnt = 0;
for (int i = 0; i < m && cnt < num; ++i) {
isap.ClearFlow();
isap.edges[i << 1].cap = 0;
LL tmp = isap.Maxflow(1, n);
if (tmp + cap[i] == aim) {
aim -= cap[i];
ans[cnt++] = i + 1;
} else {
isap.edges[i << 1].cap = cap[i];
}
}
for (int i = 0; i < num; ++i) {
printf("%d\n", ans[i]);
}
return 0;
}

Code

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
/*
ID: wcr19961
PROG: shuttle
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

const int maxn = 10;
int n, cnt;

void print(int x) {
printf("%d%c",x, ++cnt % 20 == 0 ? '\n' : ' ');
}

int main() {
freopen("shuttle.in", "r", stdin);
freopen("shuttle.out", "w", stdout);
int n, cnt = 0;
scanf("%d", &n);
int p = n, sig = 1, sum = n * (n + 2);
print(p);
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
print(p += 2 * sig);
}
print(p += sig);
sig = -sig;
}
for (int i = 0; i < n; ++i) {
print(p += 2 * sig);
}
sig = -sig;
for (int i = n - 1; i > 0; --i) {
print(p += sig);
for (int j = 0; j < i; ++j) {
print(p += 2 * sig);
}
sig = -sig;
}
printf("%d\n", --p);
return 0;
}

Code

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
/*
ID: wcr19961
PROG: lgame
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

const int maxn = 10;
const int score[] = {
2, 5, 4, 4, 1, 6, 5, 5, 1, 7,
6, 3, 5, 2, 3, 5, 7, 2, 1, 2,
4, 6, 6, 7, 5, 7
};
int ans;
char tmp[maxn];
vector<string> S;
vector<pair<string, string> > anss;

int getScore(string s) {
int res = 0;
for (int i = 0; i < s.length(); ++i) {
res += score[s[i] - 'a'];
}
return res;
}

void proc(string s1, string s2) {
if (binary_search(S.begin(), S.end(), s1) && binary_search(S.begin(), S.end(), s2)) {
int x = getScore(s1) + getScore(s2);
if (x >= ans) {
if (s2 < s1 || s1 == "") {
swap(s1, s2);
}
if (x > ans) {
ans = x;
anss.clear();
}
anss.push_back(make_pair(s1, s2));
}
}
}

int main() {
freopen("lgame.in", "r", stdin);
freopen("lgame.out", "w", stdout);
FILE* Dict = fopen("lgame.dict", "r");
while (~fscanf(Dict, "%s", tmp) && tmp[0] != '.') {
S.push_back(tmp);
}
S.push_back("");
sort(S.begin(), S.end());
scanf("%s", tmp);
string s = tmp;
sort(s.begin(), s.end());
do {
for (int i = 3; i <= s.length(); ++i) {
proc("", s.substr(0, i));
for (int j = 3; i + j <= s.length(); ++j) {
proc(s.substr(0, i), s.substr(i, j));
}
}
} while (next_permutation(s.begin(), s.end()));
printf("%d\n", ans);
sort(anss.begin(), anss.end());
ans = int(unique(anss.begin(), anss.end()) - anss.begin());
for (int i = 0; i < ans; ++i) {
printf("%s", anss[i].first.c_str());
if (anss[i].second != "") {
printf(" %s", anss[i].second.c_str());
}
puts("");
}
return 0;
}

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
/*
ID: wcr19961
PROG: race3
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

const int maxn = 55;
vector<int> g[maxn];
int vis[maxn];
int n, num1, num2;
int ans1[maxn], ans2[maxn];

bool bfs(int u) {
queue<int> q;
q.push(0);
vis[0] = 1;
while (!q.empty()) {
int k = q.front();
q.pop();
if (k == n) {
return false;
}
for (int i = 0; i < g[k].size(); ++i) {
int x = g[k][i];
if (x != u && vis[x] != 1) {
vis[x] = 1;
q.push(x);
}
}
}
return true;
}

bool bfs2(int u) {
queue<int> q;
q.push(u);
vis[u] = 2;
while (!q.empty()) {
int k = q.front();
q.pop();
for (int i = 0; i < g[k].size(); ++i) {
int x = g[k][i];
if (vis[x] == 1) {
return false;
}
if (vis[x] != 2) {
vis[x] = 2;
q.push(x);
}
}
}
return true;
}

int main() {
freopen("race3.in", "r", stdin);
freopen("race3.out", "w", stdout);
for (; ; ++n) {
int x;
while (~scanf("%d", &x)) {
if (x < 0) {
break;
}
g[n].push_back(x);
}
if (x == -1) {
break;
}
}
--n;
for (int i = 1; i < n; ++i) {
memset(vis, 0, sizeof vis);
if (bfs(i)) {
ans1[num1++] = i;
if (bfs2(i)) {
ans2[num2++] = i;
}
}
}
printf("%d", num1);
for (int i = 0; i < num1; ++i) {
printf(" %d", ans1[i]);
}
puts("");
printf("%d", num2);
for (int i = 0; i < num2; ++i) {
printf(" %d", ans2[i]);
}
puts("");
return 0;
}

Code

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
/*
ID: wcr19961
PROG: buylow
LANG: JAVA
*/
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.Random;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.math.*;
import java.util.*;

public class buylow {
public static void main(String[] args) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream("buylow.in");
OutputStream outputStream = new PrintStream("buylow.out");
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Slover slove = new Slover();
slove.slove(in, out);
out.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(buylow.class.getName()).log(Level.SEVERE, null, ex);
}
}

static class Slover {
public void slove(InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] a = new int[n + 2];
int[] len = new int[n + 2];
BigInteger[] dp = new BigInteger[n + 2];
for (int i = 1; i <= n; ++i) {
a[i] = in.nextInt();
len[i] = 1;
dp[i] = BigInteger.ONE;
}
dp[n + 1] = BigInteger.ONE;
++n;
int ans = 0;
for (int i = 2; i <= n; ++i) {
TreeSet<Integer> S = new TreeSet<Integer> ();
for (int j = i - 1; j > 0; --j) {
if (a[i] < a[j] && len[i] <= len[j] + 1) {
if (len[i] < len[j] + 1) {
len[i] = len[j] + 1;
dp[i] = dp[j];
if (len[i] > len[ans]) {
ans = i;
}
} else if (!S.contains(a[j])) {
dp[i] = dp[i].add(dp[j]);
}
S.add(a[j]);
}
}
}
out.println(len[ans] - 1 + " " + dp[ans]);
out.flush();
}
}

static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;

public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}

public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}

public boolean hasNext() {
try {
return reader.ready();
} catch (IOException ex) {
Logger.getLogger(buylow.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}

public long nextLong() {
return Long.parseLong(next());
}

public int nextInt() {
return Integer.parseInt(next());
}

public double nextDouble() {
return Double.parseDouble(next());
}
}
}

Code

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
/*
ID: wcr19961
PROG: job
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int maxn = 1010;
int a[maxn], b[maxn];

struct node {
int x, dt;
node(int x, int dt) : x(x), dt(dt) {}
bool operator < (const node& rhs) const {
return x > rhs.x;
}
};

void proc(int* p, int n, int m) {
priority_queue<node> q;
while (m--) {
int x;
scanf("%d", &x);
q.push(node(x, x));
}
for (int i = 0; i < n; ++i) {
node k = q.top();
q.pop();
p[i] = k.x;
k.x += k.dt;
q.push(k);
}
}

int main() {
freopen("job.in", "r", stdin);
freopen("job.out", "w", stdout);
int n, m1, m2;
scanf("%d%d%d", &n, &m1, &m2);
proc(a, n, m1);
sort(a, a + n);
proc(b, n, m2);
sort(b, b + n);
int ans = 0;
for (int i = 0; i < n; ++i) {
ans = max(ans, a[i] + b[n - i - 1]);
}
printf("%d %d\n", a[n - 1], ans);
return 0;
}

Code

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
/*
ID: wcr19961
PROG: stall4
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

const int maxn = 1010;
bool vis[maxn];
vector<int> g[maxn];
int n, from[maxn], tot;

bool match(int x) {
for(int i = 0; i < g[x].size(); ++i) {
if(!vis[g[x][i]]) {
vis[g[x][i]] = true;
if(from[g[x][i]] == -1 || match(from[g[x][i]])) {
from[g[x][i]] = x;
return true;
}
}
}
return false;
}

int Hungarian() {
tot = 0;
memset(from, -1, sizeof from);
for(int i = 0; i < n; ++i) {
memset(vis,0,sizeof vis);
if(match(i)) {
++tot;
}
}
return tot;
}

int main() {
freopen("stall4.in", "r", stdin);
freopen("stall4.out", "w", stdout);
int m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
int x, u;
scanf("%d", &x);
while (x--) {
scanf("%d", &u);
g[i].push_back(n + u);
}
}
n += m;
printf("%d\n", Hungarian());
return 0;
}

Code

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
149
150
151
152
153
154
155
/*
ID: wcr19961
PROG: ditch
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 210;

struct Edge {
int from, to, cap, flow;
Edge(int from, int to, int cap, int flow) : from(from), to(to), cap(cap), flow(flow) {}
};

bool operator < (const Edge& a, const Edge& b) {
return a.from < b.from || (a.from == b.from && a.to < b.to);
}

struct ISAP {
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn]; // 邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
bool vis[maxn]; // BFS使用
int d[maxn]; // 从起点到i的距离
int cur[maxn]; // 当前弧指针
int p[maxn]; // 可增广路上的上一条弧
int num[maxn]; // 距离标号计数

void AddEdge(int from, int to, int cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}

bool BFS() {
memset(vis, 0, sizeof vis);
queue<int> Q;
Q.push(t);
vis[t] = 1;
d[t] = 0;
while(!Q.empty()) {
int x = Q.front(); Q.pop();
for(int i = 0; i < G[x].size(); i++) {
Edge& e = edges[G[x][i]^1];
if(!vis[e.from] && e.cap > e.flow) {
vis[e.from] = 1;
d[e.from] = d[x] + 1;
Q.push(e.from);
}
}
}
return vis[s];
}

void ClearAll(int n) {
this->n = n;
for(int i = 0; i < n; i++) G[i].clear();
edges.clear();
}

void ClearFlow() {
for(int i = 0; i < edges.size(); i++) edges[i].flow = 0;
}

int Augment() {
int x = t, a = inf;
while(x != s) {
Edge& e = edges[p[x]];
a = min(a, e.cap-e.flow);
x = edges[p[x]].from;
}
x = t;
while(x != s) {
edges[p[x]].flow += a;
edges[p[x]^1].flow -= a;
x = edges[p[x]].from;
}
return a;
}

int Maxflow(int s, int t) {
this->s = s; this->t = t;
int flow = 0;
BFS();
memset(num, 0, sizeof(num));
for(int i = 0; i < n; i++) num[d[i]]++;
int x = s;
memset(cur, 0, sizeof(cur));
while(d[s] < n) {
if(x == t) {
flow += Augment();
x = s;
}
int ok = 0;
for(int i = cur[x]; i < G[x].size(); i++) {
Edge& e = edges[G[x][i]];
if(e.cap > e.flow && d[x] == d[e.to] + 1) { // Advance
ok = 1;
p[e.to] = G[x][i];
cur[x] = i; // 注意
x = e.to;
break;
}
}
if(!ok) { // Retreat
int m = n-1; // 初值注意
for(int i = 0; i < G[x].size(); i++) {
Edge& e = edges[G[x][i]];
if(e.cap > e.flow && d[e.to] != -1) m = min(m, d[e.to]);
}
if(--num[d[x]] == 0) break;
num[d[x] = m+1]++;
cur[x] = 0; // 注意
if(x != s) x = edges[p[x]].from;
}
}
return flow;
}

vector<int> Mincut() { // call this after maxflow
BFS();
vector<int> ans;
for(int i = 0; i < edges.size(); i++) {
Edge& e = edges[i];
if(!vis[e.from] && vis[e.to] && e.cap > 0) ans.push_back(i);
}
return ans;
}

void Reduce() {
for(int i = 0; i < edges.size(); i++) edges[i].cap -= edges[i].flow;
}
} isap;

int main() {
freopen("ditch.in", "r", stdin);
freopen("ditch.out", "w", stdout);
int n, m, u, v, len;
scanf("%d%d", &m, &n);
isap.ClearAll(n + 1);
for (int i = 0; i < m; ++i) {
scanf("%d%d%d", &u, &v, &len);
isap.AddEdge(u, v, len);
}
printf("%d\n", isap.Maxflow(1, n));
return 0;
}

Code

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
/*
ID: wcr19961
PROG: fence6
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 210;
int n, p[maxn], l[maxn];
int len[maxn][maxn], d[maxn][maxn];
struct node {
int p[9];
void read(int k, int n) {
p[0] = k;
for (int i = 1; i <= n; ++i) {
scanf("%d", &p[i]);
}
sort(p, p + n + 1);
}
bool check(const node& x) const {
for (int i = 0; i < 9; ++i) {
if (p[i] != x.p[i]) {
return false;
}
}
return true;
}
} a[maxn];

int Floyd() {
int res = inf;
for (int k = 0; k < n; ++k) {
for (int i = 0; i <= k; ++i) {
if (len[k][i] == inf) {
continue;
}
for (int j = i + 1; j <= k; ++j) {
res = min(res, d[i][j] + len[j][k] + len[k][i]);
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
return res;
}


int main() {
freopen("fence6.in", "r", stdin);
freopen("fence6.out", "w", stdout);
int m;
scanf("%d", &m);
for (int i = 0; i < m; ++i) {
int x, num1, num2;
scanf("%d%d%d%d", &x, &l[i], &num1, &num2);
a[i << 1].read(x, num1), a[(i << 1) + 1].read(x, num2);
}
n = m << 1;
memset(p, -1, sizeof p);
for (int i = 0; i < n; ++i) {
if (p[i] == -1) {
p[i] = i;
for (int j = i + 1; j < n; ++j) {
if (p[j] == -1 && a[i].check(a[j])) {
p[j] = p[i];
}
}
}
}
memset(d, 0x3f, sizeof d);
memset(len, 0x3f, sizeof len);
for (int i = 0; i < m; ++i) {
int u = p[i << 1], v = p[(i << 1) + 1];
d[u][v] = d[v][u] = len[u][v] = len[v][u] = l[i];
}
printf("%d\n", Floyd());
return 0;
}