单源最短路(时间复杂度,传参数的注意)

本文介绍了一种使用最短路径算法解决成语接龙游戏问题的方法。通过建立成语之间的连接,并利用图论中的算法求解从起始成语到目标成语的最短路径,从而找到完成游戏所需的最短时间。

 此题错在:传字符串参数会超时,找了好长时间的

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
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;

map<string,ll>m;
struct node{
	ll y;
	ll d;
	ll next_s;
}side[1000*1000];

ll cnt,head[1001],flag[1001],dis[1001],num[1001],cnt_s[1001];
vector<string>p;

bool judge(ll x,ll y,ll k){
	ll len=cnt_s[k];
	for(int i=len-4,j=0;j<4;i++,j++){
		if(p[x-1][i]!=p[y-1][j]){
			return false;
		}
	}
	 return true;
}

void init(){
	memset(head,-1,sizeof(head));
	cnt=0;
}

void add(ll x,ll y,ll d){
	side[cnt].y=y;
	side[cnt].d=d;
	side[cnt].next_s=head[x];
	head[x]=cnt++;
}

int main(){
	ll n;
	while(scanf("%lld",&n)&&n){
		for(int i=0;i<=n;i++){
			cnt_s[i]=0;
		}
		p.clear();
		init();
		ll t,cn=1;
		string s;
		for(int i=1;i<=n;i++){
			cin>>t>>s;
			cnt_s[cn]=s.size();
			num[cn++]=t;
			p.push_back(s);
		}
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n;j++){
				//坑坑坑,直接传字符串复杂度会飙升,最好传字符床所在的下标
				if(judge(i,j,i)){ 
					add(i,j,num[i]);
				}
				if(judge(j,i,j)){
					add(j,i,num[j]);
				}
			}
		}
		for(int i=1;i<=n;i++){
			flag[i]=0;
			dis[i]=INF;
		}
		for(int i=head[1];i!=-1;i=side[i].next_s){
			dis[side[i].y]=side[i].d;
		}
		dis[1]=0;
		flag[1]=1;
		ll sign=0;
		for(int i=1;i<n;i++){
			ll min_s=INF,index;
			for(int j=1;j<=n;j++){
				if(!flag[j]&&dis[j]<min_s){
					min_s=dis[j];
					index=j;
				}
			}
			if(min_s==INF){
				sign=1;
				printf("-1\n");
				break;
			}
			if(index==n){
				break;
			}
			flag[index]=1;
			for(int k=head[index];k!=-1;k=side[k].next_s){
				if(!flag[side[k].y]&&dis[side[k].y]>dis[index]+side[k].d){
					dis[side[k].y]=dis[index]+side[k].d;
				}
			}
		}
		if(sign==0){
			printf("%lld\n",dis[n]);
		}
	}	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值