您的位置:首页 > 产品设计 > UI/UE

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

2016-04-27 20:52 741 查看

Build String

Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u
Submit 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:

choose any non-empty string from strings s1, s2, ..., sn;
choose an arbitrary character from the chosen string and write it on a piece of paper;
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 8za 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: