Source:

PAT A1087 All Roads Lead to Rome (30 分)

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

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

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

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

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

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

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

  5. PAT1087. All Roads Lead to Rome

    PAT1087. All Roads Lead to Rome 题目大意 给定一个图的边权和点权, 求边权最小的路径; 若边权相同, 求点权最大; 若点权相同, 则求平均点权最大. 思路 先通过 Di ...

  6. pat1087. All Roads Lead to Rome (30)

    1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

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

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

  8. 1087. All Roads Lead to Rome (30)

    时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different ...

  9. A1087. All Roads Lead to Rome

    Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...

随机推荐

  1. hibernate使用c3p0数据源

    在配置好hibernate连接数据库环境的前提下,我们进行例如以下操作就能够搭建好hibernate中使用c3p0数据源的环境了. 1). 导入 jar 包: hibernate-release-4. ...

  2. HDU 3681

    也算难题,难在如何处理有些点可以无限次经过 问题. 这道题,其实很容易想到二分+TSP的状态压缩,但在处理上述问题时,确实没想到.题解是处理每一个Y或G或F点到其他YGF点的距离,BFS,这样就出现一 ...

  3. NGINX之——配置HTTPS加密反向代理訪问–自签CA

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46695495 出于公司内部訪问考虑,採用的CA是本机Openssl自签名生成的,因 ...

  4. MFC 小知识总结四

    1 PlaySound  播放WAV格式的音乐 This function plays a sound specified by a file name, resource, or system ev ...

  5. 【Geforce】关于如何在Geforce Experience中登录

    相信不少人无法登录这个该死的Geforce Experience.这里提供几个解决方案: 1.在“服务”中启动运行 NVIDIA NetworkService Container 方式改为手动或者自动 ...

  6. NYOJ skiing(DFS+记忆化搜索)

    skiing                                                                        时间限制:3000 ms  |  内存限制: ...

  7. bzoj1231 [Usaco2008 Nov]mixup2 混乱的奶牛——状压DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1231 小型状压DP: f[i][j] 表示状态为 j ,最后一个奶牛是 i 的方案数: 所以 ...

  8. awk 去重的同时并保持原来的顺序

    #-----------awk.awk------------ {    if(data[$0]++ == 0)        lines[++count] = $0} END {    for (i ...

  9. 99.ext afteredit事件详解

    转自:http://www.jcodecraeer.com/a/jquery_js_ajaxjishu/2012/0524/203.html 1 摘要 grid.on(afteredit,afterE ...

  10. 8.19noip模拟题

      2017 8.19 NOIP模拟赛 by coolyangzc 共3道题目,时间3小时 题目名 高级打字机 不等数列 经营与开发 源文件 type.cpp/c/pas num.cpp/c/pas ...