PAT_A1087#All Roads Lead to Rome
Source:
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 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 Klines 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 alwaysROM
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
Keys:
Attention:
- 结点编号从1开始,因为映射判定的条件是结点编号==0
Code:
- /*
- Data: 2019-08-27 19:12:20
- Problem: PAT_A1087#All Roads Lead to Rome
- AC: 37:38
- 题目大意:
- 从起点至终点ROM花费最少且最幸福的一条路,
- 若不唯一,给出平均幸福指数最高的路径
- 输入:
- 第一行给出,城市数N,路径数K,起点城市
- 接下来N-1行,城市名,幸福指数
- 接下来K行,v1,v2,cost
- 输出:
- 最短路径数目,花费,幸福指数,平均幸福指数(整数部分,除去起点)
- 起点至终点的路径
- */
- #include<cstdio>
- #include<string>
- #include<vector>
- #include<map>
- #include<iostream>
- #include<algorithm>
- using namespace std;
- const int M=1e3,INF=1e9;
- int n,m,v1,v2,t,grap[M][M],d[M],w[M],vis[M],pt=,optH=,cnt=;
- vector<int> pre[M],path,optPath;
- map<string,int> mp;
- string city[M];
- int ToInt(string s)
- {
- if(mp[s] == )
- {
- city[pt]=s;
- mp[s]=pt++;
- }
- return mp[s];
- }
- void Dijskra(int s)
- {
- fill(vis,vis+M,);
- fill(d,d+M,INF);
- d[s]=;
- for(int i=; i<=n; i++)
- {
- int u=-,Min=INF;
- for(int j=; j<=n; j++)
- {
- if(vis[j]== && d[j]<Min)
- {
- u = j;
- Min=d[j];
- }
- }
- if(u==-)
- break;
- vis[u]=;
- for(int v=; v<=n; v++)
- {
- if(vis[v]== && grap[u][v]!=INF)
- {
- if(d[u]+grap[u][v]<d[v])
- {
- pre[v].clear();
- pre[v].push_back(u);
- d[v] = d[u]+grap[u][v];
- }
- else if(d[u]+grap[u][v]==d[v])
- pre[v].push_back(u);
- }
- }
- }
- }
- void DFS(int v)
- {
- if(v == )
- {
- cnt++;
- int happy=;
- for(int i=; i<path.size(); i++)
- happy+=w[path[i]];
- if(happy > optH)
- {
- optH = happy;
- optPath = path;
- }
- else if(happy==optH && path.size()<optPath.size())
- optPath = path;
- return;
- }
- path.push_back(v);
- for(int i=; i<pre[v].size(); i++)
- DFS(pre[v][i]);
- path.pop_back();
- }
- int main()
- {
- #ifdef ONLINE_JUDGE
- #else
- freopen("Test.txt", "r", stdin);
- #endif // ONLINE_JUDGE
- string s1,s2;
- cin >> n >> m >> s1;
- ToInt(s1);
- for(int i=; i<n; i++)
- {
- cin >> s1;
- v1 = ToInt(s1);
- scanf("%d", &w[v1]);
- if(s1 == "ROM")
- t = v1;
- }
- fill(grap[],grap[]+M*M,INF);
- for(int i=; i<m; i++)
- {
- cin >> s1 >> s2;
- v1 = ToInt(s1);
- v2 = ToInt(s2);
- scanf("%d", &grap[v1][v2]);
- grap[v2][v1]=grap[v1][v2];
- }
- Dijskra();
- DFS(t);
- printf("%d %d %d %d\n", cnt,d[t],optH,optH/optPath.size());
- printf("%s", city[].c_str());
- for(int i=optPath.size()-; i>=; i--)
- printf("->%s", city[optPath[i]].c_str());
- return ;
- }
PAT_A1087#All Roads Lead to Rome的更多相关文章
- 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 ...
- [图的遍历&多标准] 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
PAT 1087 All Roads Lead to Rome 题目: Indeed there are many different tourist routes from our city to ...
- PAT甲级1087. All Roads Lead to Rome
PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
- PAT1087. All Roads Lead to Rome
PAT1087. All Roads Lead to Rome 题目大意 给定一个图的边权和点权, 求边权最小的路径; 若边权相同, 求点权最大; 若点权相同, 则求平均点权最大. 思路 先通过 Di ...
- pat1087. All Roads Lead to Rome (30)
1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT 甲级 1087 All Roads Lead to Rome(SPFA+DP)
题目链接 All Roads Lead to Rome 题目大意:求符合题意(三关键字)的最短路.并且算出路程最短的路径有几条. 思路:求最短路并不难,SPFA即可,关键是求总路程最短的路径条数. 我 ...
- 1087. All Roads Lead to Rome (30)
时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different ...
- A1087. All Roads Lead to Rome
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...
随机推荐
- github在README中展示demo
2017年更新,现在github再master分支直接就可以操作,所以我改了这篇文章 问题所在? 解决办法? 博主建议? 一:问题的所在 相信很多小胖友们在把自己的网页上传到github仓库中,都 ...
- cogs 259. 亲戚
259. 亲戚 ★ 输入文件:relations.in 输出文件:relations.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 或许你并不知道,你 ...
- servlet和Spring的DispatcherServlet详解
Servlet是什么 1. Servlet是服务器端运行的一个程序,是一个被编译好的Java类.它不是框架等. 2. Web容器的启动,需要依赖Servlet.当web服务器开始执行时,servlet ...
- 输入法InputConnection
/** * The InputConnection interface is the communication channel from an * {@link InputMethod} bac ...
- PHP array_flip()
定义和用法 array_flip() 函数返回一个反转后的数组,如果同一值出现了多次,则最后一个键名将作为它的值,所有其他的键名都将丢失. 如果原数组中的值的数据类型不是字符串或整数,函数将报错. 语 ...
- C语言程序创建文件
#include <stdio.h>#include <stdlib.h>int main() { FILE *fp;if((fp=fopen("g:\\a.txt& ...
- PCB 周期日历
在PCB行业一直以来没找到适合我们这行业的日历,主要存在2个差异导致. 1.周期差异:由于PCB 周期计算的复杂性,市面上无法找到符合PCB行业计算周期方式 (另一遍博文中有写周期计算逻辑) http ...
- 【BZOJ4590】自动刷题机
[思路分析] 比赛的时候想到了用二分+贪心,二分的部分与贪心的部分也写对了,但是由于数据范围未看没有开long long,且二分左端点赋值过小导致WA掉 正解:二分+贪心 二分代码的长度,贪心判断能否 ...
- C#Cookie操作类,删除Cookie,给Cookie赋值
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security ...
- 解决Latex复制到公众号可能报“图片粘贴失败”的问题
前几天出了个版本,还发了篇“Md2All,让公众号完美显示Latex数学公式”的文章,发完后,心里还是不太爽的,因为那个版本还是遗留了一个问题:当把Latex公式转换为本地图片,再复制到公众号时,有可 ...