Codeforces--237E--Build String(最小费用流)


Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

 Status

Description

You desperately need to build some string t. For that you've got n more strings s1, s2, ..., sn. To build string t, you are allowed to perform exactly |t| (|t| is the length of string t) operations on these strings. Each operation looks like that:

  1. choose any non-empty string from strings s1, s2, ..., sn;
  2. choose an arbitrary character from the chosen string and write it on a piece of paper;
  3. remove the chosen character from the chosen string.

Note that after you perform the described operation, the total number of characters in strings s1, s2, ..., sn decreases by 1. We are assumed to build string t, if the characters, written on the piece of paper, in the order of performed operations form string t.

There are other limitations, though. For each string si you know number ai — the maximum number of characters you are allowed to delete from string si. You also know that each operation that results in deleting a character from string si, costs i rubles. That is, an operation on string s1 is the cheapest (it costs 1 ruble), and the operation on string sn is the most expensive one (it costs n rubles).

Your task is to count the minimum amount of money (in rubles) you will need to build string t by the given rules. Consider the cost of building string t to be the sum of prices of the operations you use.

Input

The first line of the input contains string t — the string that you need to build.

The second line contains a single integer n(1 ≤ n ≤ 100) — the number of strings to which you are allowed to apply the described operation. Each of the next n lines contains a string and an integer. The i-th line contains space-separated string si and integer ai(0 ≤ ai ≤ 100). Number ai represents the maximum number of characters that can be deleted from string si.

All strings in the input only consist of lowercase English letters. All strings are non-empty. The lengths of all strings do not exceed 100 characters.

Output

Print a single number — the minimum money (in rubles) you need in order to build string t. If there is no solution, print -1.

Sample Input

Input
bbaze
3
bzb 2
aeb 3
ba 10
Output
8
Input
abacaba
4
aba 2
bcc 1
caa 2
bbb 5
Output
18
Input
xyz
4
axx 8
za 1
efg 4
t 1
Output
-1

Hint

Notes to the samples:

In the first sample from the first string you should take characters "b" and "z" with price 1 ruble, from the second string characters "a", "e" и "b" with price 2 rubles. The price of the string t in this case is 2·1 + 3·2 = 8.

In the second sample from the first string you should take two characters "a" with price 1 ruble, from the second string character "c" with price 2 rubles, from the third string two characters "a" with price 3 rubles, from the fourth string two characters "b" with price 4 rubles. The price of the string t in this case is 2·1 + 1·2 + 2·3 + 2·4 = 18.

In the third sample the solution doesn't exist because there is no character "y" in given strings.


题意:有一个目标字符串,然后有n个可供使用的字符串,为了组成目标字符串可以从备用的字符串中选取一定的字符,但是在每一个字符串中选取的字符个数有限,并且在每一个字符串中选取的字符都需要一定的花费,求组成目标字符串的最小花费

思路:超级源点向每一个需要使用的字符建边,花费为0,流量限制为这个字符的使用次数,然后每一个字符向字符串建边,花费为对应的行标,流量限制是字符出现的次数,然后每个字符串向超级汇点建边,边权为字符使用次数限制,花费为0

#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
#define MAXN 1010
#define INF 0x3f3f3f3f
struct node
{
	int u,v,cap,flow,cost,next;
}edge[MAXN*100];
int n,m,cnt[MAXN][MAXN],used[MAXN],num[MAXN],head[MAXN],cnt1;
int dis[MAXN],pre[MAXN],vis[MAXN];
char s[MAXN];
void add(int a,int b,int c,int d)
{
	node E={a,b,c,0,d,head[a]};
	edge[cnt1]=E;
	head[a]=cnt1++;
	node E1={b,a,0,0,-d,head[b]};
	edge[cnt1]=E1;
	head[b]=cnt1++;
}
bool BFS(int s,int t)
{
	memset(vis,0,sizeof(vis));
	memset(dis,INF,sizeof(dis));
	memset(pre,-1,sizeof(pre));
	vis[s]=1;
	dis[s]=0;
	queue<int>q;
	q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=head[u];i!=-1;i=edge[i].next)
		{
			node E=edge[i];
			if(dis[E.v]>dis[u]+E.cost&&E.cap>E.flow)
			{
				dis[E.v]=dis[u]+E.cost;
				pre[E.v]=i;
				if(!vis[E.v])
				{
					q.push(E.v);
					vis[E.v]=1;
				}
			}
		}
	}
	return pre[t]!=-1;
}
void mcmf(int s,int t,int &flow,int &cost)
{
	flow=cost=0;
	while(BFS(s,t))
	{
		int Min=INF;
		for(int i=pre[t];i!=-1;i=pre[edge[i^1].v])
		{
			node E=edge[i];
			Min=min(Min,E.cap-E.flow);
		}
		for(int i=pre[t];i!=-1;i=pre[edge[i^1].v])
		{
			edge[i].flow+=Min;
			edge[i^1].flow-=Min;
			cost+=edge[i].cost*Min;
		}
		flow+=Min;
	}
}
int main()
{
	while(scanf("%s",s)!=EOF)
	{
		memset(used,0,sizeof(used));
		memset(num,0,sizeof(num));
		memset(cnt,0,sizeof(cnt));
		memset(head,-1,sizeof(head));
		cnt1=0;
		int sum=strlen(s);
		for(int i=0;i<sum;i++)
		{
			int v=s[i]-'a'+1;
			num[v]++;
		}
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			memset(s,0,sizeof(s));
			scanf("%s%d",s,&used[i]);
			for(int j=0;j<strlen(s);j++)
			{
				int v=s[j]-'a'+1;
				cnt[i][v]++;
			}
		}
		int s=0,t=n+26+1;
		for(int i=1;i<=26;i++)
		{
			if(num[i]==0) continue;
			add(s,i,num[i],0);
			for(int j=1;j<=n;j++)
			{
				if(cnt[j][i]==0) continue;
				add(i,26+j,cnt[j][i],j);
			}
		}
		for(int i=1;i<=n;i++)
		add(i+26,t,used[i],0);
		int cost,flow;
		mcmf(s,t,flow,cost);
		if(flow!=sum)
			printf("-1\n");
		else
			printf("%d\n",cost);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值