Skip to content

Instantly share code, notes, and snippets.

@yurahuna
Created April 5, 2017 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yurahuna/1469a5ebe80442b3cedc38d16695a6ba to your computer and use it in GitHub Desktop.
Save yurahuna/1469a5ebe80442b3cedc38d16695a6ba to your computer and use it in GitHub Desktop.
PyCPXで最短路問題を解く
# -*- coding: utf-8 -*-
import numpy as np
from pycpx import CPlexModel
# 入力(有向グラフ,頂点は0-index)
# n m
# s_1 t_1 c_1
# s_2 t_2 c_2
# ...
# s_m t_m c_m
n, m = map(int, raw_input().split())
model = CPlexModel()
d = model.new(n)
model.constrain(d[0] == 0)
for i in range(m):
s, t, c = map(int, raw_input().split())
model.constrain(d[t] <= d[s] + c)
model.maximize(d[n-1])
print model[d]
# 4 5
# 0 1 1
# 0 2 4
# 1 2 2
# 2 3 1
# 1 3 5
# -> [ 0. 1. 3. 4.]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment