POJ 4113:北京地鐵票價-BeiJing Subway

做一道水題歡樂一下。


總時間限制:

1000ms
內存限制:
65536kB
描述

從2014年12月28日起,北京市公交地鐵將執行新的票價方案。其中地鐵的票價方案改爲:6公里(含)內3元;6公里至12公里(含)4元;12公里至22公里(含)5元;22公里至32公里(含)6元;32公里以上部分,每增加1元可乘坐20公里。

地鐵交通網絡有許多線路組成,每條線路又由多個地鐵站連接而成(爲簡化問題,部分線路和部分地鐵站被從地圖上略去)。比如地鐵4號線和1號線的部分地鐵站名稱和相鄰站之間的距離如下表:

4號線 起始/終到車站

區間距離(米)

圓明園——北京大學東門

1295

北京大學東門——中關村

887

中關村——海淀黃莊

900

海淀黃莊——人民大學

1063

人民大學——國家圖書館

2709

國家圖書館——西直門

1958

西直門——西四

3225

西四——西單

1880

西單——宣武門

815

……

1號線 起始/終到車站

區間距離(米)

軍事博物館——木樨地

1166

木樨地——復興門

1715

復興門——西單

1590

西單——天安門西

1217

天安門西——天安門東

925

天安門東——王府井

852

……


    如果我們從北京大學東門出發,前往天安門東,需要先乘坐地鐵4號線到達西單站,然後在西單換乘地鐵1號線,最後到達天安門東。整個行程的路程爲:

(887+900+1063+2709+1958+3225+1880)+ (1217+925) = 14764(米)

因此,按照新的票價方案,乘坐地鐵從北京大學東門到達天安門東需要花費5元。

    

    現在我們化簡地鐵網絡,假設網絡中至多隻有2條線路,並且網絡要麼是“一”字形,要麼是“十”字形,不存在環路(從一個站出發經過若干不同的站後又回到出發站)。

現在任意給出兩個地鐵站,從其中一個出發到達另外一個地鐵站的票價是多少?


輸入
第一行是一個整數cases(1<=cases<=10),表示測試數據的個數。下面是cases組測試數據。

對於每組測試數據,第一行是兩個整數L(1<=L<=2)和D(1<=D<=10),分別表示地鐵線路的個數和問題的個數。

然後是L行,每行表示一個地鐵線路。每行首先是一個數字M(2<=M<=20),表示這條線路上地鐵站的個數。然後是用空格隔開的M個字符串和(M-1)個整數,字符串是M個地鐵站的拼音,它們按線路上的順序依次排列;數字位於兩個字符串之間,表示這兩個相鄰地鐵站之間的距離。

然後是D行,每行兩個字符串,分別爲起點和終點地鐵站,要求輸出它們之間的票價。輸入數據保證線路中存在起點和終點,並且有唯一的路線可達。
輸出
對於每組測試數據,首先輸出一行”Casei:”,然後是D行,每行代表從起點到達終點地鐵站的票價。

注意,不同組測試數據之間的地鐵線路不能互用,例如,你不能用第1組測試數據的地鐵線路去求解第2組測試數據中的票價。
樣例輸入
21 25 YuanMingYuan 1295 BeiJingDaXueDongMen 887 ZhongGuanCun 900 HaiDianHuangZhuang 1063 RenMinDaXueBeiJingDaXueDongMen ZhongGuanCunBeiJingDaXueDongMen RenMinDaXue2 110 YuanMingYuan 1295 BeiJingDaXueDongMen 887 ZhongGuanCun 900 HaiDianHuangZhuang 1063 RenMinDaXue 2709 GuaJiaTuShuGuan 1958 XiZhiMen 3225 XiSi 1880 XiDan 815 XuanWuMen7 JunShiBoWuGuan 1166 MuXiDi 1715 FuXingMen 1590 XiDan 1217 TianAnMenXi 925 TianAnMenDong 852 WangFuJingBeiJingDaXueDongMen TianAnMenDong
樣例輸出
Case 1:33Case 2:5

這題比較簡單,就沒有使用圖的算法,直接在兩條線路上進行查找。

這裏使用vector容器來存儲地鐵站的信息,每個vector的元素是一個pair容器,存放站名和該站到下一站的距離。

用map存儲每個站的站名信息,這樣便於查找每個站點在哪條線路上。

count_price:根據距離來計算票價,這裏只要注意一下double到int轉換就可以了。

count_distance:根據地鐵線路的vector,和兩個地鐵站的名稱來計算兩個地鐵站之間的距離。

#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;

int count_price(int distance){  // 計算價格
	double km = distance / 1000;
	int price = 0;
	if (km <= 6)
		return 3;
	else if (km > 6 && km <= 12)
		return 4;
	else if (km > 12 && km <= 22)
		return 5;
	else if (km >22 && km <= 32)
		return 6;
	else {
		price = ((km - 32) / 20 + 6 + 1);
		return price;
	}
}

int count_distance(vector<pair<string,int> > lines_info, string station_a, string station_b){
	bool flag = false;
	int total_distance = 0;
	for (vector<pair<string, int> >::iterator it = lines_info.begin(); it != lines_info.end(); ++it){
		if (!flag && ((*it).first == station_a || (*it).first == station_b )){
			flag = true;
			total_distance += (*it).second;
			continue;
		}
		if (flag) {
			total_distance += (*it).second;
		}
		if (flag && ((*it).first == station_a || (*it).first == station_b)){
			total_distance -= (*it).second;
			break;
		}
	}
	return total_distance;
}

int main(){
	int cases, L, D, stations_count;
	string name, station_a, station_b, common_station;
	int distance, total_distance;
	bool flag = false;
	char buffer[50];
	cin >> cases;
	for (int m = 0; m < cases; ++m){
		cout << "Case " << m + 1 << ":" <<endl;
		cin >> L >> D;
		vector<pair<string, int> >* lines_info = new vector<pair<string, int> >[L];  // L是地鐵的線路
		map<string, int>* station_names = new map<string, int>[L];  // 用於判斷

		// 讀入地鐵線路的信息
		bool isFind = false;
		for (int i = 0; i < L; ++i){
			cin >> stations_count;
			while (stations_count --){  // 讀入地鐵站和地鐵站到下一站的長度
				if (stations_count != 0)
					cin >> name >> distance;
				else{
					cin >> name;
					distance = 0;
				}
				lines_info[i].push_back(pair<string, int>(name, distance));
				station_names[i].insert(pair<string, int>(name, distance));
				if (i == 1){  // 查找共同站點
					if ( !isFind && station_names[0].find(name) != station_names[0].end()){
						common_station = name;
						isFind = true;
					}
				}
			}
		}

		// 計算路程
		while (D--){
			cin >> station_a >> station_b;
			total_distance = 0;
			if (L == 1)
				cout << count_price(count_distance(lines_info[0], station_a, station_b)) << endl;
			else if (L == 2){
				// 計算的兩個站點都位於第一條線路
				if ((station_names[0].find(station_a) != station_names[0].end())
					&& (station_names[0].find(station_b) != station_names[0].end())){
					cout << count_price(count_distance(lines_info[0], station_a, station_b)) << endl;
				}
				// 計算的兩個站點都位於第二條線路
				else if ((station_names[1].find(station_a) != station_names[1].end())
					&& (station_names[1].find(station_b) != station_names[1].end())){
					cout << count_price(count_distance(lines_info[1], station_a, station_b)) << endl;
				}
				// 兩個站點位於不同的線路
				else{
					if (station_names[0].find(station_a) != station_names[0].end())
						cout << count_price(count_distance(lines_info[0], common_station, station_a)
							+ count_distance(lines_info[1], common_station, station_b)) << endl;
					else
						cout << count_price(count_distance(lines_info[1], common_station, station_a)
							+ count_distance(lines_info[0], common_station, station_b)) << endl;
				}
			}
		}
		delete [] lines_info;
	}
	return 0;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章