結果

問題 No.2573 moving up
ユーザー PNJPNJ
提出日時 2023-12-03 13:42:36
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 4,417 bytes
コンパイル時間 299 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 198,788 KB
最終ジャッジ日時 2023-12-03 13:43:08
合計ジャッジ時間 29,820 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,403 ms
114,016 KB
testcase_01 TLE -
testcase_02 AC 1,988 ms
123,260 KB
testcase_03 TLE -
testcase_04 AC 1,956 ms
125,052 KB
testcase_05 AC 1,972 ms
125,180 KB
testcase_06 TLE -
testcase_07 AC 204 ms
80,944 KB
testcase_08 AC 1,103 ms
109,196 KB
testcase_09 AC 1,627 ms
115,332 KB
testcase_10 AC 305 ms
84,624 KB
testcase_11 AC 907 ms
99,564 KB
testcase_12 AC 1,865 ms
125,828 KB
testcase_13 AC 158 ms
78,912 KB
testcase_14 AC 87 ms
76,744 KB
testcase_15 AC 159 ms
79,444 KB
testcase_16 AC 789 ms
99,052 KB
testcase_17 AC 515 ms
91,172 KB
testcase_18 AC 1,458 ms
111,584 KB
testcase_19 AC 199 ms
81,084 KB
testcase_20 AC 195 ms
81,204 KB
testcase_21 AC 479 ms
90,276 KB
testcase_22 AC 221 ms
81,588 KB
testcase_23 AC 479 ms
88,720 KB
testcase_24 AC 897 ms
102,076 KB
testcase_25 AC 218 ms
81,632 KB
testcase_26 AC 49 ms
61,572 KB
testcase_27 AC 50 ms
61,696 KB
testcase_28 AC 54 ms
64,352 KB
testcase_29 AC 48 ms
61,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def Map():
  return list(map(int,input().split()))
import heapq
class mcf_graph():
    n=1
    pos=[]
    g=[[]]
    def __init__(self,N):
        self.n=N
        self.pos=[]
        self.g=[[] for i in range(N)]
    def add_edge(self,From,To,cap,cost):
        assert 0<=From and From<self.n
        assert 0<=To and To<self.n
        m=len(self.pos)
        self.pos.append((From,len(self.g[From])))
        self.g[From].append({"to":To,"rev":len(self.g[To]),"cap":cap,"cost":cost})
        self.g[To].append({"to":From,"rev":len(self.g[From])-1,"cap":0,"cost":-cost})
    def get_edge(self,i):
        m=len(self.pos)
        assert 0<=i and i<m
        _e=self.g[self.pos[i][0]][self.pos[i][1]]
        _re=self.g[_e["to"]][_e["rev"]]
        return {"from":self.pos[i][0],"to":_e["to"],"cap":_e["cap"]+_re["cap"],
        "flow":_re["cap"],"cost":_e["cost"]}
    def edges(self):
        m=len(self.pos)
        result=[{} for i in range(m)]
        for i in range(m):
            tmp=self.get_edge(i)
            result[i]["from"]=tmp["from"]
            result[i]["to"]=tmp["to"]
            result[i]["cap"]=tmp["cap"]
            result[i]["flow"]=tmp["flow"]
            result[i]["cost"]=tmp["cost"]
        return result
    def flow(self,s,t,flow_limit=-1-(-1<<63)):
        return self.slope(s,t,flow_limit)[-1]
    def slope(self,s,t,flow_limit=-1-(-1<<63)):
        assert 0<=s and s<self.n
        assert 0<=t and t<self.n
        assert s!=t
        '''
         variants (C = maxcost):
         -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0
         reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge
        '''
        dual=[0 for i in range(self.n)]
        dist=[0 for i in range(self.n)]
        pv=[0 for i in range(self.n)]
        pe=[0 for i in range(self.n)]
        vis=[False for i in range(self.n)]
        def dual_ref():
            for i in range(self.n):
                dist[i]=-1-(-1<<63)
                pv[i]=-1
                pe[i]=-1
                vis[i]=False
            que=[]
            heapq.heappush(que,(0,s))
            dist[s]=0
            while(que):
                v=heapq.heappop(que)[1]
                if vis[v]:continue
                vis[v]=True
                if v==t:break
                '''
                 dist[v] = shortest(s, v) + dual[s] - dual[v]
                 dist[v] >= 0 (all reduced cost are positive)
                 dist[v] <= (n-1)C
                '''
                for i in range(len(self.g[v])):
                    e=self.g[v][i]
                    if vis[e["to"]] or (not(e["cap"])):continue
                    '''
                     |-dual[e.to]+dual[v]| <= (n-1)C
                     cost <= C - -(n-1)C + 0 = nC
                    '''
                    cost=e["cost"]-dual[e["to"]]+dual[v]
                    if dist[e["to"]]-dist[v]>cost:
                        dist[e["to"]]=dist[v]+cost
                        pv[e["to"]]=v
                        pe[e["to"]]=i
                        heapq.heappush(que,(dist[e["to"]],e["to"]))
            if not(vis[t]):
                return False
            for v in range(self.n):
                if not(vis[v]):continue
                dual[v]-=dist[t]-dist[v]
            return True
        flow=0
        cost=0
        prev_cost=-1
        result=[(flow,cost)]
        while(flow<flow_limit):
            if not(dual_ref()):
                break
            c=flow_limit-flow
            v=t
            while(v!=s):
                c=min(c,self.g[pv[v]][pe[v]]["cap"])
                v=pv[v]
            v=t
            while(v!=s):
                self.g[pv[v]][pe[v]]["cap"]-=c
                self.g[v][self.g[pv[v]][pe[v]]["rev"]]["cap"]+=c
                v=pv[v]
            d=-dual[s]
            flow+=c
            cost+=c*d
            if(prev_cost==d):
                result.pop()
            result.append((flow,cost))
            prev_cost=cost
        return result

H,W = Map()
X = [0 for i in range(W)]
Y = [0 for i in range(W)]
for i in range(W):
  X[i],Y[i] = Map()

E = []
for u in range(1,W+1):
  h,w = 1,u
  for v in range(1,W+1):
    x,y = X[v-1],Y[v-1]
    if w < y:
      d = max(x-h,y-w)
    else:
      d = (x - h) + (w - y)
    E.append((d,u,v+W))

g = mcf_graph(2*W+2)
for u in range(1,W+1):
  g.add_edge(0,u,1,0)
  g.add_edge(u+W,2*W+1,1,0)

for d,u,v in E:
  g.add_edge(u,v,1,d)

print(g.flow(0,2*W+1)[1])
0