zoukankan      html  css  js  c++  java
  • Idiomatic Phrases Game(最短路+注意坑点)

    Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters and has a certain meaning. This game will give Tom two idioms. He should build a list of idioms and the list starts and ends with the two given idioms. For every two adjacent idioms, the last Chinese character of the former idiom should be the same as the first character of the latter one. For each time, Tom has a dictionary that he must pick idioms from and each idiom in the dictionary has a value indicates how long Tom will take to find the next proper idiom in the final list. Now you are asked to write a program to compute the shortest time Tom will take by giving you the idiom dictionary. 

    InputThe input consists of several test cases. Each test case contains an idiom dictionary. The dictionary is started by an integer N (0 < N < 1000) in one line. The following is N lines. Each line contains an integer T (the time Tom will take to work out) and an idiom. One idiom consists of several Chinese characters (at least 3) and one Chinese character consists of four hex digit (i.e., 0 to 9 and A to F). Note that the first and last idioms in the dictionary are the source and target idioms in the game. The input ends up with a case that N = 0. Do not process this case. 
    OutputOne line for each case. Output an integer indicating the shortest time Tome will take. If the list can not be built, please output -1.Sample Input

    5
    5 12345978ABCD2341
    5 23415608ACBD3412
    7 34125678AEFD4123
    15 23415673ACC34123
    4 41235673FBCD2156
    2
    20 12345678ABCD
    30 DCBF5432167D
    0

    Sample Output

    17
    -1

    这个题的坑点在字符串的长度上
    代码:
    vector版
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<set>
    #include<map>
    #include<vector>
    #include<cmath>
    #define Inf 0x3f3f3f3f
    
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    char str[1005][105];
    int w[1005];
    struct node
    {
        int pos;
        int w;
        node (int x,int y)
        {
            pos=x;
            w=y;
        }
        bool friend operator < (node x,node y)
        {
            return x.w>y.w;
        }
    };
    vector<node>vec[1005];
    int dis[1005];
    int vis[1005];
    int n,m;
    void init()
    {
        for(int t=1;t<=n;t++)
        {
            dis[t]=Inf;
        }
        memset(vis,0,sizeof(vis));
    }
    
    void Dijkstra(int s)
    {
        priority_queue<node>q;
        q.push(node(s,0));
        dis[s]=0;
        while(!q.empty())
        {
            node now=q.top();
            q.pop();
            if(vis[now.pos])continue;
            vis[now.pos]=1;
            for(int t=0;t<vec[now.pos].size();t++)
            {
                node to=vec[now.pos][t];
                if(to.w+dis[now.pos]<dis[to.pos])
                {
                    dis[to.pos]=to.w+dis[now.pos];
                    to.w=dis[to.pos];
                    q.push(to);
                }
            }
        }
    }
    bool ok(int i, int j) 
    {
        int len=strlen(str[i]);
        if(str[i][len-1] == str[j][3] && str[i][len - 2] == str[j][2] && str[i][len-3] == str[j][1] && str[i][len-4] == str[j][0]) {
            return true;
        }
        return false;
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF)
       {
           if(n==0)
           {
               break;
        }
           init();
           for(int t=1;t<=n;t++)
           {
               vec[t].clear();
        }
           for(int t=1;t<=n;t++)
           {
               scanf("%d %s",&w[t],str[t]);
        }
        for(int t=1;t<=n;t++)
        {
            for(int j=1;j<=n;j++)
            {
            
                    if(ok(t,j))
                    vec[t].push_back(node(j,w[t]));
                
            }
        }
        Dijkstra(1);
        if(dis[n]!=Inf)
        printf("%d
    ",dis[n]);
        else
        {
            puts("-1");
        }
       }
        return 0;
    }

    邻接表版

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<set>
    #include<vector>
    #include<map>
    #include<cmath>
    #define Inf 0x3f3f3f3f
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    struct edge
    {
        int u,v,w;
        int next;
    }Edge[10*maxn];
    int w[1005];
    char str[1005][105];
    struct node
    {
        int pos,w;
        node(int x,int y)
        {
            pos=x;
            w=y;
        }
        bool friend operator < (node x,node y)
        {
            return x.w>y.w;
        }
    };
    int head[1005],dis[1005],vis[1005],cnt;
    void add(int u,int v,int w)
    {
        Edge[cnt].u=u;
        Edge[cnt].v=v;
        Edge[cnt].w=w;
        Edge[cnt].next=head[u];
        head[u]=cnt++;
    }
    void Dijkstra(int s)
    {
        dis[s]=0;
        priority_queue<node>q;
        q.push(node(s,0));
        while(!q.empty())
        {
            node now=q.top();
            q.pop();
            if(vis[now.pos])continue;
            vis[now.pos]=1;
            
            for(int i=head[now.pos];i!=-1;i=Edge[i].next)
            {
                if(dis[now.pos]+Edge[i].w<dis[Edge[i].v])
                {
                    dis[Edge[i].v]= dis[now.pos]+Edge[i].w;
                    q.push(node(Edge[i].v,dis[Edge[i].v]));
                } 
            }
        } 
        return ;
    }
    bool ok(int i, int j) 
    {
        int len=strlen(str[i]);
        if(str[i][len-1] == str[j][3] && str[i][len - 2] == str[j][2] && str[i][len-3] == str[j][1] && str[i][len-4] == str[j][0]) {
            return true;
        }
        return false;
    }
    int main()
    {
       int n,m;
       while(scanf("%d",&n)!=EOF)
       {
           if(n==0)
           {
               break;
        }
       cnt=0;
       memset(head,-1,sizeof(head));
       memset(dis,Inf,sizeof(dis)); 
       memset(vis,0,sizeof(vis));
          for(int t=1;t<=n;t++)
           {
               scanf("%d %s",&w[t],str[t]);
        }
        for(int t=1;t<=n;t++)
        {
            for(int j=1;j<=n;j++)
            {
            
                    if(ok(t,j))
                    add(t,j,w[t]);
            }
        }
       Dijkstra(1);
       if(dis[n]!=Inf)
       printf("%d
    ",dis[n]);
       else
       {
            printf("-1
    ");
       }
      }
       return 0;
    }
  • 相关阅读:
    简单的评价星星
    存储过程的调用
    连接Oracle的帮助类
    Oracle基础知识笔记
    myBatis自动生成mapping,dao和model
    Spring +SpringMVC 实现文件上传功能。。。
    嵌入式发展
    The Onion Router洋葱路由器
    NFC芯片
    中国计算机学会推荐国际学术刊物 (网络与信息安全)
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/11274685.html
Copyright © 2011-2022 走看看