Codeforces round #147 (Div. 2) E build string (minimum cost Stream)

Source: Internet
Author: User

Question: Here is a target string str_t. You can use the following n strings (STR [N] [N]). the limit [I] characters are selected from each STR [I] string. The cost of taking a character from the string I is I. Ask the minimum price for the string. If the string cannot be str_t, output-1.

Set a super Source Vertex st and a super sink vertex ed. Create an edge with a capacity of limit [I] from the Super source point to each STR [I] at a cost of 0. Split the target string into 26 letters. Based on the number of characters in the target string, the edge from this character to the super sink point is created. The size is the number of the destination string, and the cost is 0. Create an edge of the letter from STR [I] to the target number for each character in STR [I]. The size is 1, and the cost is I.

#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <string>#include <set>#include <map>#include <cmath>#include <algorithm>#include <queue>using namespace std;const int MaxM=200001;const int INF=(1<<30);const int N=105;struct Edge{int u,v,pre,cap,cost;Edge(){}Edge(int u,int v,int pre,int cap,int cost) :u(u),v(v),pre(pre),cap(cap),cost(cost) {}}edge[MaxM];int head[MaxM],nEdge,re_flow;void init(){nEdge=0;memset(head,-1,sizeof(head));}void addEdge(int u,int v,int cap,int cost){edge[nEdge]=Edge(u,v,head[u],cap,cost);head[u]=nEdge++;edge[nEdge]=Edge(v,u,head[v],0,-cost);head[v]=nEdge++;}struct MinCostFlow{queue<int> que;int vis[MaxM],pre[MaxM],dis[MaxM],pos[MaxM];int spfa(int s,int t,int n){for(int i=0;i<=n;i++){pre[i]=-1; vis[i]=0; dis[i]=INF;}que.push(s); pre[s]=s; dis[s]=0; vis[s]=1;while(!que.empty()){int u=que.front();que.pop(); vis[u]=0;for(int i=head[u];i!=-1;i=edge[i].pre){int v=edge[i].v,cost=edge[i].cost;if(edge[i].cap>0&&dis[u]+cost<dis[v]){dis[v]=dis[u]+cost;pre[v]=u; pos[v]=i;if(!vis[v]){vis[v]=1;que.push(v);}}}}if(pre[t]!=-1&&dis[t]<INF) return 1;return 0;}void solve(int s,int t,int n,int &flow,int &cost){flow=0,cost=0;while(spfa(s,t,n)){int mi=INF;for(int u=t;u!=s;u=pre[u]) mi=min(mi,edge[pos[u]].cap);flow+=mi;cost+=mi*dis[t];for(int u=t;u!=s;u=pre[u]){edge[ pos[u] ].cap-=mi;edge[ pos[u]^1 ].cap+=mi;}}}}flow;void input(int &st,int &ed){    int n,len,limit[N],cnt[30]={0};    char str_t[N],str[N][N];    scanf("%s %d",str_t,&n);    st=0;    for(int i=1;i<=n;i++)    {        scanf("%s %d",str[i],&limit[i]);        addEdge(st,i,limit[i],0);        len=(int)strlen(str[i]);        for(int j=0;j<len;j++)        {            addEdge(i,n+str[i][j]-'a'+1,1,i);        }    }    re_flow=len=(int)strlen(str_t);    for(int i=0;i<len;i++)    {        cnt[str_t[i]-'a']++;    }    ed=n+31;    for(int i=0;i<30;i++)    {        if(cnt[i]) addEdge(n+i+1,ed,cnt[i],0);    }}int main(){    init();    int st,ed,mx_flow,cost;    input(st,ed);    flow.solve(st,ed,ed-st+1,mx_flow,cost);    if(mx_flow!=re_flow) cout<<-1<<endl;    else cout<<cost<<endl;    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.