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的更多相关文章

  1. PAT 甲级 1087 All Roads Lead to Rome(SPFA+DP)

    题目链接 All Roads Lead to Rome 题目大意:求符合题意(三关键字)的最短路.并且算出路程最短的路径有几条. 思路:求最短路并不难,SPFA即可,关键是求总路程最短的路径条数. 我 ...

  2. PAT 甲级 1087 All Roads Lead to Rome

    https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984 Indeed there are many ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. [图的遍历&多标准] 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 ...

  7. 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)

    题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...

  8. PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra

    题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...

  9. PAT (Advanced Level) 1087. All Roads Lead to Rome (30)

    暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

随机推荐

  1. Django 1.10文档中文版Part3

    目录 2.7 第一个Django app,Part 5:测试 2.7.1 自动化测试介绍 2.7.2 基本的测试策略 2.7.3 编写我们的第一个测试程序 2.7.4 测试一个视图 2.7.5 测试越 ...

  2. 11.python3标准库--使用进程、线程和协程提供并发性

    ''' python提供了一些复杂的工具用于管理使用进程和线程的并发操作. 通过应用这些计数,使用这些模块并发地运行作业的各个部分,即便是一些相当简单的程序也可以更快的运行 subprocess提供了 ...

  3. scrapy shell 用法(慢慢更新...)

    scrapy shell 命令 1.scrapy shell url #url指你所需要爬的网址 2.有些网址数据的爬取需要user-agent,scrapy shell中可以直接添加头文件, 第①种 ...

  4. ASP.NET Core 2.0 MVC 发布部署--------- linux安装.NET CORE SDK具体操作链接以及操作总细节

    具体链接:https://www.microsoft.com/net/learn/get-started/linuxubuntu 如下图:

  5. 通过复制(拷贝)新建javaweb项目报错无法访问

    我们有时候为了方便,用eclipse在原来动态web项目上直接复制,粘贴项目,来形成以一个新的项目.可是运行的时候,它显示的url地址,还是原来的项目地址.初步判定,有可能是eclipse配置的问题. ...

  6. Webmin忘记密码解决方法,及配置文件介绍

    Webmin忘记Web登陆时候的密码,无法登陆了,Google了一下,基本方法是通过changepass.pl可以修改密码 首先找到changepass.pl这个文件目录 $sudo locate c ...

  7. 在eclipse中使用Maven3(笔记二)

    笔记本二   在Eclipse 中使用Maven 第一节:m2eclipse 插件安装 打开Eclipse,点击菜单Help - > Install New Software 点击Add 按钮N ...

  8. linq to sql: 在Entityfamework Core中使用多个DbContext

    最近在学习DotNetCore并做一个自己的小项目,分为了多个数据库,AccountDbContext和BlogDbContext, 发blog的时候需要用到Account的信息,但是再Blog中只记 ...

  9. git的使用总结【干货·转载】

    源文地址:https://juejin.im/post/5a54386af265da3e3b7a6317 摘抄: 版本树 / graph / network 干净简洁清晰 提交信息明确 易维护易读 举 ...

  10. Mono for Android 学习一 环境的搭建

    JAVA SDK和Android SDK下载安装 1.疑问:用mono for android 开发为什么必须要java sdk的支持 答:因为android  sdk是java开发的,所以和它相关的 ...