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. 一个爬取https和http通用的工具类(JDK自带的URL的用法)

    今天在java爬取天猫的时候因为ssl报错,所以从网上找了一个可以爬取https和http通用的工具类.但是有的时候此工具类爬到的数据不全,此处不得不说python爬虫很厉害. package cn. ...

  2. 阿里分布式开源框架DUBBO 入门+ 进阶+ 项目实战视频教程

    史诗级Java/JavaWeb学习资源免费分享 欢迎关注我的微信公众号:"Java面试通关手册"(坚持原创,分享各种Java学习资源,面试题,优质文章,以及企业级Java实战项目回 ...

  3. PHY Linux 驱动

    以太网 MAC(链路层)+PHY(物理层/RTL8201F,88E1111);集成型DM9000,RTL8139CP 由于网络数据传输量较大,不论是分开型还是集成型,通常会在MAC和PHY之间引入DM ...

  4. PXC加入新节点避免SST时grastate.dat文件内容的修改问题

    PXC加入新节点避免SST时grastate.dat文件内容的修改问题 在主从同步完成并关闭实例后,需要修改grastate.dat中的seqno:到底应该填已经执行过最后的XID号(Executed ...

  5. STL中heap相关函数

    heap并不是属于STL中的containers,而是在<algorithm>下提供了相关的函数 make_heap,sort_heap,pop_heap,push_heap 函数的说明: ...

  6. jplayer.js 与 video.js

    HTML5 - 两款基于JS的视频播放器 都是基于h5 video 标签,如果不支持则会自动转成flash,这里个人比较推荐使用jplayer; 1.video.js pc端有时候会与视频打交道,如果 ...

  7. oracle客户端不需要配置tnsnames.ora文件直接连接服务器数据库

    在以前的oracle使用过程中,想要在客户端连接到服务器时,都是在客户端中的tnsnames.ora文件配置如以下内容: adb = (DESCRIPTION = (ADDRESS_LIST = (A ...

  8. js固定小数位数 .toFixed()

    toFixed(num)法可把 Number 四舍五入为指定小数位数的数字. num为需要固定的位数 var num=2;console.log(num.toFixed(2));//2.00;var ...

  9. CPU运行时间——time

    用途说明time命令常用于测量一个命令的运行时间,注意不是用来显示和修改系统时间的(这是date命令干的事情).但是今天我通过查看time命令的手册页,发现它能做的不仅仅是测量运行时间,还可以测量内存 ...

  10. FreeMarker使用之比较if

    1. =或者==:判断两个值是否相等. 2. !=:判断两个值是否不等. 3. >或者gt:判断左边值是否大于右边值 4. >=或者gte:判断左边值是否大于等于右边值 5. <或者 ...