zoj 1750 Idiomatic Phrases Game (dijkstra)

Idiomatic Phrases Game


Time Limit: 2 Seconds      Memory Limit: 65536 KB



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.

Input

The 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.

Output

One 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

题意:成语接龙,一个字符的后四个字符和另一个字符的前四个字符相同,则连一条边,求0至n-1的最短路。

#include"stdio.h"
#include"string.h"
#include"queue"
#include"algorithm"
using namespace std;
#define N 1005
#define inf 0x7fffffff
int mark[N],g[N][N];
int dis[N];
struct node
{
    char s1[5],s2[5];
    int w;
}e[N];
void dijkstra(int s,int n)
{
    int i,u,min;
    for(i=0;i<n;i++)
        dis[i]=g[s][i];
    memset(mark,0,sizeof(mark));
    mark[s]=1;
    while(1)
    {
        u=0;
        min=inf;
        for(i=0;i<n;i++)
        {
            if(!mark[i]&&dis[i]<min)
            {
                min=dis[i];
                u=i;
            }
        }
        if(u==0)
            break;
        mark[u]=1;
        for(i=0;i<n;i++)
        {
            if(!mark[i]&&g[u][i]<inf&&dis[i]>dis[u]+g[u][i])
            {
                dis[i]=dis[u]+g[u][i];
            }
        }
    }
    if(dis[n-1]<inf)
        printf("%d\n",dis[n-1]);
    else
        printf("-1\n");
}
int main()
{
    int i,j,k,n,w;
    char str[50];
    while(scanf("%d",&n),n)
    {
        for(i=0;i<n;i++)
        {
            scanf("%d %s",&w,&str);
            int m=strlen(str);
            for(j=0;j<4;j++)
                e[i].s1[j]=str[j];
            e[i].s1[j]='\0';
            for(k=0,j=m-4;j<m;j++)
                e[i].s2[k++]=str[j];
            e[i].s2[k]='\0';
            e[i].w=w;
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                g[i][j]=(i==j?0:inf);
                if(strcmp(e[i].s2,e[j].s1)==0)
                    g[i][j]=e[i].w;
                if(strcmp(e[i].s1,e[j].s2)==0)
                    g[j][i]=e[j].w;
            }
        }
        dijkstra(0,n);
    }
    return 0;
}

zoj 1750 Idiomatic Phrases Game (dijkstra)

时间: 2024-10-25 07:07:11

zoj 1750 Idiomatic Phrases Game (dijkstra)的相关文章

ZOJ 2750 -- Idiomatic Phrases Game(Dijkstra)

 ZOJ 2750 -- Idiomatic Phrases Game(Dijkstra) 题意 : 给定一本字典,字典里有很多成语,要求从字典里的第一个成语开始,运用字典里的成语变到最后一个成语,变得过程就是成语接龙,后一个成语的第一个字必须有前一个成语的最后一个字相等,给定的成语是4位16进制位,每个成语前边跟的数字代表着找到这个成语之后再找到下个成语还需要t分钟 . 思路 : 将所有的成语看成一个点,如果找到下一个成语,就建一条有向边,然后用dijkstra求最短路. 1 #include

最短路径算法——迪杰斯特拉算法(Dijkstra)

图结构中应用的最多的就是最短路径的查找了,关于最短路径查找的算法主要有两种:迪杰斯特拉算法(Dijkstra)和Floyd算法. 其中迪杰斯特拉算法(Dijkstra)实现如下: 原理就是不断寻找当前的最优解: void main() { int V[Max][Max]={0,8,32,Infinity,Infinity, 12,0,16,15,Infinity, Infinity,29,0,Infinity,13, Infinity,21,Infinity,0,7, Infinity,Infi

hdu 1874(Dijkstra )

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27692    Accepted Submission(s): 10019 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路

poj1062昂贵的聘礼(Dijkstra**)

1 /* 2 题意: 物主有一个物品,价值为P,地位为L, 以及一系列的替代品Ti和该替代品所对应的"优惠"Vi 3 g[u][i] 表示的是u物品被i物品替换后的优惠价格!(u>0, i>0) 4 g[u][0]表示不用替换该物品的实际价格 ! 5 d[0]表示的是第一个物品经过一系列的物品替换之后的最少优惠价格! 6 7 思路:每当我们通过Dijkstra算法得到离源点(1)最近的距离的节点 p的时候(也就是1...pre[p], p)这条 8 路径上的物品互相替换后得

ZOJ 2724 Windows 消息队列 (优先队列)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2724 Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text cha

ZOJ 1743 Concert Hall Scheduling(DP)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=743 题意:有两个音乐厅出租.给出n个租客,每个租客有个租的时间段[L,R],以及租费.任意时候音乐厅只能租给最多一个租客.问如何选择租给哪些租客使得赚的钱最多? 思路:f[i][j]表示第一个音乐厅到时刻i.第二个到时刻j,可以获得的最大值. struct node { int x,y,w; int operator<(const node &a) const

迪杰斯特拉(dijkstra)算法的简要理解和c语言实现(源码)

迪杰斯特拉(dijkstra)算法:求最短路径的算法,数据结构课程中学习的内容. 1 . 理解 算法思想::设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合(用S表示,初始时S中只有一个源点,以后每求得一条最短路径 , 就将 加入到集合S中,直到全部顶点都加入到S中,算法就结束了),第二组为其余未确定最短路径的顶点集合(用U表示),按最短路径长度的递增次序依次把第二组的顶点加入S中.在加入的过程中,总保持从源点v到S中各顶点的最短路径长度不大于从源点v

zoj 3696 Alien&#39;s Organ(泊松分布)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3696 Alien's Organ Time Limit: 2 Seconds      Memory Limit: 65536 KB There's an alien whose name is Marjar. It is an universal solder came from planet Highrich a long time ago. Marjar

ZOJ 1642 Match for Bonus(dp)

Match for Bonus Time Limit: 2 Seconds      Memory Limit: 65536 KB Roy played a game with his roommates the other day. His roommates wrote 2 strings of characters, and gave each character a bonus value. When Roy pinned the positions of one common char