【剑指紫金港】1087 All Roads Lead to Rome Dijkstra+DFS

12 篇文章 0 订阅
4 篇文章 0 订阅

A 1087 All Roads Lead to Rome

题目链接

Problem Description

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

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 ROMwhich represents Rome.

Output

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

题目大意

有N个点,K条边,每个点有一个名字,给定一个起点,终点是名字为"ROM"的点。输入除了起点外N-1个点的权值,然后再输入K条边的长度。求起点到终点的最短路径并输出;如果最短路径有多条,选路径上的点权值累加最大的那条路线输出;如果权值累加最大有多条,选平均权值最大的那条路线输出。

解题思路

多条最短路选最优方案题型,裸DFS会超时,必须先用Dijkstra算法先求出最短路路径并存储用于多种最短路方案的搜索。d[ ]存储起点到其他所有点的最短距离,node结构体数组存放所有最短路方案的前驱节点(用于回溯路径)。dfs中x是起点,y是终点,nhay是当前累加的点权值,num是当前搜索了多少个点,用于求平均权值。具体看代码理解。

AC代码

#include <bits/stdc++.h>
using namespace std;
#define max(a, b) ((a) > (b) ? (a) : (b))
#define INF 0x3f3f3f3f
typedef long long ll;

const int maxn = 205;

int graph[maxn][maxn],hay[maxn],vis[maxn],d[maxn],n,k,happy,cost,stop,sum=0;
int maxhay=0,maxave=0;
string ans[maxn],start,city,ct1,ct2;
unordered_map<string, int> idHash;
vector<int> path,npath;

struct Node{
	int ind;
	int dpath[maxn];
}node[maxn];

void Dijkstra(int x){
	fill(d, d+maxn, INF);
	fill(vis, vis+maxn, 0);
	d[x]=0;
	for(int i=0; i<n; i++){
		int u=-1,maxe=INF;
		for(int j=0; j<n; j++){
			if(!vis[j] && d[j]<maxe){
				maxe=d[j];
				u=j;
			}
		}
		if(u==-1){return ;}
		vis[u]=1;
		for(int j=0; j<n; j++){
			if(!vis[j] && graph[u][j]!=-1){
				if(maxe+graph[u][j]<d[j]){
					d[j]=maxe+graph[u][j];
					node[j].ind=0;
					node[j].dpath[node[j].ind++]=u;
				}else if(maxe+graph[u][j]==d[j]){
					node[j].dpath[node[j].ind++]=u;
				}
			}
		}
	}
}

void dfs(int x, int y, int nhay, int num){
	if(x==y){
		sum++;
		if(nhay>maxhay){
			path=npath;
			maxhay=nhay;
			maxave=nhay/num;
		}else if(nhay == maxhay){
			if(nhay/num > maxave){
				path = npath;
				maxave = nhay/num;
			}
		}
		return ;
	}
	for(int i=0,limit=node[y].ind; i<limit; i++){
		int now=node[y].dpath[i];
		npath.emplace_back(now);
		dfs(x, now, nhay+hay[now], num+1);
		npath.pop_back();
	}
}

int main(int argc, char **argv){
	for(int i=0; i<maxn; i++){
		for(int j=0; j<maxn; j++){
			graph[i][j]=-1;
		}
	}
	cin>>n>>k>>start;
	idHash[start]=0;
	ans[0]=start;
	for(int i=1; i<n; i++){
		cin>>city>>happy;
		hay[i]=happy;
		ans[i]=city;
		idHash[city]=i;
	}
	stop=idHash["ROM"];
	for(int i=0; i<k; i++){
		cin>>ct1>>ct2>>cost;
		int a=idHash[ct1];
		int b=idHash[ct2];
		graph[a][b]=graph[b][a]=cost;
	}
	Dijkstra(0);
	dfs(0, stop, hay[stop], 0);
	printf("%d %d %d %d\n",sum, d[stop], maxhay, maxave);
	for(auto it=path.rbegin(); it!=path.rend(); it++){
		cout<<ans[*it]<<"->";
	}
	printf("ROM");
    return 0;
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

征服所有不服

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值