Codeforces 884f F. Anti-Palindromize

题 

  OvO http://codeforces.com/contest/884/problem/F

  (Educational Codeforces Round 31 - F)

  884f

 

  题目标签上的 flows 极大降低了难度……

  做法:

  首先贪心,每个对应的元素对,固定b值比较大的那个元素设为不交换的元素。

  然后费用流,2n+2个点,设源点为0,汇点为2n+1

  源点向 1 ~ n 的点连一条费用为0,流量为1的边

  从 n+1 ~ 2n 向汇点连一条费用为0,流量为1的边

  对于每个固定元素,如果他是字符串中第i个元素,则i点向n+i点连一条费用0,流量1的边

  对于每个非固定元素对(i,j)(i可以等于j),如果i可以放到到j的位置,则i向j连一条流量为1的边,

  如果i=j,则费用为0;否则费用为b[j]

 

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <string>
#include <queue>

using namespace std;

const int MAXN = 300;
const int MAXM = 300000;
const int INF = 0x3f3f3f3f;

struct Edge
{
    int to,next,cap,flow,cost;
}edge[MAXM];

int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从0~N-1

void init(int n)
{
    N = n;
    tol = 0;
    memset(head,-1,sizeof(head));
}

void addedge(int u,int v,int cap,int cost)
{
    edge[tol].to = v;
    edge[tol].cap = cap;
    edge[tol].cost = cost;
    edge[tol].flow = 0;
    edge[tol].next = head[u];
    head[u] = tol++;
    edge[tol].to = u;
    edge[tol].cap = 0;
    edge[tol].cost = -cost;
    edge[tol].flow = 0;
    edge[tol].next = head[v];
    head[v] = tol++;
}

bool spfa(int s,int t)
{
    queue<int>q;
    for(int i = 0;i < N;i++)
    {
        dis[i] = INF;
        vis[i] = false;
        pre[i] = -1;
    }
    dis[s] = 0;
    vis[s] = true;
    q.push(s);
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(int i = head[u]; i != -1;i = edge[i].next)
        {
            int v = edge[i].to;
            if(edge[i].cap > edge[i].flow &&
               dis[v] > dis[u] + edge[i].cost )
            {
                dis[v] = dis[u] + edge[i].cost;
                pre[v] = i;
                if(!vis[v])
                {
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    }
    if(pre[t] == -1)return false;
    else return true;
}

int minCostMaxflow(int s,int t,int &cost)
{
    int flow = 0;
    cost = 0;
    while(spfa(s,t))
    {
        int Min = INF;
        for(int i = pre[t];i != -1;i = pre[edge[i^1].to])
        {
            if(Min > edge[i].cap - edge[i].flow)
                Min = edge[i].cap - edge[i].flow;
        }
        for(int i = pre[t];i != -1;i = pre[edge[i^1].to])
        {
            edge[i].flow += Min;
            edge[i^1].flow -= Min;
            cost += edge[i].cost * Min;
        }
        flow += Min;
    }
    return flow;
}	

int n,val[MAXN],fix[MAXN],sum;
char str[MAXN];
int ans;

int main()
{
	int i,j,s,t,tmp;
	scanf("%d",&n);
	init(2*n+2);
	s=0; t=2*n+1;
	scanf("%s",str+1);
	sum=0;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&val[i]);
		sum+=val[i];
	}
	memset(fix,0,sizeof(fix));
	for(i=1;i<=n/2;i++)
		if(val[i]>val[n+1-i])
			fix[i]=1,addedge(i,n+i,1,0);
		else
			fix[n+1-i]=1,addedge((n+1-i),n+(n+1-i),1,0);
	for(i=1;i<=n;i++)
		addedge(s,i,1,0),addedge(n+i,t,1,0);
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
		{
			if(fix[i] || fix[j])
				continue;
			if(str[i]==str[n+1-j])
				continue;
			if(i==j) tmp=0;
			else tmp=val[j];
			addedge(i,n+j,1,tmp);
		}
	int flow=minCostMaxflow(s,t,ans);
	ans=sum-ans;
	printf("%d\n",ans);
	return 0;
}

  

 

posted @ 2017-11-09 17:51  太阳星人FxxL  阅读(501)  评论(0编辑  收藏  举报