zoukankan      html  css  js  c++  java
  • NEAU 2019积分赛第五场 题目订正

     

    G-String Problem

    Boy Valera likes strings. And even more he likes them, when they are identical. That's why in his spare time Valera plays the following game. He takes any two strings, consisting of lower case Latin letters, and tries to make them identical. According to the game rules, with each move Valera can change one arbitrary character Ai in one of the strings into arbitrary character Bi, but he has to pay for every move a particular sum of money, equal to Wi. He is allowed to make as many moves as he needs. Since Valera is a very economical boy and never wastes his money, he asked you, an experienced programmer, to help him answer the question: what minimum amount of money should Valera have to get identical strings.

    Input

    The first input line contains two initial non-empty strings s and t, consisting of lower case Latin letters. The length of each string doesn't exceed 105. The following line contains integer n (0 ≤ n ≤ 500) — amount of possible changings. Then follow n lines, each containing characters Ai and Bi (lower case Latin letters) and integer Wi (0 ≤ Wi ≤ 100), saying that it's allowed to change character Ai into character Bi in any of the strings and spend sum of money Wi.

    Output

    If the answer exists, output the answer to the problem, and the resulting string. Otherwise output -1 in the only line. If the answer is not unique, output any.

    Examples

    Input

    uayd
    uxxd
    3
    a x 8
    x y 13
    d c 3
    

    Output

    21
    uxyd
    

    Input

    a
    b
    3
    a b 2
    a b 3
    b a 5
    

    Output

    2
    b
    

    Input

    abc
    ab
    6
    a b 4
    a b 7
    b a 8
    c b 11
    c a 3
    a c 0
    

    Output

    -1

    简单的最短路问题

    除了现场过的4题以外最可做的题

    在比赛是错误的估算了时间复杂度

    使用了一种错误的预处理+松弛的操作 最后wa到结尾

    再遇到两种方法决策时 要迅速权衡利弊

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <ctime>
    #include <string>
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <algorithm>
    #include <iostream>
    #define ll long long
    using namespace std;
    char temp1[400005];
    char temp2[400005];
    char ans[400005];
    int dis[40][40];
    int mp[100][100];
    int main()
    {
        //freopen("in.txt","r",stdin);
            memset(dis,0x3f,sizeof(dis));
            memset(mp,0x3f,sizeof(mp));
            scanf("%s%s",temp1+1,temp2+1);
            int n;
            char temp3[5];
            char temp4[5];
            int temp5;
            int temp6;
            int temp7;
            scanf("%d",&n);
            for(int i=1; i<=n; i++)
            {
                scanf("%s%s%d",temp3,temp4,&temp5);
                temp6=temp3[0]-'a'+1;
                temp7=temp4[0]-'a'+1;
                if(temp5<mp[temp6][temp7]) mp[temp6][temp7]=temp5;
                else continue;
            }
            for(int i=1; i<=26; i++)
            {
                for(int j=1; j<=26; j++)
                {
                    dis[i][j]=mp[i][j];
                    if(i==j) dis[i][j]=0;
                }
            }
            for(int k=1; k<=26; k++)
            {
                for(int i=1; i<=26; i++)
                {
                    for(int j=1; j<=26; j++)
                    {
                        if(dis[i][j]>dis[i][k]+dis[k][j])
                        {
                            dis[i][j]=dis[i][k]+dis[k][j];
                        }
                    }
                }
            }
            int len1=strlen(temp1+1);
            int len2=strlen(temp2+1);
            int MIN=1e9;
            int pos=-1;
            int flag=1;
            long long sum=0;
            if(len1==len2)
            {
            for(int i=1;i<=len1;i++)
            {
                MIN=1e9;
                pos=-1;
                for(int j=1;j<=26;j++)
                {
                    if(dis[temp1[i]-'a'+1][j]+dis[temp2[i]-'a'+1][j]<MIN)
                    {
                        MIN=dis[temp1[i]-'a'+1][j]+dis[temp2[i]-'a'+1][j];
                        pos=j;
                    }
                }
                if(MIN>=1e9||pos==-1)
                {
                    flag=0;
                    break;
                }
                else
                {
                    sum+=MIN;
                    ans[i]=pos+'a'-1;
                }
            }
            }
            if(flag==0||len1!=len2)
            {
                printf("-1
    ");
            }
            else
            {
                printf("%lld
    ",sum);
                printf("%s
    ",ans+1);
            }
        return 0;
    }
    
    
  • 相关阅读:
    C# 验证IP地址、Email格式、URl网址
    如何创建、安装和调试Windows服务
    C#发送Email邮件方法总结
    C#调用java类、jar包方法。
    未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序
    在已经存在的表上创建索引
    Windows下的.NET+ Memcached安装
    把表从Access2007导出到Sql Server
    FusionCharts参数说明
    Sublime Text 3 之配置package control
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852307.html
Copyright © 2011-2022 走看看