結果

問題 No.1615 Double Down
ユーザー iaNTUiaNTU
提出日時 2021-06-22 03:11:34
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 6,228 bytes
コンパイル時間 511 ms
コンパイル使用メモリ 82,280 KB
実行使用メモリ 409,344 KB
最終ジャッジ日時 2024-07-17 15:33:07
合計ジャッジ時間 50,236 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,904 ms
284,708 KB
testcase_01 AC 2,108 ms
288,708 KB
testcase_02 AC 2,730 ms
299,176 KB
testcase_03 AC 2,702 ms
295,512 KB
testcase_04 AC 2,960 ms
292,012 KB
testcase_05 AC 3,064 ms
303,252 KB
testcase_06 AC 3,849 ms
315,328 KB
testcase_07 AC 3,751 ms
313,396 KB
testcase_08 AC 3,921 ms
313,812 KB
testcase_09 AC 8,946 ms
334,204 KB
testcase_10 AC 8,013 ms
328,404 KB
testcase_11 AC 7,518 ms
341,980 KB
testcase_12 AC 5,054 ms
354,276 KB
testcase_13 AC 4,866 ms
348,088 KB
testcase_14 AC 5,090 ms
360,064 KB
testcase_15 AC 4,987 ms
405,916 KB
testcase_16 AC 4,973 ms
405,660 KB
testcase_17 AC 4,809 ms
409,344 KB
testcase_18 AC 8,991 ms
376,968 KB
testcase_19 TLE -
testcase_20 AC 9,943 ms
367,948 KB
testcase_21 AC 9,405 ms
342,220 KB
testcase_22 TLE -
testcase_23 TLE -
testcase_24 AC 9,770 ms
349,144 KB
testcase_25 AC 9,135 ms
347,116 KB
testcase_26 AC 9,266 ms
315,692 KB
testcase_27 AC 591 ms
99,624 KB
testcase_28 AC 584 ms
98,756 KB
testcase_29 AC 755 ms
126,020 KB
testcase_30 AC 722 ms
109,248 KB
testcase_31 AC 815 ms
106,576 KB
testcase_32 AC 808 ms
121,504 KB
testcase_33 AC 1,092 ms
155,280 KB
testcase_34 AC 1,052 ms
137,096 KB
testcase_35 AC 1,029 ms
148,088 KB
testcase_36 AC 165 ms
79,208 KB
testcase_37 AC 166 ms
79,588 KB
testcase_38 AC 191 ms
80,200 KB
testcase_39 AC 189 ms
80,180 KB
testcase_40 AC 184 ms
79,464 KB
testcase_41 AC 181 ms
79,852 KB
testcase_42 AC 205 ms
80,536 KB
testcase_43 AC 228 ms
80,376 KB
testcase_44 AC 209 ms
80,452 KB
testcase_45 AC 45 ms
55,920 KB
testcase_46 AC 46 ms
57,372 KB
testcase_47 AC 49 ms
57,928 KB
testcase_48 AC 45 ms
56,168 KB
testcase_49 AC 49 ms
56,800 KB
testcase_50 AC 49 ms
57,340 KB
testcase_51 AC 49 ms
57,456 KB
testcase_52 AC 47 ms
57,380 KB
testcase_53 AC 46 ms
56,332 KB
testcase_54 AC 46 ms
56,288 KB
testcase_55 AC 47 ms
57,968 KB
testcase_56 AC 46 ms
57,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
class mf_graph:
    n=1
    g=[[] for i in range(1)]
    pos=[]
    def __init__(self,N):
        self.n=N
        self.g=[[] for i in range(N)]
        self.pos = []
    def add_edge(self,From,To,cap):
        assert 0<=From and From<self.n
        assert 0<=To and To<self.n
        assert 0<=cap
        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})
        self.g[To].append({"to":From,"rev":len(self.g[From])-1,"cap":0})
        return m
    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"]}
    def edges(self):
        m=len(self.pos)
        result=[]
        for i in range(m):
            result.append(self.get_edge(i))
        return result
    def change_edge(self,i,new_cap,new_flow):
        m=len(self.pos)
        assert 0<=i and i<m
        assert 0<=new_flow and new_flow<=new_cap
        _e=self.g[self.pos[i][0]][self.pos[i][1]]
        _re=self.g[_e["to"]][_e["rev"]]
        _e["cap"]=new_cap-new_flow
        _re["cap"]=new_flow
    def flow(self,s,t,flow_limit=(2**31)-1):
        assert 0<=s and s<self.n
        assert 0<=t and t<self.n
        level=[0 for i in range(self.n)]
        Iter=[0 for i in range(self.n)]
        que=deque([])
        def bfs():
            for i in range(self.n):level[i]=-1
            level[s]=0
            que=deque([])
            que.append(s)
            while(len(que)>0):
                v=que.popleft()
                for e in self.g[v]:
                    if e["cap"]==0 or level[e["to"]]>=0:continue
                    level[e["to"]]=level[v]+1
                    if e["to"]==t:return
                    que.append(e["to"])
        def dfs(func,v,up):
            if (v==s):return up
            res=0
            level_v=level[v]
            for i in range(Iter[v],len(self.g[v])):
                e=self.g[v][i]
                if (level_v<=level[e["to"]] or self.g[e["to"]][e["rev"]]["cap"]==0):continue
                d=func(func,e["to"],min(up-res,self.g[e["to"]][e["rev"]]["cap"]))
                if d<=0:continue
                self.g[v][i]["cap"]+=d
                self.g[e["to"]][e["rev"]]["cap"]-=d
                res+=d
                if res==up:return res
            level[v]=self.n
            return res
        flow=0
        while(flow<flow_limit):
            bfs()
            if level[t]==-1:break
            for i in range(self.n):Iter[i]=0
            while(flow<flow_limit):
                f=dfs(dfs,t,flow_limit-flow)
                if not(f):break
                flow+=f
        return flow
    def min_cut(self,s):
        visited=[False for i in range(self.n)]
        que=deque([])
        que.append(s)
        while(len(que)>0):
            p=que.popleft()
            visited[p]=True
            for e in self.g[p]:
                if e["cap"] and not(visited[e["to"]]):
                    visited[e["to"]]=True
                    que.append(e["to"])
        return visited

