Sicily 1031. Campus(shortest path)

1031. CampusConstraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

At present, Zhongshan University has 4 campuses with a total area of 6.17 square kilometers sitting respectively on both sides of the Pearl River or facing the South China Sea. The Guangzhou South Campus covers an area of 1.17 square kilometers, the North Campus covers an area of 0.39 square kilometers, the Guangzhou East Campus has an area of 1.13 square kilometers and the Zhuhai Campus covers an area of 3.48 square kilometers. All campuses have exuberance of green trees, abundance of lawns and beautiful sceneries, and are ideal for molding the temperaments, studying and doing research.

Sometime, the professors and students have to go from one place to another place in one campus or between campuses. They want to find the shortest path between their source place S and target place T. Can you help them?

Input

The first line of the input is a positive integer C. C is the number of test cases followed. In each test case, the first line is a positive integer N (0<N<=100) that represents the number of roads. After that, N lines follow. The i-th(1<=i<=N) line contains two strings Si, Ti and one integer Di (0<=Di<=100). It means that there is a road whose length is Di between Si and Ti. Finally, there are two strings S and T, you have to find the shortest path between S and T. S, T, Si(1<=i<=N) and Ti(1<=i<=N) are all given in the following format: str_Campus.str_Place. str_Campus represents the name of the campus, and str_Place represents the place in str_Campus. str_Campus is "North", "South", "East" or "Zhuhai". str_Place is a string which has less than one hundred lowercase characters from "a-z". You can assume that there is at most one road directly between any two places.

Output

The output of the program should consist of C lines, one line for each test case. For each test case, the output is a single line containing one integer. If there is a path between S and T, output the length of the shortest path between them. Otherwise just output "-1" (without quotation mark). No redundant spaces are needed.

Sample Inputde style="color: rgb(51, 51, 51); font-size: 18.01801872253418px; line-height: 27.027027130126953px;" >12South.xiaolitang South.xiongdelong 2South.xiongdelong Zhuhai.liyuan 100South.xiongdelong South.xiaolitangde>Sample Outputde style="color: rgb(51, 51, 51); font-size: 18.01801872253418px; line-height: 27.027027130126953px;" >2de>Problem Source

ZSUACM Team Member

极度坑爹的一题,不过也学到了很多,顺便以这道来研究最短路径问题好了,,注意地点相同的特殊情况(坑),还有一个要记住的地方就是丫的给你100条路最多可以有200个地方啊- –

首先是没有用map的Floyd-Warshall算法:

#include <stdio.h>#include <string.h>#define INF 99999999#define MAX 115 * 2int total_num_of_name, map[MAX][MAX];char num_of_name[MAX][MAX];int min(int a, int b) {return a > b ? b : a;}void make_map(char f[], char t[], int dis) {int i, t_num, f_num;for (i = 0; i < total_num_of_name; i++) {if (strcmp(f, num_of_name[i]) == 0) {f_num = i;break;}}if (i >= total_num_of_name) {f_num = total_num_of_name;strcpy(num_of_name[total_num_of_name++], f);}for (i = 0; i < total_num_of_name; i++) {if (strcmp(t, num_of_name[i]) == 0) {t_num = i;break;}}if (i >= total_num_of_name) {t_num = total_num_of_name;strcpy(num_of_name[total_num_of_name++], t);}map[f_num][t_num] = dis < map[f_num][t_num] ? dis : map[f_num][t_num];map[t_num][f_num] = dis < map[t_num][f_num] ? dis : map[t_num][f_num];}int find_num(char a[]) {for (int i = 0; i < total_num_of_name; i++) {if (strcmp(a, num_of_name[i]) == 0)return i;}return -1;}void Floyd_Warshall() {int i, j, k;for (i = 0; i < MAX; i++) {map[i][i] = 0;}for (k = 0; k < total_num_of_name; k++) {for (i = 0; i < total_num_of_name; i++) {for (j = 0; j < total_num_of_name; j++) {map[i][j] = min(map[i][j], map[i][k] + map[k][j]);}}}}int main() {int case_num, n, dis, f_num, t_num;char temp_f[MAX], temp_t[MAX];scanf("%d", &case_num);while (case_num–) {scanf("%d", &n);for (int i = 0; i < MAX; i++) {for (int j = 0; j < MAX; j++) {map[i][j] = INF;}}memset(num_of_name, '\0', sizeof(num_of_name));total_num_of_name = 0;while (n–) {memset(temp_f, '\0', sizeof(temp_f));memset(temp_t, '\0', sizeof(temp_t));scanf("%s %s %d", temp_f, temp_t, &dis);make_map(temp_f, temp_t, dis);}scanf("%s%s", &temp_f, &temp_t);if (strcmp(temp_f, temp_t) == 0) {printf("0\n");continue;}f_num = find_num(temp_f);t_num = find_num(temp_t);if (f_num == -1 || t_num == -1) {printf("-1\n");continue;}Floyd_Warshall();printf("%d\n", map[f_num][t_num] >= INF ? -1 : map[f_num][t_num]);}return 0;}然后试试用map:

要么读书、要么旅行,灵魂和身体,必须有一个在路上。

Sicily 1031. Campus(shortest path)

相关文章:

你感兴趣的文章:

标签云: