C++&Python——【USACO 5.4.1】——Canada Tour

44 篇文章 0 订阅
8 篇文章 0 订阅

Canada Tour

You have won a contest sponsored by an airline. The prize is a ticket to travel around Canada, beginning in the most western point served by this airline, then traveling only from west to east until you reach the most eastern point served, and then coming back only from east to west until you reach the starting city. No city may be visited more than once, except for the starting city, which must be visited exactly twice (at the beginning and the end of the trip). You are not allowed to use any other airline or any other means of transportation.

Given a list of cities served by the airline and a list of direct flights between pairs of cities, find an itinerary which visits as many cities as possible and satisfies the above conditions beginning with the first city and visiting the last city on the list and returning to the first city.

PROGRAM NAME: tour

INPUT FORMAT

Line 1:The number N of cities served by the airline and the number V of direct flights that will be listed. N will be a positive integer not larger than 100. V is any positive integer.
Lines 2..N+1:Each line contains a name of a city served by the airline. The names are ordered from west to east in the input file. There are no two cities in the same meridian. The name of each city is a string of, at most, 15 digits and/or characters of the Latin alphabet; there are no spaces in the name of a city.
Lines N+2..N+2+V-1:Each line contains two names of cities (taken from the supplied list), separated by a single blank space. This pair is connected by a direct, two-way airline flight.

SAMPLE INPUT (file tour.in)

8 9	
Vancouver		
Yellowknife	
Edmonton
Calgary
Winnipeg
Toronto	
Montreal
Halifax	
Vancouver Edmonton
Vancouver Calgary	
Calgary Winnipeg
Winnipeg Toronto
Toronto Halifax
Montreal Halifax
Edmonton Montreal
Edmonton Yellowknife
Edmonton Calgary

OUTPUT FORMAT

Line 1:The number M of different cities visited in the optimal itinerary. Output 1 if no itinerary is possible.

SAMPLE OUTPUT (file tour.out)

7

Namely: Vancouver, Edmonton, Montreal, Halifax, Toronto, Winnipeg, Calgary, and Vancouver (but that's not a different city). 


加拿大之旅

你赢得了一个航空公司赞助的比赛。该奖项是一张前往加拿大各地旅行的机票,从最西端的航线开始,然后从西向东旅行直到到达最东端,然后才从东向西返回,直到到达起跑线。没有一个城市可以不止一次地参观,除了开始的城市,这个城市必须两次访问(在旅行开始和结束的时候)。你不允许使用任何其他航空公司或任何其他交通工具。

给出一组航空公司服务的城市和之间的直航城市列表,找到一个行程访问尽可能多的城市。

项目名称:tour

输入格式

第一行:航空公司服务的城市的数量和将的直飞航班的数量。N将是一个不大于100的正整数。V是任何正整数。

line 2 . .N+ 1:每行包含航空公司服务的城市名称。在输入文件中,城市从西方到东部列出。同一行上没有两个城市。每个城市的名称最多是一个字符串,最多有15个数字或拉丁字母的字符;在城市的名字里没有空格。

line N + 2 . .N+2+ v - 1:每行包含两个城市名称(从所提供的列表中取走),由一个空格隔开。这一对是通过直接、双向的航班连接的。

示例输入(文件tour.in)

8 9	
Vancouver		
Yellowknife	
Edmonton
Calgary
Winnipeg
Toronto	
Montreal
Halifax	
Vancouver Edmonton
Vancouver Calgary	
Calgary Winnipeg
Winnipeg Toronto
Toronto Halifax
Montreal Halifax
Edmonton Montreal
Edmonton Yellowknife
Edmonton Calgary

输出格式

第1行:在最佳行程中,不同城市的数量有多少。如果没有行程,则输出1。

样例输出(文件tour.out)

7个

例如:Vancouver, Edmonton, Montreal, Halifax, Toronto, Winnipeg, Calgary, 和 Vancouver(但这不是一个不同的城市)。


——一定要注意,需要达到最东部城市之后才能返回。开始时被这点阴到了。。


/*
ID : mcdonne1
LANG : C++
TASK : tour
*/

#pragma GCC optimize("O3")

#include <iostream>
#include <fstream>
#include <cstring>
#include <map>

using namespace std;

int n, v;
int f[101][101];
bool b[101][101];
string s1, s2;
map <string, int> m;

int main () {
	ifstream fin("tour.in", ios::in);
	ofstream fout("tour.out", ios::out);
	fin>>n>>v;
	for (int i = 1; i <= n; i++) {
		fin>>s1;
		m[s1] = i;
	}
	for (int i = 1; i <= v; i++) {
		fin>>s1>>s2;
		b[m[s1]][m[s2]] = b[m[s2]][m[s1]] = true;
	}
	memset (f, 128, sizeof(f));
	f[1][1] = 1;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			for (int k = 1; k <= min (i, j); k++)
				if (!(k == i and i ^ 1) and !(k == j and j ^ 1))
				{
					if (b[k][i]) f[i][j] = max (f[i][j], f[k][j] + 1);
					if (b[k][j]) f[i][j] = max (f[i][j], f[i][k] + 1);
				}
	if (f[n][n] < 0) f[n][n] = 2;
	fout<<f[n][n] - 1<<endl;
	return 0;
}

'''
ID : mcdonne1
LANG : PYTHON2
TASK : tour
'''
fin = open ('tour.in', 'r')
fout = open ('tour.out', 'w')
f = [[-1024 for i in range (110)] for j in range(110)]
b = [[False for i in range (110)] for j in range (110)]
m = {}
r = fin.readline().split()
n = int(r[0])
v = int(r[1])
for i in range (1, n + 1) :
	r = fin.readline().split()
	m[r[0]] = i
for i in range (1, v + 1) :
	r = fin.readline().split()
	s1 = r[0]
	s2 = r[1]
	b[m[s1]][m[s2]] = True
	b[m[s2]][m[s1]] = True
f[1][1] = 1
for i in range (1, n + 1) :
	for j in range (1, n + 1) :
		for k in range (1, min(i, j) + 1) :
			if (((k == i) and (i != 1)) == False) and (((k == j) and (j != 1)) == False) :
				if b[k][i] == True :
					f[i][j] = max (f[i][j], f[k][j] + 1)
				if b[k][j] == True :
					f[i][j] = max (f[i][j], f[i][k] + 1)
if f[n][n] < 0 :
	f[n][n] = 2
fout.write (str(f[n][n] - 1) + '\n')
fin.close()
fout.close()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值