CF 818 G Four Melodies 题解
作者:互联网
CF 818 G Four Melodies 题解
根据题目要求连边,跑最小费用流。
一个优化技巧:由于spfa会退化成\(O(n\times m)\),所以需要利用dijkstra+势函数来优化。第一次最短路可以dp出来,由于是DAG。
/*
{
######################
# Author #
# Gary #
# 2020 #
######################
*/
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
using namespace std;
//inline int read(){
// int x=0;
// char ch=getchar();
// while(ch<'0'||ch>'9'){
// ch=getchar();
// }
// while(ch>='0'&&ch<='9'){
// x=(x<<1)+(x<<3)+(ch^48);
// ch=getchar();
// }
// return x;
//}
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int a[4004];
int n;
const int GRAPH_SIZE= 6000+20;
int s=0,t=GRAPH_SIZE-1;
struct EDGE{
int u,v,c,cos;
};
vector<EDGE> e;
vector<int> each[GRAPH_SIZE];
int maxflow,mincost;
int flow[GRAPH_SIZE];
int dis[GRAPH_SIZE],inq[GRAPH_SIZE],las[GRAPH_SIZE];
bool spfa(){
memset(inq,0,sizeof(inq));
memset(dis,63,sizeof(dis));
flow[s]=INF;
dis[s]=0;
queue<int> q;
q.push(s);
inq[s]=1;
while(!q.empty()){
int now=q.front();
q.pop();
inq[now]=0;
for(auto it:each[now]){
int to,f,c;
to=e[it].v;
f=e[it].c;
c=e[it].cos;
if(f<=0) continue;
if(dis[to]>dis[now]+c){
dis[to]=dis[now]+c;
las[to]=it;
flow[to]=min(flow[now],f);
if(!inq[to]) q.push(to);
inq[to]=1;
}
}
}
return dis[t]!=INF;
}
void KM(){
while(spfa()){
maxflow+=flow[t];
mincost+=dis[t]*flow[t];
int now=t;
while(now!=s){
e[las[now]].c-=flow[t];
e[las[now]^1].c+=flow[t];
now=e[las[now]].u;
}
}
}
void make_edge(int U,int V,int C,int COS){
EDGE tmp;
tmp.u=U;
tmp.v=V;
tmp.c=C;
tmp.cos=COS;
e.PB(tmp);
each[U].PB(e.size()-1);
swap(tmp.u,tmp.v);
tmp.c=0;
tmp.cos=-COS;
e.PB(tmp);
each[V].PB(e.size()-1);
}
int lasval[100000+20];
int main(){
scanf("%d",&n);
rb(i,1,n) scanf("%d",&a[i]);
rb(i,1,n) make_edge(2*i-1,2*i,1,-1);
int las[7]={0};
rb(i,1,n){
if(las[a[i]%7]){
make_edge(2*las[a[i]%7]-1,2*i-1,INF,0);
make_edge(2*las[a[i]%7],2*i-1,INF,0);
}
if(lasval[a[i]]){
make_edge(2*lasval[a[i]]-1,2*i-1,INF,0);
}
if(lasval[a[i]-1]){
make_edge(2*lasval[a[i]-1],2*i-1,INF,0);
}
if(lasval[a[i]+1]){
make_edge(2*lasval[a[i]+1],2*i-1,INF,0);
}
las[a[i]%7]=lasval[a[i]]=i;
}
int supers=2*n+1;
rb(i,1,n) make_edge(supers,2*i-1,1,0),make_edge(2*i,t,1,0);
make_edge(s,supers,4,0);
KM();
printf("%d\n",-mincost);
return 0;
}
标签:tmp,Four,int,题解,make,818,edge,now,las 来源: https://www.cnblogs.com/gary-2005/p/14148208.html