【Codeforces Round】 #147 (Div. 2) E. Build String(最小费用最大流,最大流量判断)

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 100characters.

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.

题目大意:你有一个想要得到的串s,你有n个可供操作的串,现在的话就是让你用这些可供操作的串组成串s,

其实就是让你从下面的串中得到串s所有的字符(包含数量),但是他给了一个串可供操作的数量,也就是说,一个串,你最多可以获得其中的a[i]个字符,然后就是每个字符的价格为他所在的第i个字符串,

思路:其实这个题目的题意我都理解了很长时间的,后来发现很多东西都是没什么用的。。。

首先说这个题目很容易就想到了,网络流中的最小费用最大流,流量要和我们设定的那个出口流量(也就是字符串的长度)相等的,还有就是每一个字符串可以提供的数量是一定的,也就是说我们还要设定一个流量来控制他的a[i]

下面是我建图的图

其中有的边的权值我没有画完的,简单说一下吧, 基本的格式是这样的   容量/花费 

前面的S连接的就是我们的字符串,上面的容量就是我们最多可以在这个字符串中取得的字符数量,花费为0

中间的红线就是代表了这个串可以提供的字符数量也就是容量,下面的就是他的花费

最后的就是我们S串中真正需要的多少个相应的字符串

PS:最后的流量要和S串是相等的,不然的话就是不能组成上面的S串

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
#define INF 1e9
using namespace std;
const int maxn=1000+10;
struct Edge
{
    int from,to,cap,flow,cost;
    Edge(){}
    Edge(int f,int t,int c,int fl,int co):from(f),to(t),cap(c),flow(fl),cost(co){}
};

struct MCMF
{
    int n,m,s,t;
    vector<Edge> edges;
    vector<int> G[maxn];
    bool inq[maxn];
    int d[maxn];
    int p[maxn];
    int a[maxn];

    void init(int n,int s,int t)
    {
        this->n=n, this->s=s, this->t=t;
        edges.clear();
        for(int i=0;i<n;++i) G[i].clear();
    }

    void AddEdge(int from,int to,int cap,int cost)
    {
        edges.push_back(Edge(from,to,cap,0,cost));
        edges.push_back(Edge(to,from,0,0,-cost));
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool BellmanFord(int &flow, int &cost)
    {
        for(int i=0;i<n;++i) d[i]=INF;
        memset(inq,0,sizeof(inq));
        d[s]=0, a[s]=INF, inq[s]=true, p[s]=0;
        queue<int> Q;
        Q.push(s);
        while(!Q.empty())
        {
            int u=Q.front(); Q.pop();
            inq[u]=false;
            for(int i=0;i<G[u].size();++i)
            {
                Edge &e=edges[G[u][i]];
                if(e.cap>e.flow && d[e.to]>d[u]+e.cost)
                {
                    d[e.to]= d[u]+e.cost;
                    p[e.to]=G[u][i];
                    a[e.to]= min(a[u],e.cap-e.flow);
                    if(!inq[e.to]){ Q.push(e.to); inq[e.to]=true; }
                }
            }
        }
        if(d[t]==INF) return false;
        flow +=a[t];
        cost +=a[t]*d[t];
        int u=t;
        while(u!=s)
        {
            edges[p[u]].flow += a[t];
            edges[p[u]^1].flow -=a[t];
            u = edges[p[u]].from;
        }
        return true;
    }

    int Min_cost()
    {
        int flow=0,cost=0;
        while(BellmanFord(flow,cost));
        return cost;
    }
    int solve(int k)
    {
        int flow=0,cost=0;
        while(BellmanFord(flow,cost));
        return flow==k? cost:-1;
    }
}mcmf;
int mp['z'+10],temp['z'+10];
int a[maxn];
int main()
{
    //cout<<int('a')<<endl;
    string s;
    int n;
    cin>>s>>n;
    for(int i=0;i<s.length();i++)
        mp[s[i]]++;
    int S=0,T=200;
    mcmf.init(n+501,S,T);
    string t;
    for(int i=1;i<=n;i++)
    {
        memset(temp,0,sizeof(temp));
        cin>>t>>a[i];
        mcmf.AddEdge(S,i,a[i],0);
        for(int j=0;j<t.length();j++)
        {
            temp[t[j]]++;
        }
        for(int j='a';j<='z';j++)
        {
            mcmf.AddEdge(i,j+4,temp[j],i);
        }
    }
    for(int i='a';i<='z';i++)
    {
        mcmf.AddEdge(i+4,T,mp[i],0);
    }
    int ans=mcmf.solve(s.length());
    printf("%d\n",ans);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值