【PAT刷题甲级】1087.All Roads Lead to Rome

1087 All Roads Lead to Rome (30 分)

Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.

Input Specification

Each input file contains one test case. For each case, the first line contains 2 positive integers N (2≤N≤200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N−1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format City1 City2 Cost. Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.

Output Specification

For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommanded. If such a route is still not unique, then we output the one with the maximum average happiness – it is guaranteed by the judge that such a solution exists and is unique.

Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommanded route. Then in the next line, you are supposed to print the route in the format City1->City2->…->ROM.

Sample Input

6 7 HZH
ROM 100
PKN 40
GDN 55
PRS 95
BLN 80
ROM GDN 1
BLN ROM 1
HZH PKN 1
PRS ROM 2
BLN HZH 2
PKN GDN 1
HZH PRS 1

Sample Output

3 3 195 97
HZH->PRS->ROM

题意

从起始城市到罗马花费最小的路径,若不止一条,选择幸福值最大的路径,输出花费最小方案数,花费金额,最大幸福值,以及平均幸福值(保留整数即可)(Dijkstra+DFS)

代码

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
map<string,int> m;		//城市名--编号
map<int,string> rm;		//编号--城市名
vector<int> pre[210];
vector<int> path,temppath;
int n,k,G[210][210],st;
int d[210],num[210];	//最小花费金额,最小花费方案数
int w[210];				//每个城市幸福值(点权)
bool vis[210]= {false};
const int inf=0x3fffffff;
int opt=-1;
void Dijkstra(int st) {
	fill(d,d+210,inf);
	d[st] = 0;
	num[st] = 1;
	for(int i=1; i<=n; i++) {
		int u=-1,minn=inf;
		for(int j=1; j<=n; j++) {
			if(vis[j]==false && d[j]<minn) {
				u=j;
				minn=d[j];
			}
		}
		if(u==-1)	return;
		vis[u]=true;
		for(int v=1; v<=n; v++) {
			if(vis[v]==false && G[u][v]!=inf) {
				if(d[u]+G[u][v]<d[v]) {
					d[v]=d[u]+G[u][v];
					pre[v].clear();
					pre[v].push_back(u);
					num[v] = num[u];
				} else if(d[u]+G[u][v]==d[v]) {
					num[v] += num[u];
					pre[v].push_back(u);
				}
			}
		}
	}
}
void DFS(int v) {
	if(v==st) {
		temppath.push_back(v);
		int value=0;
		for(int i=temppath.size()-1; i>=0; i--) {
			int id=temppath[i];
			value += w[id];
		}
		if(value>opt) {
			opt=value;
			path=temppath;
		}
		temppath.pop_back();
		return;
	}
	temppath.push_back(v);
	for(int i=0; i<pre[v].size(); i++) {
		DFS(pre[v][i]);
	}
	temppath.pop_back();
}
int main() {
	scanf("%d%d",&n,&k);
	int hp,cost;
	string s,tmp,a,b;
	cin >> s;
	m[s]=1,rm[1]=s,w[1]=0;
	st = m[s];
	for(int i=2; i<=n; i++) {
		cin >> tmp >> hp;
		m[tmp] = i;
		rm[i] = tmp;
		w[i] = hp;
	}
	fill(G[0],G[0]+210*210,inf);
	for(int i=0; i<k; i++) {
		cin >> a >> b >> cost;
		int id1=m[a],id2=m[b];
		G[id1][id2]=G[id2][id1]=cost;
	}
	Dijkstra(st);
	int ed=m["ROM"];
	DFS(ed);
	printf("%d %d %d %d\n",num[ed],d[ed],opt,opt/(path.size()-1));
	for(int i=path.size()-1; i>=0; i--) {
		printf("%s",rm[path[i]].c_str());
		if(i!=0)	printf("->");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, based on the given problem, here is a class diagram that represents the entities and their relationships: ``` +-----------------+ +-----------------+ | Country |<>----------| City | +-----------------+ +-----------------+ | | | | | - name | | - name | | - cities | | - roads | | | | | | + addCity() | | + addRoad() | | + removeCity() | | + removeRoad() | +-----------------+ +-----------------+ ^ | | | | | +----------------------+ | TravelingUnit | +----------------------+ | | | - type | | - driver | | - wheels/legs | | - weapons | | - canFire | | - maxSpeed | | | | + move() | | + captureByBandit() | +----------------------+ ^ | | | | | +---------------------+ | Driver | +---------------------+ | | | - nationality | | - name | | | +---------------------+ ``` Explanation: - The `Country` class represents a country and has a relationship with multiple `City` objects through the `cities` attribute. It also has attributes like `name` and methods like `addCity()` and `removeCity()` for managing cities. - The `City` class represents a city and has a relationship with multiple `Road` objects through the `roads` attribute. It has attributes like `name` and methods like `addRoad()` and `removeRoad()` for managing roads. - The `TravelingUnit` class represents a traveling unit and contains attributes like `type`, `driver`, `wheels/legs`, `weapons`, `canFire`, and `maxSpeed`. It has methods like `move()` for moving the unit and `captureByBandit()` for capturing the unit. - The `Driver` class represents a driver and contains attributes like `nationality` and `name`. It is associated with the `TravelingUnit` class. Note: This is a basic representation of the system based on the given problem statement. There might be additional classes or relationships required depending on the specific requirements of the system.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值