結果
| 問題 |
No.1911 Alternately Coloring
|
| コンテスト | |
| ユーザー |
ぷら
|
| 提出日時 | 2022-04-22 22:50:24 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 561 ms / 4,000 ms |
| コード長 | 2,786 bytes |
| コンパイル時間 | 2,408 ms |
| コンパイル使用メモリ | 209,068 KB |
| 最終ジャッジ日時 | 2025-01-28 20:37:35 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 29 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
long long inf = 1001001001001001001;
long long dist[1001][2][2];
using P = array<long long,4>;
bool chmin(long long &x,long long y) {
if(x > y) {
x = y;
return true;
}
return false;
}
int main() {
int N,M;
cin >> N >> M;
vector<vector<int>>road(N);
for(int i = 0; i < M; i++) {
int u,v;
cin >> u >> v;
u--;
v--;
road[u].push_back(v);
road[v].push_back(u);
}
vector<int>R(N),B(N);
for(int i = 0; i < N; i++) {
cin >> R[i];
}
for(int i = 0; i < N; i++) {
cin >> B[i];
}
vector<int>dist2(N,-1);
dist2[0] = 0;
queue<int>que;
que.push(0);
while (!que.empty()) {
int x = que.front();
que.pop();
for(int i:road[x]) {
if(dist2[i] == -1) {
dist2[i] = dist2[x]^1;
que.push(i);
}
}
}
long long a = 0,b = 0;
for(int i = 0; i < N; i++) {
if(dist2[i]) {
a += R[i];
b += B[i];
}
else {
a += B[i];
b += R[i];
}
}
long long ans = max(a,b),sum = 0;
for(int i = 0; i < N; i++) {
sum += max(R[i],B[i]);
}
for(int i = 0; i < N; i++) {
priority_queue<P,vector<P>,greater<P>>que2;
for(int l = 0; l < N; l++) {
for(int j = 0; j < 2; j++) {
for(int k = 0; k < 2; k++) {
dist[l][j][k] = inf;
}
}
}
dist[i][0][0] = 0;
que2.push({0,i,0,0});
while (!que2.empty()) {
P x = que2.top();
que2.pop();
if(dist[x[1]][x[2]][x[3]] != x[0]) {
continue;
}
for(int k:road[x[1]]) {
int c = 0;
if((x[2]^x[3]) == 1) {
c = max(R[k],B[k])-R[k];
}
if((x[2]^x[3]) == 0) {
c = max(R[k],B[k])-B[k];
}
if(chmin(dist[k][x[2]^1][x[3]],x[0]+c)) {
que2.push({x[0]+c,k,x[2]^1,x[3]});
}
if(x[3] == 0) {
int c = 0;
if(x[2] == 1) {
c = max(R[k],B[k])-B[k];
}
if(x[2] == 0) {
c = max(R[k],B[k])-R[k];
}
if(chmin(dist[k][x[2]^1][1],x[0]+c)) {
que2.push({x[0]+c,k,x[2]^1,1});
}
}
}
}
ans = max(ans,sum-dist[i][1][0]);
ans = max(ans,sum-dist[i][1][1]);
}
cout << ans << endl;
}
ぷら