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. kendo Grid Unexpected number错误

    这种用了自定义function来代替默认的就需要把所有的created,update等都替换掉,不能只替换部分

  2. leetcode第一刷_Minimum Window Substring

    好题.字符串.线性时间. 我认为第一次拿到这个题的人应该不会知道该怎么做吧,要么就是我太弱了..先搞清楚这个题要求的是什么.从一个长字符串中找一个字串,这个字串中的字符全然包括了另一个给定目标串中的字 ...

  3. Android 四大组件学习之ContentProvider五

    上几节学习了ContentProvider的实际用途,读取短信.插入短信,读取联系人.插入联系人等. 本节课在学习ContentProvider的观察者. 在生活中有第三方的软件.比方什么短信软件.此 ...

  4. 学习笔记——SQL SERVER2014内存数据库

    sql server2014支持内存数据库功能. 内存可以说是数据库性能的生命线.理论上,如果内存足够,SQL SERVER可以将所有的数据都装载到内存里,访问.修改什么的,都在内存中进行,只有在ch ...

  5. null in JavaScript

    C# String.IsNullOrEmpty Javascript equivalent https://stackoverflow.com/questions/5746947/c-sharp-st ...

  6. springboot 异常: Requested bean is currently in creation: Is there an unresolvable circular reference?

    2018-07-31 11:56:18.812 WARN 10316 --- [ main] ConfigServletWebServerApplicationContext : Exception ...

  7. poj3926

    dp+优化 很明显可以用单调队列优化. 记录下自己犯的sb错误:  数组开小,sum没搞清... #include<cstdio> #include<cstring> usin ...

  8. The Moronic Cowmpouter(负进位制转换)

    http://poj.org/problem?id=3191 题意:将一个整型的十进制整数转化为-2进制整数. #include <stdio.h> #include <algori ...

  9. Python 33(2)进程理论

    一:什么是进程         进程指的是一个正在进行 / 运行的程序,进程是用来描述程序执行过程的虚拟概念 进程vs程序 程序:一堆代码 进程:程序的执行的过程 进程的概念起源于操作系统,进程是操作 ...

  10. ASP.NET Core 多环境

    ASP.NET Core 支持在多个环境中管理应用程序,如开发(Development),预演(Staging)和生产(Production).环境变量用来指示应用程序正在运行的环境,允许应用程序适当 ...