【HDU - 1546】 Idiomatic Phrases Game(Dijkstra,可选map处理字符串)

题干:

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

题目大意:

    成语接龙游戏,多组输入,输入格式为:   第一行是成语个数n,后面n行:“当前成语出发找下一个成语” 的权值(题目描述为 时间),以及后面的汉字(以16进制表示,最少三个汉字),首尾相连找从1连到n的最短路。

解题报告:

     成语接龙游戏,权值val(不是这个题的maze啊)比较有意思,跟这题的权值maze有点类似:【POJ - 3037】Skiing (Dijkstra算法),相似之处在于权值只与边的起点有关  即:同一个Point发出的边的边权是相同的。poj3037那个题就是利用了这一点,推出了起点(1,1)和任一点的maze值。而此题是通过val来更新的maze值。思想类似。这题亦可以用map来保存字符串的首尾字符。

AC代码:

#include<bits/stdc++.h>using namespace std;
const int MAX = 1000 + 5;
const int INF = 0x3f3f3f3f;
int len[MAX],val[MAX];
char str[MAX][MAX];
//
bool vis[MAX];
int maze[MAX][MAX],dis[MAX];
int n;
void Dijkstra(int u,int v) {int minv,minw,all = n;dis[u] = 0;while(all--) {minw = INF;for(int i = 1; i<=n; i++) {if(!vis[i] && dis[i]<minw) {minv = i;minw = dis[i];}}vis[minv] = 1;if(minv == v) return ;for(int i = 1; i<=n; i++) {if(!vis[i] && dis[i]>dis[minv] + maze[minv][i]) dis[i] = dis[minv] + maze[minv][i];}}}void init() {memset(len,0,sizeof(len) );memset(val,INF,sizeof(val) );memset(vis,0,sizeof(vis));memset(maze,INF,sizeof(maze));memset(dis,INF,sizeof(dis));
}
bool ok(int i, int j) {if(str[i][len[i]-1] == str[j][3] && str[i][len[i] - 2] == str[j][2] && str[i][len[i]-3] == str[j][1] && str[i][len[i]-4] == str[j][0]) {return true;}return false;
}
int main()
{while(~scanf("%d",&n) ) {if(n == 0) break;init();for(int i = 1; i<=n; i++) {scanf("%d %s",&val[i],str[i]);len[i] = strlen(str[i]);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(ok(i,j)) maze[i][j] = val[i];}}
//		for(int i = 1; i<=n; i++) {
//			for(int j = 1; j<=n; j++) {
//				printf("%d ",maze[i][j]) ;
//			}
//			printf("\n");
//		}Dijkstra(1,n);if(dis[n] == INF) printf("-1\n");else printf("%d\n",dis[n]);}return 0 ;} 

总结:

    1.还是那句话 写好了 init()函数不调用是什么操作?

    2.init中对数组的初始化别落下,,比如这次就落下了dis数组。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441946.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java迭代器修改链表_Java恼人的迭代器不会返回链表中的元素

给出以下代码&#xff1a;public void insertIntoQueue(float length,int xElement,int yElement,int whichElement){Dot dot new Dot(xElement,yElement);GeometricElement element null;// some codeint robotX,robotY;boolean flag false;for (Iterator i robotList.ite…

(精)【ACM刷题之路】POJ题目详细多角度分类及推荐题目

POJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(poj…

java输入正确的信息_判断用户输入的信息是否正确

package com.Embed.util;import java.sql.Connection;import java.sql.DriverManager;import java.text.SimpleDateFormat;import java.util.Date;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Common {// 判断用户输入的时间格式是否正确publ…

【UVA - 10815】 Andy's First Dictionary(STL+字符处理)

题干&#xff1a; Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. F…

java里dir是什么意思_关于文件系统:为什么user.dir系统属性在Java中工作?

我读过的几乎每篇文章都告诉我&#xff0c;你不能用Java创建chdir。 这个问题的公认答案说你不能用Java做到这一点。但是&#xff0c;这里有一些我尝试过的东西&#xff1a;geocodebox:~$ java -versionjava version"1.6.0_14"Java(TM) SE Runtime Environment (buil…

【Uva - 10935】 Throwing cards away I (既然是I,看来还有Ⅱ、Ⅲ、Ⅳ?)(站队问题队列问题)

题干&#xff1a; Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top card and move the card that …

腾讯 tars java_腾讯TARS开源团队郑苏波:腾讯微服务开发框架的源码剖析

郑苏波&#xff1a;大家下午好&#xff01;我是腾讯微服务的郑苏波。先做一个框架的介绍&#xff0c;Tars是一个支持多语言内嵌服务治理功能的框槛&#xff0c;能跟DevOps比较好的协同开发。这个框架四大特点&#xff1a;一是对用户透明实现&#xff0c;让业务可以聚焦自己的逻…

【POJ - 3310】Caterpillar(并查集判树+树的直径求树脊椎(bfs记录路径)+dfs判支链)

题干&#xff1a; An undirected graph is called a caterpillar if it is connected, has no cycles, and there is a path in the graph where every node is either on this path or a neighbor of a node on the path. This path is called the spine of the caterpillar …

*【POJ - 1860】Currency Exchange (单源最长路---Bellman_Ford算法判正环)

题干&#xff1a; Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specia…

使用java开发应用程序_使用Java中的插件支持开发应用程序

我一直在研究如何开发可以加载插件的应用程序.到目前为止,我已经看到这可以通过定义一个接口来实现,并让插件实现它.但是,我当前的问题是如何在Jars中打包时加载插件.有没有“最好的”方法呢&#xff1f;我正在考虑的当前逻辑是让每个插件和他们的Jar内部寻找实现接口的类.但我…

【POJ - 1995】Raising Modulo Numbers(裸的快速幂)

题干&#xff1a; People are different. Some secretly read magazines full of interesting girls pictures, others create an A-bomb in their cellar, others like using Windows, and some like difficult mathematical games. Latest marketing research shows, that t…

软件设计师下午题java_2018上半年软件设计师下午真题(三)

● 阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】生成器( Builder)模式的意图是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。图6-1所示为其类图。【Java代码】import java.util.*&#xff1b;class Product {priv…

【ZOJ - 2724】【HDU - 1509】Windows Message Queue(优先队列)

题干&#xff1a; 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 change, the system will add a message to the queue. Meanwhil…

java细粒度锁_Java细粒度锁实现的3种方式

最近在工作上碰见了一些高并发的场景需要加锁来保证业务逻辑的正确性&#xff0c;并且要求加锁后性能不能受到太大的影响。初步的想法是通过数据的时间戳&#xff0c;id等关键字来加锁&#xff0c;从而保证不同类型数据处理的并发性。而java自身api提供的锁粒度太大&#xff0c…

【POJ - 1062】【nyoj - 510】昂贵的聘礼 (Dijkstra最短路+思维)

题干&#xff1a; 年轻的探险家来到了一个印第安部落里。在那里他和酋长的女儿相爱了&#xff0c;于是便向酋长去求亲。酋长要他用10000个金币作为聘礼才答应把女儿嫁给他。探险家拿不出这么多金币&#xff0c;便请求酋长降低要求。酋长说&#xff1a;"嗯&#xff0c;如果…

php e notice,PHP函数之error_reporting(E_ALL ^ E_NOTICE)详细说明

举例说明&#xff1a;在Windows环境下&#xff1a;原本在php4.3.0中运行正常的程序&#xff0c;在4.3.1中为何多处报错&#xff0c;大体提示为&#xff1a;Notice:Undefined varialbe:变量名称. 例如有如下的代码&#xff1a; 复制代码 代码如下:if (!$tmp_i) { $tmp_i10; }在4…

【POJ - 3169】 Layout(差分约束+spfa)(当板子记?)

题干&#xff1a; Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 < N < 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are nu…

php中pregmatch,php中preg_match的isU代表什么意思

isU是大小写分的意思&#xff0c;这里s还有则不包括换行符而U是反转了匹配数量的值使其不是默认的重复,大概就是这样了个体我们看文章。正则后面的/(.*)/isU &#xff0c;“isU”参数代表什么意思&#xff1f;这是正则中的修正符.i是同时查找大小写字母,s是圆点(.)匹配所有字符…

【POJ - 1511】 Invitation Cards(Dijkstra + 反向建图 多源到单源最短路的处理)

题干&#xff1a; In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all…

【HDU - 3499】 Flight (单源最短路+优惠问题)

题干&#xff1a; Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to some other city to avoid meeting her. He will travel only by air and he can go to any city if there exists a flight and it can help him re…