|
| 1 | +import fs from 'fs-extra' |
| 2 | +import { createReader, loadFixtures, locateFixtures } from 'jest.setup' |
| 3 | +import path from 'path' |
| 4 | +import { createMcmf } from '../src' |
| 5 | + |
| 6 | +describe('codeforces', function () { |
| 7 | + describe('contest 1082', function () { |
| 8 | + test('g', function () { |
| 9 | + const mcmf = createMcmf() |
| 10 | + function solve(content: string): number { |
| 11 | + const io = createReader(content) |
| 12 | + const [n, m] = io.readIntegersOfLine() |
| 13 | + |
| 14 | + const source = 0 |
| 15 | + const target: number = n + m + 1 |
| 16 | + mcmf.init(source, target, n + m + 2, n + m * 3) |
| 17 | + |
| 18 | + const nodes: number[] = io.readIntegersOfLine() |
| 19 | + for (let i = 0; i < n; ++i) { |
| 20 | + const weight: number = nodes[i] |
| 21 | + mcmf.addEdge(i + 1, target, weight, 1) |
| 22 | + } |
| 23 | + |
| 24 | + let answer = 0 |
| 25 | + for (let i = 1; i <= m; ++i) { |
| 26 | + const [u, v, weight] = io.readIntegersOfLine() |
| 27 | + const x = n + i |
| 28 | + answer += weight |
| 29 | + mcmf.addEdge(source, x, weight, 1) |
| 30 | + mcmf.addEdge(x, u, Number.MAX_SAFE_INTEGER, 1) |
| 31 | + mcmf.addEdge(x, v, Number.MAX_SAFE_INTEGER, 1) |
| 32 | + } |
| 33 | + |
| 34 | + const [mincost, maxflow] = mcmf.minCostMaxFlow() |
| 35 | + answer -= maxflow |
| 36 | + return answer |
| 37 | + } |
| 38 | + |
| 39 | + const caseDir: string = locateFixtures('codeforces/1082/g') |
| 40 | + const filenames: string[] = fs.readdirSync(caseDir).sort() |
| 41 | + for (const filename of filenames) { |
| 42 | + if (filename.endsWith('.in')) { |
| 43 | + const inputFilepath = path.join(caseDir, filename) |
| 44 | + const content = loadFixtures(inputFilepath) |
| 45 | + const output: number = solve(content) |
| 46 | + |
| 47 | + const answerFilepath = inputFilepath.replace(/\.in$/, '.out') |
| 48 | + const answer = Number(loadFixtures(answerFilepath)) |
| 49 | + // eslint-disable-next-line jest/no-conditional-expect |
| 50 | + expect(output).toEqual(answer) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + mcmf.solve(context => expect(Object.keys(context)).toMatchSnapshot()) |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + describe('contest 277', function () { |
| 59 | + test('e', function () { |
| 60 | + const mcmf = createMcmf() |
| 61 | + |
| 62 | + const caseDir: string = locateFixtures('codeforces/0277/e') |
| 63 | + const filenames: string[] = fs.readdirSync(caseDir).sort() |
| 64 | + for (const filename of filenames) { |
| 65 | + if (filename.endsWith('.in')) { |
| 66 | + const inputFilepath = path.join(caseDir, filename) |
| 67 | + const content = loadFixtures(inputFilepath) |
| 68 | + const output: number = solve(content) |
| 69 | + const answerFilepath = inputFilepath.replace(/\.in$/, '.out') |
| 70 | + const answer = Number(loadFixtures(answerFilepath)) |
| 71 | + // eslint-disable-next-line jest/no-conditional-expect |
| 72 | + expect(Math.abs(output - answer)).toBeLessThanOrEqual(1e-6) |
| 73 | + } |
| 74 | + } |
| 75 | + mcmf.solve(context => expect(Object.keys(context)).toMatchSnapshot()) |
| 76 | + |
| 77 | + function solve(content: string): number { |
| 78 | + const io = createReader(content) |
| 79 | + |
| 80 | + const [N] = io.readIntegersOfLine() |
| 81 | + |
| 82 | + const vertexes: Vertex[] = new Array(N) |
| 83 | + for (let i = 0; i < N; ++i) { |
| 84 | + const [x, y] = io.readIntegersOfLine() |
| 85 | + vertexes[i] = { x, y } |
| 86 | + } |
| 87 | + vertexes.sort((p, q) => { |
| 88 | + if (p.y === q.y) return p.x - q.x |
| 89 | + return q.y - p.y |
| 90 | + }) |
| 91 | + |
| 92 | + const source = 0 |
| 93 | + const target: number = N * 2 + 1 |
| 94 | + mcmf.init(source, target, N * 2 + 2, N * N + 2 * N) |
| 95 | + |
| 96 | + for (let i = 0; i < N; ++i) { |
| 97 | + mcmf.addEdge(source, i + 1, 2, 0) |
| 98 | + mcmf.addEdge(N + i + 1, target, 1, 0) |
| 99 | + for (let j = i + 1; j < N; ++j) { |
| 100 | + if (vertexes[i].y === vertexes[j].y) continue |
| 101 | + mcmf.addEdge(i + 1, N + j + 1, 1, dist(vertexes[i], vertexes[j])) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + const [mincost, maxflow] = mcmf.minCostMaxFlow() |
| 106 | + const answer = maxflow === N - 1 ? mincost : -1 |
| 107 | + return answer |
| 108 | + } |
| 109 | + |
| 110 | + function dist(p: Vertex, q: Vertex): number { |
| 111 | + const d = (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y) |
| 112 | + return Math.sqrt(d) |
| 113 | + } |
| 114 | + |
| 115 | + interface Vertex { |
| 116 | + x: number |
| 117 | + y: number |
| 118 | + } |
| 119 | + }) |
| 120 | + }) |
| 121 | +}) |
0 commit comments