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;
    }
    
    
  • 相关阅读:
    rally测试后生成的html报告,无法正常展示
    开机自动执行的脚本
    python设计模式-命令模式
    解决docker命令行终端显示不全的问题
    ansible简单入门
    python函数的执行过程
    rally task配置文件
    python调试工具remote_pdb
    rally使用tempest进行测试
    rally测试opentack------安装部署和简单实践
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852307.html
Copyright © 2011-2022 走看看