首页 技术 正文
技术 2022年11月10日
0 收藏 720 点赞 2,282 浏览 2044 个字

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=29821

思路:首先拆点,每个字符对应的位置拆成i和i+len,然后源点和i连边,容量为1,花费为0,i+len与汇点连边,容量为1,花费为0,然后就是对于那些在P中的字符串连边,容量为1,花费为g[u][v],最后跑最小费最大流即可,然后答案就是len-flow,cost;

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<climits>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<vector>
#include<queue>
#include<string>
#include<cmath>
#include<set>
using namespace std;
#define inf 1<<30
#define INF 1LL<<60
typedef long long ll;
typedef pair<int,int>PP;
#define MAXN 1111 struct Edge{
int v,cap,cost,next;
}edge[MAXN*MAXN]; int len,vs,vt,NE;
int head[MAXN]; void Insert(int u,int v,int cap,int cost)
{
edge[NE].v=v;
edge[NE].cap=cap;
edge[NE].cost=cost;
edge[NE].next=head[u];
head[u]=NE++; edge[NE].v=u;
edge[NE].cap=;
edge[NE].cost=-cost;
edge[NE].next=head[v];
head[v]=NE++;
} int pre[MAXN],cur[MAXN];
bool mark[MAXN];
int dist[MAXN]; bool spfa(int vs,int vt)
{
memset(mark,false,sizeof(mark));
fill(dist,dist+MAXN,inf);
dist[vs]=;
queue<int>que;
que.push(vs);
while(!que.empty()){
int u=que.front();
que.pop();
mark[u]=false;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v,cost=edge[i].cost;
if(edge[i].cap>&&dist[u]+cost<dist[v]){
dist[v]=dist[u]+cost;
pre[v]=u;
cur[v]=i;
if(!mark[v]){
mark[v]=true;
que.push(v);
}
}
}
}
return dist[vt]<inf;
} void MinCostFlow(int vs,int vt)
{
int flow=,cost=;
while(spfa(vs,vt)){
int aug=inf;
for(int u=vt;u!=vs;u=pre[u]){
aug=min(aug,edge[cur[u]].cap);
}
flow+=aug,cost+=dist[vt]*aug;
for(int u=vt;u!=vs;u=pre[u]){
edge[cur[u]].cap-=aug;
edge[cur[u]^].cap+=aug;
}
}
printf("%d %d\n",len-flow,cost);
} char str1[MAXN],str2[MAXN];
int g[MAXN][MAXN];
int main()
{
int _case;
scanf("%d",&_case);
while(_case--){
scanf("%s",str1);
len=strlen(str1);
memset(g,,sizeof(g));
for(int i=;i<len-;i++){
int u=str1[i]-'a',v=str1[i+]-'a';
if(!g[u][v]){
g[u][v]=(i+)*(i+);
}
}
scanf("%s",str2);
len=strlen(str2);
vs=,vt=*len+;
NE=;
memset(head,-,sizeof(head));
for(int i=;i<len;i++){
Insert(vs,i+,,);
Insert(i++len,vt,,);
for(int j=i+;j<len;j++){
int u=str2[i]-'a',v=str2[j]-'a';
if(g[u][v]){
Insert(i+,j++len,,g[u][v]);
}
}
}
MinCostFlow(vs,vt);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,145
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,617
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,461
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,236
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,871
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,952