結果

問題 No.1911 Alternately Coloring
ユーザー chineristACchineristAC
提出日時 2022-01-17 02:19:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,223 ms / 4,000 ms
コード長 2,472 bytes
コンパイル時間 249 ms
コンパイル使用メモリ 86,784 KB
実行使用メモリ 89,296 KB
最終ジャッジ日時 2023-08-15 07:44:14
合計ジャッジ時間 40,077 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
74,136 KB
testcase_01 AC 112 ms
78,992 KB
testcase_02 AC 103 ms
74,124 KB
testcase_03 AC 134 ms
81,452 KB
testcase_04 AC 130 ms
81,744 KB
testcase_05 AC 113 ms
79,020 KB
testcase_06 AC 2,131 ms
88,908 KB
testcase_07 AC 1,523 ms
86,416 KB
testcase_08 AC 1,575 ms
85,876 KB
testcase_09 AC 1,935 ms
87,304 KB
testcase_10 AC 1,333 ms
85,936 KB
testcase_11 AC 1,998 ms
88,344 KB
testcase_12 AC 2,004 ms
87,876 KB
testcase_13 AC 1,929 ms
87,140 KB
testcase_14 AC 1,443 ms
85,828 KB
testcase_15 AC 1,851 ms
87,100 KB
testcase_16 AC 1,787 ms
86,588 KB
testcase_17 AC 1,735 ms
86,144 KB
testcase_18 AC 1,611 ms
86,052 KB
testcase_19 AC 1,710 ms
86,664 KB
testcase_20 AC 1,955 ms
87,784 KB
testcase_21 AC 1,565 ms
85,992 KB
testcase_22 AC 1,846 ms
87,044 KB
testcase_23 AC 2,223 ms
89,296 KB
testcase_24 AC 126 ms
81,520 KB
testcase_25 AC 128 ms
81,784 KB
testcase_26 AC 130 ms
82,088 KB
testcase_27 AC 1,890 ms
87,248 KB
testcase_28 AC 924 ms
83,984 KB
testcase_29 AC 1,891 ms
88,280 KB
testcase_30 AC 100 ms
74,124 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Dijkstra():
    class Edge():
        def __init__(self, _to, _cost):
            self.to = _to
            self.cost = _cost

    def __init__(self, V):
        self.G = [[] for i in range(V)]
        self._E = 0
        self._V = V

    @property
    def E(self):
        return self._E

    @property
    def V(self):
        return self._V

    def add_edge(self, _from, _to, _cost):
        self.G[_from].append(self.Edge(_to, _cost))
        self._E += 1

    def shortest_path(self, s):
        import heapq
        que = []
        d = [10**15] * self.V
        d[s] = 0
        heapq.heappush(que, (0, s))

        while len(que) != 0:
            cost, v = heapq.heappop(que)
            if d[v] < cost: continue

            for i in range(len(self.G[v])):
                e = self.G[v][i]
                if d[e.to] > d[v] + e.cost:
                    d[e.to] = d[v] + e.cost
                    heapq.heappush(que, (d[e.to], e.to))
        return d

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import log,gcd

input = lambda :sys.stdin.readline()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

def solve(N,M,E,R,B):
    G = Dijkstra(2*N)
    edge = [[] for i in range(N)]
    for u,v in E:    
        u,v = u-1,v-1

        G.add_edge(2*u,2*v+1,max(R[v]-B[v],0))
        G.add_edge(2*u+1,2*v,max(B[v]-R[v],0))
        G.add_edge(2*v,2*u+1,max(R[u]-B[u],0))
        G.add_edge(2*v+1,2*u,max(B[u]-R[u],0))

        edge[u].append(v)
        edge[v].append(u)
    
    color = [-1] * N
    color[0] = 0
    stack = [0]
    flag = True
    while stack:
        v = stack.pop()
        for nv in edge[v]:
            if color[nv]==-1:
                color[nv] = 1 - color[v]
                stack.append(nv)
            else:
                if color[nv] != 1 - color[v]:
                    flag = False
    
    if flag:
        x,y = 0,0
        for i in range(N):
            if color[i]:
                x += R[i]; y += B[i]
            else:
                y += R[i]; x += B[i]
        return max(x,y)
    
    res = sum(max(R[i],B[i]) for i in range(N))
    minus = res

    for i in range(N):
        t0 = G.shortest_path(2*i)[2*i+1]
        t1 = G.shortest_path(2*i+1)[2*i]
        minus = min(minus,t0,t1)

    return res-minus

N,M = mi()
E = [tuple(mi()) for i in range(M)]
R = li()
B = li()
print(solve(N,M,E,R,B))
0