Codeforces 844F Anti-Palindromize 最小费用流
想到网络流就差不多了, 拆拆点, 建建边。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int edgenum, S, T;
int head[N], dist[N], pre[N];
bool vis[N]; struct Edge {
int from, to, cap, flow, cost, next;
} edge[]; void MCMFinit() {
edgenum = ;
memset(head, -, sizeof(head));
} void addEdge(int u, int v, int w, int c) {
Edge E1 = {u, v, w, , c, head[u]};
edge[edgenum] = E1;
head[u] = edgenum++;
Edge E2 = {v, u, , , -c, head[v]};
edge[edgenum] = E2;
head[v] = edgenum++;
} bool SPFA(int s, int t) {
queue<int> Q;
memset(dist, INF, sizeof(dist));
memset(vis, false, sizeof(vis));
memset(pre, -, sizeof(pre));
dist[s] = ; vis[s] = true; Q.push(s);
while(!Q.empty()) {
int u = Q.front(); Q.pop(); vis[u] = false;
for(int i = head[u]; i != -; i = edge[i].next) {
Edge E = edge[i];
if(dist[E.to] > dist[u] + E.cost && E.cap > E.flow) {
dist[E.to] = dist[u] + E.cost;
pre[E.to] = i;
if(!vis[E.to]) {
vis[E.to] = true;
Q.push(E.to);
}
}
}
}
return pre[t] != -;
} void MCMF(int s, int t, int &cost, int &flow) {
flow = ; cost = ;
while(SPFA(s, t)) {
int Min = INF;
for(int i = pre[t]; i != -; i = pre[edge[i^].to]) {
Edge E = edge[i];
Min = min(Min, E.cap - E.flow);
}
for(int i = pre[t]; i != -; i = pre[edge[i^].to]) {
edge[i].flow += Min;
edge[i^].flow -= Min;
cost += edge[i].cost * Min;
}
flow += Min;
}
} int n;
char s[N];
int a[N], b[N];
int cnt[]; int main() {
MCMFinit();
scanf("%d", &n);
S = , T = (n / + ) * + n + ;
int B = (n / + );
int G = (n / + ) * ;
scanf("%s", s + );
for(int i = ; i <= n; i++) scanf("%d", &b[i]);
for(int i = ; i <= n; i++) {
a[i] = s[i] - 'a' + ;
cnt[a[i]]++;
}
for(int i = , o = ; i <= G; i += B, o++) {
addEdge(S, i, cnt[o], );
for(int j = ; j < B; j++) {
addEdge(i, i + j, , );
int c1 = a[j] == o ? -b[j] : ;
int c2 = a[n - j + ] == o ? -b[n - j + ] : ;
addEdge(i + j, G + j, , c1);
addEdge(i + j, G + n - j + , , c2);
}
}
for(int i = ; i <= n; i++) addEdge(G + i, T, , );
int cost = , flow = ;
MCMF(S, T, cost, flow);
printf("%d\n", -cost);
return ;
} /*
*/
Codeforces 844F Anti-Palindromize 最小费用流的更多相关文章
- Codeforces 730I:Olympiad in Programming and Sports(最小费用流)
http://codeforces.com/problemset/problem/730/I 题意:有n个人参加两种比赛,其中每个人有两个参加比赛的属性,如果参加了其中的一个比赛,那么不能参加另一个比 ...
- codeforces:818G Four Melodies分析
题目 题目大意是有一组自然数v1,...,vn,要求在其中找到四个非空子序列(从原来的自然数序列中挑选一部分数,并按原先后关系排序),这些子序列互不相交,且每个子序列中的前后元素的值要么差值的绝对值为 ...
- codeforces的dp专题
1.(467C)http://codeforces.com/problemset/problem/467/C 题意:有一个长为n的序列,选取k个长度为m的子序列(子序列中不能有位置重复),求所取的k个 ...
- [codeforces/edu31]总结
链接:http://codeforces.com/contest/884 A题: 求86400-ai的前缀和,找到第一个大于等于给定t的即可. B题: 任意相邻两段之间必然有一个0,然后算一下至少的长 ...
- ABP Zero示例项目登录报错“Empty or invalid anti forgery header token.”问题解决
ABP Zero项目,登录时出现如图"Empty or invalid anti forgery header token."错误提示的解决方法: 在 WebModule.cs的P ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
随机推荐
- springcloud-4:服务注册(hello-service)
服务端 请见 http://www.cnblogs.com/huiy/p/8668005.html 客户端: 主启动类 import org.springframework.boot.SpringAp ...
- Ex 2_14 去掉数组中所有重复的元素..._第二次作业
首先利用归并排序算法对数组进行排序,时间复杂度为O(nlogn),接着再利用时间复杂度为O(n) 的去重复算法去掉数组中的重复元素.总的时间复杂度为O(nlogn). (这题应该用分支算法解决)以下为 ...
- Ex 2_27 矩阵A的平方是A自乘后的乘积,即AA..._第三次作业
- iOS 高德地图轨迹回放的 思路, 及方法
// 开始,公司要求制作一段跑步轨迹 在地图上的 动画回放, 传入一段经纬度, 开始一想,这不是很简单吗, 高德地图有可以把经纬度转换成坐标点的方法 /** * @brief 将经纬度转换为指定vie ...
- 使用XIB 或者storyboard 创建imageView 模式 UIViewContentModeScaleAspectFill 图片越界问题
ImageView UIViewContentModeScaleAspectFill 超出边界的问题 代码如下 [_photoView setClipsToBounds:Yes]; sto ...
- Confluence 6 配置附件大小
你可以限制上传到 Confluence 的附件的大小. 配置可以上传到 Confluence 的附件所允许的大小: 进入 > 基本配置(General Configuration). 选择 编 ...
- Confluence 6 中进行用户管理的优化配置和限制的基本建议
避免跨目录的多个用户名:如果你连接了超过一个的目录服务器,我们建议你需要确定你的用户名在目录服务器中是唯一的.例如:我们不建议你定义一个用户同时在'Directory1' 和 'Directory2' ...
- ionic3 调用摄像头 当键盘弹出时候 出现摄像头 背景
iOS 端毫无 bug,Android 端却出现了问题.当软键盘弹出后,Android 端的 tabs 移到了软键盘的上面,再仔细一看,整个界面都被压扁了,输入框也不知道去哪儿了. 于是去翻 Ioni ...
- LeetCode(75):分类颜色
Medium! 题目描述: 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 ...
- hdu1198 普通的并查集
今天开始(第三轮)并查集,,之前学的忘了一些 本题很简单直接上代码 #include<iostream> #include<cstring> #include<cstdi ...