不知为何MLE了N遍。。。

C. MRT Map

time limit per test
3.0 s
memory limit per test
256 MB
input
standard input
output
standard output

Alaa and Duaa spent a long day in Universal Studios in Sentosa island, and now it's time to go back to home. The best way to get home is by using the train, starting from "HarbourFront" station and ending at "Serangoon" station.

Because the journey to home is long, Alaa and Duaa decided to invent a new game and play with it to spend time. In this game, they will use the train system map in Singapore (which called MRT map). The game goes as follow:

  1. Alaa will choose a station to start a journey from it.
  2. Duaa will choose a station to end the journey in it.
  3. Alaa and Duaa will start calculating the minimum cost to go from the starting station to the end station. And the winner is the one who can find the shortest distance from the starting station to the end station.

The cost to go from station u to stationv, is the number of common letters in the names of the two station. For example, the cost to go from "HarbourFront" station to "Serangoon" station is 4, because there exist 4 common letters, which are 'a', 'n', 'o' and 'r'. Note that each letter will be count once only (e.g. the letter 'o' exist two times in "HarbourFront" and two times "Serangoon", but it will add only 1 to the cost).

The letters in the names are case insensitive. For example the cost to go from city with name "AbC" to city with name "aBc" is 3, because the three letters in both names are common regardless of the state of the letter (uppercase or lowercase).

Alaa and Duaa need your help to find the shortest distance from the starting station to the end station, to announce the winner's name. Can you?

Input

The first line contains two integers n andm (2  ≤  n  ≤  104) (n - 1 ≤  m  ≤  min(105,)), wheren is the number of stations in the map, andm is the number of links between the stations.

Then n lines follow, each line i contains the name of the ith station in the map. The name of each station is a non-empty string consisting of English letters only, and with length no more than 20.

Then m lines follow, each line contains two integersu and v (1 ≤  u, v  ≤  n), which mean that there is a link between stationsu and v. It is guaranteed that no link will connect a station with itself, and there is at most one link between any two stations.

You can use the given links only to move between stations.

The next line contains two integers s ande (1  ≤  s, e  ≤  n), where s is the starting stating (which were chosen by Alaa), and e is the end station (which were chosen by Duaa). It is guaranteed that you can go from any station to any other station in the map.

Output

Print the shortest distance from the starting station s to the end statione.

Examples
Input
6 5
Bishan
Marymount
Caldecott
BotanicGardens
LorongGhuan
Serangoon
1 2
2 3
3 4
1 5
5 6
4 6
Output
19
Input
13 13
BuonaVista
OneNorth
KentRidge
HawParVilla
PasirPanjang
LabradorPark
TelokBlangah
HarbourFront
Commonwealth
Queenstown
Redhill
TiongBahru
OutramPark
1 2
2 3
3 4
4 5
5 6
6 7
7 8
1 9
9 10
10 11
11 12
12 13
13 8
10 8
Output
14
 
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN = 10010;
const int INF = 0x3f3f3f;
char station[MAXN][26];
bool used1[30];
bool used2[30];
bool v[MAXN];
int d[MAXN];
int w[MAXN][MAXN];

int Distance(char arr1[], char arr2[])
{
	int t = 0;
	memset(used1, 0, sizeof(used1));

	int len1 = strlen(arr1), len2 = strlen(arr2);
	for(int i =  0; i < len1; i++)
	{
		if(arr1[i] < 'a')
		{
			arr1[i] += 32; 
		 } 
		used1[arr1[i] - 'a'] = true;
		//cout<<arr1[i];
	}
		memset(used2, 0, sizeof(used2));
	for(int i = 0; i < len2; i++)
	{
		if(arr2[i] < 'a')
		{
			arr2[i] += 32;
		}
		//cout<<used2[arr2[i] - 'a']<<endl;;
		if(used1[arr2[i] - 'a'] == true && !used2[arr2[i] - 'a'])
		{
			t++;
			used2[arr2[i] - 'a'] = true;
			//cout<<arr2[i]<<" ";
		}
	 }
	// cout<<endl;
	 //cout<<t<<endl;
	 return t;
}

int Dijkstra(int s, int e, int n)
{
	memset(v, 0, sizeof(v));
	for(int i = 1; i <= n; i++)
	{
		d[i] = i == s?0:INF; 
	}
	for(int i = 1; i <= n; i++)
	{
		int index, m = INF;
		for(int j = 1; j <= n; j++)
		{
			if(!v[j] && d[j] < m)
			{
				m = d[j];
				index = j;
				v[index] = 1;
			}
		}
		for(int j = 1; j <= n; j++)
		{
			d[j] = min(d[j], d[index] + w[index][j]);
		}
	 }
	 return d[e];
}
int main()
{
	int n, m;
	int a, b;
	int s1, s2, t;
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= n; i++)
	{
		scanf("%s", &station[i]);
	}
	memset(w, INF, sizeof(w));
	for(int j = 1; j <= m; j++)
	{
		scanf("%d %d", &a, &b);
		w[a][b] = Distance(station[a], station[b]);
		w[b][a] = w[a][b];
	//	cout<<w[a][b]<<endl;
	}
	scanf("%d %d", &s1, &s2);
	t = Dijkstra(s1, s2, n);
	cout<<t<<endl;
	return 0;
	
 } 

猜你喜欢

转载自blog.csdn.net/swin16/article/details/78185606
MLE
今日推荐