https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984

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), 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

代码:

#include <bits/stdc++.h>
using namespace std; #define inf 0x3f3f3f3f
int N, K;
string st;
map<string, int> mp, cost;
map<int, string> pos;
int maze[220][220], vis[220], dis[220], see[220];
int MinStep = 0, cnt = 0;
vector<vector<int> > ans;
vector<int> v; struct Node{
double ave;
int all = 0;
int num;
int n;
}node[100010]; bool cmp(const Node &a, const Node &b) {
if(a.all != b.all) return a.all > b.all;
return a.ave > b.ave;
} void dfs(int act, int step) {
if(step > MinStep) return; if(act == mp["ROM"]) {
cnt ++;
ans.push_back(v);
return;
} for(int i = 1; i <= N; i ++) {
if(maze[act][i] != inf && dis[act] + maze[act][i] == dis[i] && see[i] == 0) {
v.push_back(i);
see[i] = 1;
dfs(i, step + maze[act][i]);
v.pop_back();
see[i] = 0;
}
}
} void dijkstra(int act) {
dis[act] = 0;
int temp = act; for(int i = 1; i <= N; i ++) {
int minn = inf;
for(int j = 1; j <= N; j ++) {
if(dis[j] < minn && vis[j] == 0) {
minn = dis[j];
temp = j;
}
}
vis[temp] = 1;
for(int k = 1; k <= N; k ++) {
if(vis[k] == 0)
dis[k] = min(dis[k], maze[temp][k] + dis[temp]);
}
}
} int main() {
memset(dis, inf, sizeof(dis));
memset(vis, 0, sizeof(vis));
memset(maze, inf, sizeof(maze));
memset(see, 0, sizeof(see));
scanf("%d%d", &N, &K);
cin >> st;
mp[st] = 1;
pos[1] = st;
for(int i = 0; i < N - 1; i ++) {
string city; int val;
cin >> city >> val;
mp[city] = i + 2;
pos[i + 2] = city;
cost[city] = val;
}
for(int i = 0; i < K; i ++) {
string stt, en; int cos;
cin >> stt >> en >> cos;
maze[mp[stt]][mp[en]] = maze[mp[en]][mp[stt]] = cos;
}
dijkstra(1);
MinStep = dis[mp["ROM"]]; dfs(1, 0); for(int i = 0; i < ans.size(); i ++) {
node[i].num = i;
node[i].n = ans[i].size();
for(int j = 0; j < ans[i].size(); j ++) {
node[i].all += cost[pos[ans[i][j]]];
}
node[i].ave = 1.0 * node[i].all / node[i].n;
} sort(node, node + ans.size(), cmp);
printf("%d %d %d %d\n", cnt, MinStep, node[0].all, (int)node[0].ave);
cout << st;
for(int i = 0; i < ans[node[0].num].size(); i ++) {
cout << "->";
cout << pos[ans[node[0].num][i]];
}
return 0;
}

  渐渐开始不喜欢最短路

PAT 甲级 1087 All Roads Lead to Rome的更多相关文章

  1. PAT甲级1087. All Roads Lead to Rome

    PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...

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

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

  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. 有crontab中的脚本不执行,需要在脚本里面export各种环境变量

    [oracle@sta ~]$ vi .bash_profile # .bash_profile # Get the aliases and functionsif [ -f ~/.bashrc ]; ...

  2. 【转】mysql交互式连接&非交互式连接

    交互式操作:通俗的说,就是你在你的本机上打开mysql的客户端,就是那个黑窗口,在黑窗口下进行各种sql操作,当然走的肯定是tcp协议. 非交互式操作:就是你在你的项目中进行程序调用.比如一边是tom ...

  3. velocity模板实战

    场景:json配置报文转换遇到的问题:1.json报文转换成map,多节点如何处理?数组如何处理? 2.velocity模板处理数组 3.应用之间rabbitmq通讯map反序列化,数组报错?知识点: ...

  4. Android 网络请求超时处理方案

    以用户登录为例介绍用户访问网络时的请求超时处理的两种方法: 1)使用android提供的工具类AsyncTask类,此类提供了一个AsyncTask.execute().get(timeout, un ...

  5. (转)LVS+Keepalived使用总结 vip丢失

    /sbin/ifconfig lo: $vip broadcast $vip netmask 255.255.255.255 up /sbin/ifconfig lo: 172.16.254.63 b ...

  6. MSF里MS17_010利用模块笔记

    1.   auxiliary/scanner/smb/smb_ms17_010      //扫描检测漏洞辅助模块 扫描结果这里可以看到 2,3,4这几台主机存在此漏洞! 2.     auxilia ...

  7. Sqlite 快速批量插入数据 测试

    public static int insertDbBatch() { string sql = ""; SQLiteConnection conn = new SQLiteCon ...

  8. 20155233 《网络对抗》Exp2 后门原理与实践

    实验过程 1.Win获得Linux Shell windows:使用ipconfig指令查看本机ip: windows:使用ncat命令打开监听: Linux反弹连接win: 输入 nc window ...

  9. [Oracle]如何为数据库设置Event(eg: ORA-00235)

    [Oracle]如何为数据库设置Event(eg: ORA-00235) ■ When you use SPFILE, Setting procedure: 1. Check the current ...

  10. Codeforces 954D Fight Against Traffic(BFS 最短路)

    题目链接:Fight Against Traffic 题意:有n个点个m条双向边,现在给出两个点S和T并要增加一条边,问增加一条边且S和T之间距离不变短的情况有几种? 题解:首先dfs求一下S到其他点 ...