当前位置: 首页 > 工具软件 > ROME > 使用案例 >

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

乌灿
2023-12-01

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;
}
 类似资料: