PAT甲级1087. All Roads Lead to Rome
PAT甲级1087. All Roads Lead to Rome
题意:
确实有从我们这个城市到罗马的不同的旅游线路。您应该以最低的成本找到您的客户的路线,同时获得最大的幸福。
输入规格:
每个输入文件包含一个测试用例。对于每种情况,第一行包含2个正整数N(2 <= N <= 200),城市数,K,
双城之间的路线总数;其次是起始城市的名称。下一个N-1行每个都给出一个城市的名字和一个整数,代表从城市可以获得的幸福,除了起始城市。然后K行跟随,每个描述两个城市之间的路线,格式为“City1 City2 Cost”。
这里一个城市的名字是3个英文字母,目的地总是ROM代表罗马。
输出规格:
对于每个测试用例,我们应该以最低的成本找到路由。如果这样一条路线不是独一无二的,那么最好是幸福的路线。如果这样的路线还不是唯一的,
那么我们输出一个具有最大平均幸福感的人 - 法官保证这样的解决方案存在并且是独一无二的。
因此,在第一行输出中,您必须打印4个数字:成本最低,成本,幸福的不同路线的数量,
和推荐路线的平均幸福(仅占整数部分)。然后在下一行,您应该以“City1-> City-> ...-> ROM”的格式打印路线。
思路:
Dijkstra找出最短路径,dfs遍历找到所有的路线,根据题意找出recommend的路线。 题意要看清呀。
the one with the maximum happiness will be recommended
这个我还以为是有包含最大的happiness的city的路线。= =。捉急。。其实是总和的happiness的最大就ok了。
ac代码:
C++
// pat1087.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<map>
#include<cmath>
#include<unordered_map>
#include<unordered_set>
using namespace std;
const int INF = 0x7fffffff;
int n, k;
string starting;
unordered_map<string, int> citytoi;
unordered_map<int,string> itocity;
unordered_map<int, int> happiness;
int mymap[201][201];
int Dijkstra()
{
vector<int> len(n, INF);
vector<int> visit(n, 0);
len[0] = 0;
int now, minlen;
while (1)
{
now = -1;
minlen = INF;
for (int i = 0; i < n; i++)
{
if (!visit[i] && len[i] < minlen)
{
minlen = len[i];
now = i;
}
}
if (now == -1 || now == citytoi["ROM"]) break;
visit[now] = 1;
for (int i = 0; i < n; i++)
{
if (!visit[i] && mymap[now][i] && mymap[now][i] + minlen < len[i])
{
len[i] = mymap[now][i] + minlen;
}
}
}
return len[citytoi["ROM"]];
}
vector<vector<int> >res;
void dfs(vector<int> temp, int count, int need, int now,vector<int>& visit)
{
visit[now] = 1;
if (count > need) return;
if (count == need && now == citytoi["ROM"])
{
int maxhapp = happiness[temp[0]];
int len = temp.size();
int sum = 0;
for (int i = 0; i < len; i++)
{
maxhapp = max(maxhapp, happiness[temp[i]]);
sum += happiness[temp[i]];
}
temp.insert(temp.begin(), sum / len); //1 avg
temp.insert(temp.begin(), sum); //0 sum
res.push_back(temp);
return;
}
for (int i = 0; i < n; i++)
{
if (!visit[i] && mymap[now][i])
{
temp.push_back(i);
dfs(temp, count + mymap[now][i], need, i, visit);
visit[i] = 0;
temp.pop_back();
}
}
}
bool cmp(vector<int>& a, vector<int>& b)
{
if (a[0] != b[0]) return a[0] > b[0];
else return a[1] > b[1];
}
int main()
{
scanf("%d %d", &n, &k);
cin >> starting;
//input happiness
citytoi[starting] = 0;
itocity[0] = starting;
happiness[0] = 0;
char city1[4],city2[4];
string c1, c2;
int num;
for (int i = 1; i <= n - 1; i++)
{
scanf("%s %d", city1, &num);
c1 = string(city1);
citytoi[c1] = i;
itocity[i] = c1;
happiness[i] = num;
}
//input route
int ic1, ic2;
for (int i = 0; i < k; i++)
{
scanf("%s %s %d", city1, city2, &num);
c1 = string(city1), c2 = string(city2);
ic1 = citytoi[c1], ic2 = citytoi[c2];
mymap[ic1][ic2] = mymap[ic2][ic1] = num;
}
//caculate
int need = Dijkstra();
vector<int> temp;
vector<int> dfsvisit(n, 0);
dfs(temp, 0, need, 0, dfsvisit);
//output
sort(res.begin(), res.end(), cmp);
printf("%d %d %d %d\n",res.size(), need, res[0][0], res[0][1]);
cout << starting << "->";
int len = res[0].size();
for (int i = 2; i < len - 1; i++)
{
cout << itocity[res[0][i]] << "->";
}
cout << itocity[res[0][len - 1]] << endl;
return 0;
}
PAT甲级1087. All Roads Lead to Rome的更多相关文章
- PAT 甲级 1087 All Roads Lead to Rome(SPFA+DP)
题目链接 All Roads Lead to Rome 题目大意:求符合题意(三关键字)的最短路.并且算出路程最短的路径有几条. 思路:求最短路并不难,SPFA即可,关键是求总路程最短的路径条数. 我 ...
- PAT 甲级 1087 All Roads Lead to Rome
https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984 Indeed there are many ...
- PAT甲级——A1087 All Roads Lead to Rome【30】
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...
- PAT 1087 All Roads Lead to Rome[图论][迪杰斯特拉+dfs]
1087 All Roads Lead to Rome (30)(30 分) Indeed there are many different tourist routes from our city ...
- PAT 1087 All Roads Lead to Rome
PAT 1087 All Roads Lead to Rome 题目: Indeed there are many different tourist routes from our city to ...
- [图的遍历&多标准] 1087. All Roads Lead to Rome (30)
1087. All Roads Lead to Rome (30) Indeed there are many different tourist routes from our city to Ro ...
- 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)
题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...
- PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra
题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...
- PAT (Advanced Level) 1087. All Roads Lead to Rome (30)
暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
随机推荐
- 64_s1
SAASound-3.2-17.fc26.i686.rpm 13-Feb-2017 22:13 27650 SAASound-3.2-17.fc26.x86_64.rpm 13-Feb-2017 23 ...
- python批量替换文件名
替换关键字 #-*-coding:utf-8-*- import os import re filepath = u'E:\\CMMI4\\07_测试文档' files = os.walk(filep ...
- Machine Learning系列--EM算法理解与推导
EM算法,全称Expectation Maximization Algorithm,译作最大期望化算法或期望最大算法,是机器学习十大算法之一,吴军博士在<数学之美>书中称其为“上帝视角”算 ...
- Windows搭建RobotFramework环境(一)
Robot Framework官网 http://robotframework.org/http://robotframework.org/ 安装说明 https://github.com/robot ...
- eclipse 常见问题之字体更改、添加注释模板
有些同学可能会和我有一样的困扰,每次想要更改字体大小.背景颜色等,都需要百度一下才知道怎么去做...不知道有没有这种情况的孩子,反正我经常遇到,老是记不住,今天写下来,顺带自己忘记的时候可以查看一下. ...
- django 项目中的 favicon.ico 处理
django 项目中的 favicon.ico 处理 (django == 2.0.6) 1. 引入模块: from django.views.generic.base import Redirec ...
- mybatis注解使用
spring整合mybatis时,要使用mybatis的注解,需要spring-config.xml文件中添加下述配置: <!--下述配置指明了我们的Mapper,即Dao,在相应的包里也就可以 ...
- Java多线程之赛跑游戏(含生成exe文件)
在JavaSE中,多线程是一个重要的内容. 我们要了解多线程的概念,就要先了解进程的概念:要了解进程的概念,就离不开操作系统的概念. 在一台正常运行的电脑中,计算机硬件(如CPU.内存.硬盘.网卡.显 ...
- 开始学习MaxCompute
https://help.aliyun.com/document_detail/34615.html?spm=a2c4g.11186623.6.688.jVxTMW
- jquery 获取某a标签的href地址 实现页面加载时跳转
jQuery(document).ready(function(){if(jQuery("#zzjg a").length>0){var hrefValue = jQuery ...