ZOJ-2750 Idiomatic Phrases Game---Dijk最短路

题目链接:

https://vjudge.net/problem/ZOJ-2750

题目大意:

给定一本字典,字典里有很多成语,要求从字典里的第一个成语开始,运用字典里的成语变到最后一个成语,变得过程就是成语接龙,后一个成语的第一个字必须有前一个成语的最后一个字相等,给定的成语是4位16进制数字,每个成语前边跟的数字代表着找到这个成语之后再找到下个成语还需要t分钟 。(这里是找到该成语后还需要时间t,才能找到下一个成语)

思路:

每个成语有前面四个字符和后面四个字符,如果第i个成语后面字符等于第j个成语前面四个字符,那么这中间的消耗就是第i个成语自带的消耗T,按照这个原理就可以建图啦。然后用dijkstra求出从第一个成语到其他各个成语的最短路,然后判断最后一个成语是否可达,可达就输出d[n],不能就输出-1

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 #include<stack>
 8 #include<map>
 9 #include<sstream>
10 using namespace std;
11 typedef long long ll;
12 const int maxn = 1000 + 10;
13 const int INF = 1 << 25;
14 int T, n, m, cases;
15 int Map[maxn][maxn];
16 bool v[maxn];
17 int d[maxn];
18 void dijkstra(int u0)
19 {
20     memset(v, 0, sizeof(v));
21     for(int i = 1; i <= n; i++)d[i] = (i == u0 ? 0 : INF);
22     for(int i = 1; i <= n; i++)
23     {
24         int x, m = INF;
25         for(int j = 1; j <= n; j++)if(!v[j] && d[j] <= m)m = d[x = j];
26         v[x] = 1;
27         for(int j = 1; j <= n; j++)
28             d[j] = min(d[j], d[x] + Map[x][j]);//松弛操作
29     }
30 }
31 struct node
32 {
33     string s1, s2;
34     int time;
35 };
36 node a[maxn];
37 int main()
38 {
39     while(cin >> n && n)
40     {
41         string s;
42         int x;
43         for(int i = 1; i <= n; i++)
44         {
45             cin >> x >> s;
46             a[i].time = x;
47             a[i].s1 = a[i].s2 = "";
48             for(int j = 0, k = s.size() - 4; j < 4; j++, k++)
49             {
50                 a[i].s1 += s[j];
51                 a[i].s2 += s[k];
52             }
53             //cout<<a[i].s1<<" "<<a[i].s2<<endl;
54         }
55         for(int i = 1; i <= n; i++)//建图
56         {
57             for(int j = 1; j <= n; j++)
58             {
59                 Map[i][j] = INF;
60                 if(i == j)continue;
61                 if(a[i].s2 == a[j].s1)Map[i][j] = a[i].time;
62             }
63         }
64         dijkstra(1);
65         if(d[n] >= INF)cout<<"-1"<<endl;
66         else cout<<d[n]<<endl;
67     }
68     return 0;
69 }

原文地址:https://www.cnblogs.com/fzl194/p/8728315.html

时间: 2024-10-26 03:21:57

ZOJ-2750 Idiomatic Phrases Game---Dijk最短路的相关文章

ZOJ 2750 -- Idiomatic Phrases Game(Dijkstra)

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

ZOJ 2750 Idiomatic Phrases Game

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 w

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 bui

Idiomatic Phrases Game(图论最短路)

Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3504    Accepted Submission(s): 1182 Problem Description Tom is playing a game called Idiomatic Phrases Game. An idiom cons

hdu 1546 Idiomatic Phrases Game 最短路

Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2283    Accepted Submission(s): 742 Problem Description Tom is playing a game called Idiomatic Phrases Game. An idiom cons

HDU 1546 Idiomatic Phrases Game

成语接龙. 上一个的尾必须和下一个的首相同.注意:花费的时间是上一个. 一开始我就建图建错了. 比如第 i 个成语 与第 j 个成语, 第  i 个成语前面的时间为 t : 建图为 i -> j = t: 基友说这<图论算法理论.实现及应用>上有一样的题,我借来看了一下,发现它建图似乎有错误. 0->3 这条边的权值似乎错了. 反正我的建图是这样的 0->1=5;0->3=5; 1->2=5; 2->4=7; 3->4=15; 求 0 -> 4

Idiomatic Phrases Game HDU 1546

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

ZOJ 3781 Paint the Grid Reloaded (最短路)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意: 在n*m矩阵的图定义连通区域为x值或y值相同且颜色相同的连通,连通具有传递性 每次可以把一个连通区域颜色反转(O变X,X变O) 问把所有块的颜色变为X最小的步数 方法: 很巧妙的最短路问题,先建图,然后以每个顶点为起点,找单源最短路的最大的值(也就是最深的深度),然后这个值取min 建图:相邻的块连边权1的边(即:通过1次反转可以使得两个连通块变为一个连通块

hdu1546——Idiomatic Phrases Game

Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2191    Accepted Submission(s): 712 Problem Description Tom is playing a game called Idiomatic Phrases Game. An idiom cons

Idiomatic Phrases Game 成语接龙SPFA+map

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 w