QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#669958 | #9315. Rainbow Bracket Sequence | futarian | TL | 0ms | 0kb | C++14 | 3.6kb | 2024-10-23 20:07:05 | 2024-10-23 20:07:05 |
answer
#include "bits/stdc++.h"
using namespace std;
#define int ll
#define ll long long
const int Len = 515 , inf = 1e9;
const ll Inf = 1e13;
ll dis[Len],fl,co;
int c[Len << 1],v[Len << 1],l[Len],n,m,head[Len],cur[Len],cnt = 1,vis[Len],flag[Len],S,T,SS,TT;
int val[Len],M;
struct node
{
int next,to,w,ww;
node(){next = to = w = ww = 0;}
node(int NEXT,int TO,int W,int WW){next = NEXT , to = TO , w = W , ww = WW;}
}edge[Len << 1];
inline void add(int from,int to,int w,int ww)
{
edge[++ cnt].to = to;
edge[cnt].next = head[from];
edge[cnt].w = w;
edge[cnt].ww = ww;
head[from] = cnt;
}
inline void adeg(int from,int to,int w,int ww)
{
add(from , to , w , ww);
add(to , from , 0 , -ww);
}
queue<int> Q;
inline int Dij()
{
while(!Q.empty()) Q.pop();
for(int i = 1 ; i <= TT ; i ++) dis[i] = Inf , cur[i] = vis[i] = flag[i] = 0;
dis[SS] = 0 , cur[SS] = head[SS] , vis[SS] = 1 , Q.push(SS);
while(!Q.empty())
{
int p = Q.front();Q.pop();
vis[p] = 0;
for(int e = head[p] ; e ; e = edge[e].next)
{
int to = edge[e].to;
if(edge[e].w && dis[to] > dis[p] + edge[e].ww)
{
dis[to] = dis[p] + edge[e].ww;
cur[to] = head[to];
if(!vis[to]) vis[to] = 1 , Q.push(to);
}
}
}
if(dis[TT] == Inf) return 0;
return 1;
}
int dfs(int u,int In)
{
//printf("woeee%lld\n",In);
if(u == TT) return In;
flag[u] = 1;
int Out = 0;
for(int e = cur[u] ; e && In > 0 ; e = edge[e].next)
{
cur[u] = e;
int to = edge[e].to;
if(edge[e].w && dis[to] == dis[u] + edge[e].ww && !flag[to])
{
int res = dfs(to , min(In , edge[e].w));
In -= res;
Out += res;
edge[e].w -= res;
edge[e ^ 1].w += res;
co += res * edge[e].ww;
}
}
flag[u] = 0;
if(!Out) return dis[u] = 0;
return Out;
}
struct addedg
{
int u,v,l,r,ww;
addedg(){u = v = l = r = ww = 0;}
addedg(int U,int V,int L,int R,int WW){u = U , v = V , l = L , r = R , ww = WW;}
}edd[Len << 2];
inline void I(int u,int v,int l,int r,int ww){edd[++ M] = addedg(u , v , l , r , ww);}
inline void BuildGraph()
{
for(int i = 1 ; i <= M ; i ++)
{
int u,v,L,R,ww;u = edd[i].u , v = edd[i].v , L = edd[i].l , R = edd[i].r , ww = edd[i].ww;
edge[cnt + 1].ww = L;
val[v] += L , val[u] -= L;
adeg(v , u , R - L , ww);
}
SS = T + 1 , TT = SS + 1;int sm = 0;
for(int i = 1 ; i <= T ; i ++)
{
if(val[i] < 0) adeg(SS , i , -val[i] , 0);
else adeg(i , TT , val[i] , 0) , sm += val[i];
}
adeg(S , T , Inf , 0);
//puts("doijocdjossjio");
int as = 0;
while(Dij())
{
as += dfs(SS , inf);
//printf("%lld\n",as);
}
if(as != sm)
{
puts("-1");
return;
}
as = edge[cnt].w;
edge[cnt].w = edge[cnt ^ 1].w = 0;
SS = T , TT = S;
while(Dij()) as += dfs(SS , Inf);
printf("%lld\n",-co);
}
signed main()
{
int ttt;scanf("%lld",&ttt);
while(ttt --)
{
scanf("%lld %lld",&n,&m);
for(int i = 1 ; i <= m ; i ++) scanf("%lld",&l[i]);
for(int i = 1 ; i <= (n << 1) ; i ++) scanf("%lld",&c[i]);
for(int i = 1 ; i <= (n << 1) ; i ++) scanf("%lld",&v[i]);
S = 2 * n + m + 2 , T = 2 * n + m + 3 , SS = T + 1 , TT = SS + 1;
for(int i = 1 ; i <= m ; i ++) I(S , i , l[i] , inf , 0);
for(int i = 1 ; i <= (n << 1) ; i ++) I(c[i] , m + i , 0 , 1 , -v[i]);
for(int i = 1 ; i <= (n << 1) ; i ++) I(m + i , m + i + 1 , (i >> 1) + (i & 1) , inf , 0);
I(S - 1 , T , n , n , 0);
//puts("doijocdjossjio");
BuildGraph();
for(int i = 0 ; i <= cnt ; i ++) edge[i] = node(0 , 0 , 0 , 0);
fl = co = M = 0 , cnt = 1;for(int i = 1 ; i <= T ; i ++) head[i] = val[i] = 0;
}
return 0;
}
详细
Test #1:
score: 0
Time Limit Exceeded
input:
2 3 2 1 2 1 2 2 2 1 2 3 1 4 2 2 1 3 2 2 2 1 2 2 2 1 2 3 1 4 2 2 1