杭电 hdu 1546 Idiomatic Phrases Game (最短路径 + Dijkstra)

杭电  hdu  1546   Idiomatic Phrases Game  (最短路径 + Dijkstra)



Idiomatic Phrases Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2504    Accepted Submission(s): 815


Problem Description
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
 

Author
ZHOU, Ran
 

Source
Zhejiang Provincial Programming Contest 2006
 

Recommend
linle   |   We have carefully selected several similar problems for you:   1548  1317  1535  3339  1217 






题意:成语接龙,就是要将第一个成语当作首,中间过程随便挑其它成语,最后要可以将最后一个成语连接起来,也就是让第一个和最后一个成语保持连接。
如果可以连接,输出最短的用时,不然输出-1
先给予一个n,接着有n行,然后是v和一串字符ch[],v表示连接这个成语的用时,ch[]表示成语




题解:这里可以用Dijkstra来算,通过从首个成语开始计算。
有几个要注意的地方:
1. 要注意,成语的字数,是将头4个字符当作首字,将后4个字符当做尾,只要前一个成语的尾和后一个成语的头相同,就可以连接
2. 要注意,成语的个数,不一定是四字成语,可能是很多很多,所以字符串要开的大一点,反正我是开1000以上
3. 连接的时候,最后的一个成语的时间不能算进去
4. 连接的时候,第一个成语的首,必须连接第一个成语的尾,这个要注意,不能化点之后就不注意连接




#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

const int maxn = 100005;
const int INF = 0x3f3f3f3f;               //无穷大

int hand[maxn];                  //静态邻接表的首
int dis[maxn];                    //记录首位置到各个位置的距离
int vist[maxn];                  //记录是不是访问过了这个点 0为没有访问过,1为访问过
int path[maxn];                  //记录一个个路径的过程
int top;
int begin1, end1, sum;           //记录从哪里开始,到结束的点,也就是从首成语的尾开始,到最后一个成语的头,然后加上首成语的时间

struct node{
    int from;
    int valou;
    friend bool operator < (node a, node b){               //重置
        return (a.valou > b.valou);
    }
};

mapmp;
priority_queuep;                    //优先队列

struct fun{                  //边的记录
    int from;
    int to;
    int v;
    int next;
}edge[maxn];

void Add_edge(int a, int b, int v1){               //创建静态邻接表
    edge[top].from = a;
    edge[top].to = b;
    edge[top].v = v1;
    edge[top].next = hand[a];              //类似基本的链表的插入,将一个个边放在hand[]后面,连接起来
    hand[a] = top++;
}

void Init (int n){                             //初始化
    int i, j, a, b, v, an = 1;
    char ch1[2000], ch2[10], ch3[10];

    mp.clear();
    memset (hand, -1, sizeof (hand));
    memset (vist, 0, sizeof (vist));
    memset (dis, INF, sizeof (dis));
    memset (path, -1, sizeof (path));
    top = 0;
    for (i = 0; i < n; ++i){                    
            scanf ("%d %s", &v, ch1);
            for (j = 0; j <= 3; ++j)             //提取成语的首字,当作边的首端点
                ch2[j] = ch1[j];
            ch2[4] = '\0';
            int k = 0, len;
            len = strlen (ch1);
            for (j = len - 4; j < len; ++j)           //提取成语的尾字,当作边的末端点
                ch3[k++] = ch1[j];
            ch3[4] = '\0';
            if (mp.find(ch2) != mp.end())
                a = mp[ch2];                  //将边数字化,更好使用
            else{
                mp[ch2] = an;
                a = an++;
            }
            if (mp.find(ch3) != mp.end())
                b = mp[ch3];
            else{
                mp[ch3] = an;
                b = an++;
            }
            if (i == 0){
                begin1 = b;                     //确定起始点
                sum = v;
            }
            if (i == n - 1){
                end1 = a;                     //确定结束点
            }
            else{
                Add_edge(a, b, v);
            }
        }
}

void Dijkstra (int a){
    node now, next;
    int from, v1, v2, u, i, to1;

    while (!p.empty()) p.pop();                    //清空队列
    now.from = a; 
    now.valou = 0;
    dis[a] = 0;
    p.push(now);                             //将起始点压入队列中
    while (!p.empty()){
        now = p.top();
        p.pop();
        u = now.from;
        v2 = now.valou;
        if (vist[u]) continue;                    //去除重复访问过的点
        vist[u] = 1;
        for (i = hand[u]; i != -1; i = edge[i].next){          //更新和这个点连接的所有点的路径长度
            v1 = edge[i].v;
            to1 = edge[i].to;
            if (vist[to1]) continue;
            if (v2 + v1 < dis[to1]){                      //如果这条路径的长度短,则更新到这个点的路径长度
                dis[to1] = v2 + v1;
                next.from = to1;
                next.valou = dis[to1];
                p.push(next);                          //将更新后的路径压入队列中,进行下次访问操作
                path[to1] = u;                          //记录路径的过程
            }
        }
    }
}

int main (){
    int n;

    while (scanf ("%d", &n) != EOF){
        if (n == 0) break;
        Init (n);
        Dijkstra (begin1);
        if (dis[end1] == INF)                  //如果路径没有更新过,则表示无法连通,输出-1
            printf ("-1\n");
        else
            printf ("%d\n", dis[end1] + sum);        //要加第一个成语的用时
    }

    return 0;
}


你可能感兴趣的:(最短路径,杭电)