n, m, k, l = map(int, input().split())
x = [0] * l; y = [0] * l; z = [0] * l
for i in range(l) :
    x[i], y[i], z[i] = map(int, input().split())

edges = [[] for i in range(k + 1)]
for i in range(l) :
    edges[z[i]].append(i)

alivex = [True] * (n + 1)
alivey = [True] * (m + 1)
wentx = [False] * (n + 1)
wenty = [False] * (m + 1)
val = [0] * (k + 1)
matchx = [0] * (n + 1)
matchy = [0] * (m + 1)

S = 0; T = n + m + 1
G = mf_graph(n + m + 2)
for j in range(1, n + 1) :
    G.add_edge(S, j, 1)
for j in range(1, m + 1) :
    G.add_edge(j + n, T, 1)

previous_edges = []
for i in range(k, -1, -1) :
    for j in edges[i] :
        if alivex[x[j]] and alivey[y[j]] : 
            previous_edges.append(j)
            G.add_edge(x[j], y[j] + n, 1)

    gx = [[] for j in range(n + 1)]
    gy = [[] for j in range(m + 1)]

    for j in previous_edges :
        gx[x[j]].append(y[j])
        gy[y[j]].append(x[j])

    val[i] = G.flow(S, T)

    for j in range(1, n + 1) :
        matchx[j] = -1
    for j in range(1, m + 1) :
        matchy[j] = -1

    for j in range(1, n + 1) :
        wentx[j] = False
    for j in range(1, m + 1) :
        wenty[j] = False

    for e in G.edges() :
        if e["flow"] == 1 :
            if e["from"] != S and e["to"] != T :
                matchx[e["from"]] = e["to"] - n
                matchy[e["to"] - n] = e["from"]

    qu = []
    for j in range(1, n + 1) :
        if matchx[j] < 0 :
            wentx[j] = True
            qu.append(j)

    while len(qu) :
        idx = qu.pop()
        for j in gx[idx] :
            if not wentx[matchy[j]] :
                wentx[matchy[j]] = True
                qu.append(matchy[j])

    for j in range(1, n + 1) :
        if not wentx[j] :
            alivex[j] = False

    for j in range(1, m + 1) :
        if matchy[j] < 0 :
            wenty[j] = True
            qu.append(j)

    while len(qu) :
        idx = qu.pop()
        for j in gy[idx] :
            if not wenty[matchx[j]] :
                wenty[matchx[j]] = True
                qu.append(matchx[j])

    for j in range(1, m + 1) :
        if not wenty[j] :
            alivey[j] = False

    gsz = [len(G.g[j]) for j in range(0, n + 1)]
    for j in range(1, n + 1) :
        if not wentx[j] :
            for idx in range(gsz[j]) :
                e = G.g[j][idx]
                if e["cap"] >= 0 and e["to"] != S and not wenty[e["to"] - n] and matchx[j] != e["to"] - n :
                    G.g[j][idx]["cap"] = 0
                    G.g[e["to"]][e["rev"]]["cap"] = 0

    psz = len(previous_edges)
    j = 0
    while j < psz :
        idx = previous_edges[j]
        if not wentx[x[idx]] and not wenty[y[idx]] and matchx[x[idx]] != y[idx] :
            psz -= 1
            previous_edges[j], previous_edges[psz] = previous_edges[psz], previous_edges[j]
            previous_edges.pop()
            j -= 1
        j += 1

ans = 0
for i in range(0, k + 1) :
    ans += val[i] << i
print(ans)
0