Language: C++

package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	p := getNextInt64(scanner)

	edges := make([]Edge, m)

	var g Graph
	g.lto = make([][]int, n)
	g.rto = make([][]int, n)
	for i := 0; i < n; i++ {
		g.lto[i] = make([]int, 0)
		g.rto[i] = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		a := getNextInt(scanner) - 1
		b := getNextInt(scanner) - 1
		c := (getNextInt64(scanner) - p) * -1
		edges[i] = Edge{
			a:    a,
			b:    b,
			cost: c,
		}
		g.lto[a] = append(g.lto[a], b)
		g.rto[b] = append(g.rto[b], a)
	}

	g.lreach = make([]bool, n)
	g.rreach = make([]bool, n)

	g.dfs(0, false)
	g.dfs(n-1, true)

	cc := make([]int64, n)
	for i := 0; i < n; i++ {
		cc[i] = math.MaxInt64
	}

	cc[0] = 0

	for i := 0; i < n; i++ {
		for ii := 0; ii < m; ii++ {
			if cc[edges[ii].a] == math.MaxInt64 {
				continue
			}
			if g.lreach[edges[ii].a] == false {
				continue
			}
			if g.rreach[edges[ii].a] == false {
				continue
			}
			if cc[edges[ii].b] > edges[ii].cost+cc[edges[ii].a] {
				cc[edges[ii].b] = edges[ii].cost + cc[edges[ii].a]
				if i == n-1 {
					fmt.Fprintln(writer, "-1")
					writer.Flush()
					return
				}
			}
		}
	}
	ans := -cc[n-1]
	if ans < 0 {
		ans = 0
	}
	fmt.Fprintln(writer, ans)
	writer.Flush()
}

type Graph struct {
	lto, rto       [][]int
	lreach, rreach []bool
}

func (g *Graph) dfs(s int, r bool) {
	to := g.lto
	reach := g.lreach
	if r == true {
		to = g.rto
		reach = g.rreach
	}
	if reach[s] == true {
		return
	}
	reach[s] = true
	for _, t := range to[s] {
		g.dfs(t, r)
	}
}

type Edge struct {
	a, b int
	cost int64
}


Compiler output:
a.cc:1:1: error: 'package' does not name a type
    1 | package main
      | ^~~~~~~
a.cc:18:1: error: 'func' does not name a type
   18 | func getNextString(scanner *bufio.Scanner) string {
      | ^~~~
a.cc:23:1: error: 'func' does not name a type
   23 | func getNextInt(scanner *bufio.Scanner) int {
      | ^~~~
a.cc:28:1: error: 'func' does not name a type
   28 | func getNextInt64(scanner *bufio.Scanner) int64 {
      | ^~~~
a.cc:33:1: error: 'func' does not name a type
   33 | func main() {
      | ^~~~
a.cc:111:1: error: 'type' does not name a type; did you mean 'typeof'?
  111 | type Graph struct {
      | ^~~~
      | typeof
a.cc:116:6: error: expected constructor, destructor, or type conversion before '(' token
  116 | func (g *Graph) dfs(s int, r bool) {
      |      ^
a.cc:132:1: error: 'type' does not name a type; did you mean 'typeof'?
  132 | type Edge struct {
      | ^~~~
      | typeof