码迷,mamicode.com
首页 > 其他好文 > 详细

Idiomatic Phrases Game HDU 1546

时间:2015-06-08 23:16:58      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

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


迪杰斯特拉算法,加了一些离散化。

算法核心:

1.建起点到每条边的边权INF,与起点相连的为初始边权。

2.每次取最近的点,标记访问情况。

3.将该点相邻的点更新与起点的距离。

执行以上知道所有点都访问过

这样以来数组d中储存的就是起点到其他所有点的最短距离。

  1 #include<cstring>
  2 #include<cstdio>
  3 #include<algorithm>
  4 #include<cstdio>
  5 #include<vector>
  6 #include<iostream>
  7 #include<map>
  8 #include<string>
  9 
 10 using namespace std;
 11 
 12 #define INF 0x3f3f3f3f
 13 
 14 map<string,int>mp;
 15 vector<int>in[1005];
 16 int cost[1005][1005];
 17 int vis[1005];
 18 int cur,aim;
 19 int n;
 20 
 21 void init()
 22 {
 23     cur=0;
 24     for(int i=0;i<=1000;i++) vis[i]=0,in[i].clear();
 25     for(int i=0;i<=1000;i++)
 26         for(int j=0;j<=1000;j++)
 27         cost[i][j]=INF;
 28     mp.clear();
 29 }
 30 
 31 int D(int s,int e)
 32 {
 33     int d[1005];
 34     for(int i=0;i<cur;i++) d[i]=INF;
 35 
 36     int k,maxn=INF;
 37 
 38     for(int i=0;i<in[s].size();i++)
 39         d[in[s][i]]=cost[s][in[s][i]];
 40 
 41     d[s]=0;
 42 
 43     vis[s]=1;
 44 
 45     while(1)
 46     {
 47         maxn=INF;
 48         k=-1;
 49         for(int i=0;i<cur;i++)
 50         {
 51             if(d[i]<maxn&&!vis[i])
 52                 maxn=d[i],k=i;
 53         }
 54 
 55         if(k==-1) break;
 56         vis[k]=1;
 57 
 58         for(int i=0;i<in[k].size();i++)
 59             if(!vis[in[k][i]]&&cost[k][in[k][i]]+maxn<d[in[k][i]])
 60                 d[in[k][i]]=cost[k][in[k][i]]+maxn;
 61     }
 62 
 63     return d[e];
 64 }
 65 
 66 int main()
 67 {
 68     while(scanf("%d",&n)!=EOF&&n)
 69     {
 70         init();
 71         int fee;
 72         for(int i=0;i<n;i++)
 73         {
 74 
 75             int co;
 76             string temp;
 77             cin>>co>>temp;
 78 
 79             string tm;
 80             string tn;
 81 
 82             for(int i=0;i<4;i++) tm+=temp[i];
 83             for(int i=temp.size()-4;i<temp.size();i++) tn+=temp[i];
 84 
 85             int s,t;
 86             if(mp[tm]&&mp[tn])
 87             {
 88                 s=mp[tm],t=mp[tn];
 89                 in[s].push_back(t);
 90             }
 91             else if(mp[tm]&&!mp[tn])
 92             {
 93                 mp[tn]=cur++;
 94                 s=mp[tm],t=mp[tn];
 95                 in[s].push_back(t);
 96             }
 97             else if(!mp[tm]&&mp[tn])
 98             {
 99                 mp[tm]=cur++;
100                 s=mp[tm],t=mp[tn];
101                 in[s].push_back(t);
102             }
103             else if(!mp[tm]&&!mp[tn])
104             {
105                 mp[tm]=cur++;mp[tn]=cur++;
106                 s=mp[tm],t=mp[tn];
107                 in[s].push_back(t);
108             }
109 
110             cost[s][t]=min(co,cost[s][t]); //此处一定要注意更新最小边权
111 
112             if(i==n-1) fee=co,aim=mp[tm];
113         }
114 
115         int flag=D(0,aim);
116         if(flag>=INF) cout<<"-1"<<endl;
117         else cout<<flag<<endl;
118     }
119 }

 



Idiomatic Phrases Game HDU 1546

标签:

原文地址:http://www.cnblogs.com/wsaaaaa/p/4562156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!