結果

問題 No.119 旅行のツアーの問題
ユーザー aru aruaru aru
提出日時 2020-08-12 18:25:17
言語 Go
(1.22.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,567 bytes
コンパイル時間 12,571 ms
コンパイル使用メモリ 240,808 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 00:47:30
合計ジャッジ時間 13,291 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
)

func out(x ...interface{}) {
	fmt.Println(x...)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = getInt()
	}
	return ret
}

func getString() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

// 最大流(Dinic)
type edge struct {
	to, cap, rev int
}

// G :
var G [][]edge
var level []int
var iter []int

func initG(N int) {
	G = make([][]edge, N)
	level = make([]int, N)
	iter = make([]int, N)
}

func addEdge(from, to, cap int) {
	G[from] = append(G[from], edge{to, cap, len(G[to])})
	G[to] = append(G[to], edge{from, 0, len(G[from]) - 1})
}

func bfs(s int) {
	for i := 0; i < len(level); i++ {
		level[i] = -1
	}
	que := []int{s}
	level[s] = 0
	for len(que) > 0 {
		v := que[0]
		que = que[1:]
		for _, e := range G[v] {
			if e.cap > 0 && level[e.to] < 0 {
				level[e.to] = level[v] + 1
				que = append(que, e.to)
			}
		}
	}
}

func dfs(v, t, f int) int {
	if v == t {
		return f
	}
	for i, e := range G[v] {
		if e.cap > 0 && level[v] < level[e.to] {
			d := dfs(e.to, t, min(f, e.cap))
			if d > 0 {
				G[v][i].cap -= d
				G[e.to][e.rev].cap += d
				return d
			}
		}
	}
	return 0
}

const inf = math.MaxInt64 >> 10

func maxFlow(s, t int) int {
	flow := 0
	N := len(iter)
	for {
		bfs(s)
		if level[t] < 0 {
			return flow
		}
		iter = make([]int, N)
		for {
			f := dfs(s, t, inf)
			if f <= 0 {
				break
			}
			flow += f
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)
	N := getInt()

	initG(N*2 + 2)
	s := 0
	e := N*2 + 2 - 1
	ans := 0
	for i := 0; i < N; i++ {
		b, c := getInt(), getInt()
		addEdge(s, i+1, b)
		addEdge(i+N+1, e, c)
		addEdge(i+1, i+N+1, inf)
		ans += b + c
	}
	M := getInt()
	for i := 0; i < M; i++ {
		d, e := getInt(), getInt()
		addEdge(d+1, e+1+N, inf)
	}
	ans -= maxFlow(s, e)
	out(ans)
}
0