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

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<=N<=200), 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 recommended. 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 recommended 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<stdio.h>
#include<map>
#include<string>
#include<string.h>
#include<stack>
using namespace std;
#define MAX 210
int INF = ;
int HappyVal[];
int visit[MAX];
int Grap[MAX][MAX];
int d[MAX];
int h[MAX];
int num[MAX];
int pre[MAX];
int Count[MAX]; void Dijkstra(int Begin,int NodeNum)
{
d[Begin] = ;
h[Begin] = HappyVal[Begin];
num[Begin] = ;
Count[Begin] = ;
for(int i = ;i < NodeNum ;i++)
{
int index = -;
int MIN = INF;
for(int j = ;j <NodeNum ;j++)
{
if(!visit[j] && d[j] < MIN)
{
index = j;
MIN = d[j];
}
} if(index == -) return ;
visit[index] = true;
for(int v = ;v <NodeNum ;v++)
{
if(!visit[v] && Grap[index][v]!=INF)
{
if(d[index]+Grap[index][v]<d[v])
{
d[v] = d[index]+Grap[index][v];
num[v] = num[index];
h[v] = h[index] + HappyVal[v];
pre[v] = index;
Count[v] = Count[index] +;
}
else if(d[index]+Grap[index][v]==d[v])
{
num[v] = num[v] + num[index]; if(h[v] < h[index] + HappyVal[v])
{
h[v] = h[index] + HappyVal[v];
Count[v] = Count[index] +;
pre[v] = index;
}
else if( h[v] == h[index] + HappyVal[v] && (double)(h[index] + HappyVal[v])/(Count[index]+) > (double)h[v]/Count[v])
{
Count[v] = Count[index] +;
pre[v] = index;
}
}
}
}
} } int main()
{
int i,j,N,K,happy,ROM;
char Begin[],tem[];
scanf("%d%d%s",&N,&K,Begin);
map<string,int> mm;
map<int,string> mm2;
mm[Begin] = ;
mm2[] = Begin ;
HappyVal[mm[Begin]] = ;
for(i = ; i < N ;i++)
{
scanf("%s%d",tem,&happy);
if(strcmp("ROM",tem)==) ROM = i;
mm[tem] = i;
mm2[i] = tem;
HappyVal[i] = happy;
} char x[],y[]; for(i = ; i < N ;i++)
{
for(j = ; j < N ;j++)
{
Grap[i][j] = INF;
}
d[i] = h[i] = INF;
pre[i] = -;
Count[i] = ;
} for(i = ; i < K ;i++)
{
scanf("%s%s",x,y);
scanf("%d",&Grap[mm[x]][mm[y]]);
Grap[mm[y]][mm[x]] = Grap[mm[x]][mm[y]];
} Dijkstra( mm[Begin] , N); printf("%d %d %d %d\n",num[mm["ROM"]],d[mm["ROM"]],h[mm["ROM"]],h[mm["ROM"]]/Count[mm["ROM"]]); stack<int> ss;
i= mm["ROM"];
while(i != -)
{
ss.push(i);
i = pre[i];
}
int fir = ;
while(!ss.empty())
{
if(fir == )
{
fir = ;
printf("%s",mm2[ss.top()].c_str());
}
else printf("->%s",mm2[ss.top()].c_str());
ss.pop();
} printf("\n"); return ;
}

1087. All Roads Lead to Rome (30)的更多相关文章

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

  2. 1087 All Roads Lead to Rome (30)(30 分)

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

  3. PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra

    题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...

  4. PAT (Advanced Level) 1087. All Roads Lead to Rome (30)

    暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  5. 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)

    题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...

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

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

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

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

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

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

随机推荐

  1. 高德地图 JavaScript API 开发系列教程(一)

    高德地图 API 提供包括 Web API 即 JavaScript API,Android API,定位API,IOS API,WP API,Win8 API等,本系列教程主要针对高德 JavaSc ...

  2. android一些基础知识

    android应用基于JAVA, 支持SQL,由于底层是LINUX,所以支持C/C++ 目前有两种编程:基于ADT的JAVA编程,基于NDK的C编程 Android编程环境需要哪些:官方推荐用JDK+ ...

  3. android开发之路09(浅谈SQLite数据库01)

    1.SQLite数据库: SQLite 是一个开源的嵌入式关系数据库,实现自包容.零配置.支持事务的SQL数据库引擎. 其特点是高度便携.使 用方便.结构紧凑.高效.可靠. 与其他数据库管理系统不同, ...

  4. 一次配置jdk环境变量的感悟

    开发java也一年多了,昨日一次偶然的机会,想在dos命令下执行一个程序,发现在 命令行输入 javac Test.java的时候,竟然提示javac不是内部命令, 之后输入 java ,也提示不是内 ...

  5. Sublime Text 插件之常用20个插件

    作为一个开发者你不可能没听说过 Sublime Text.不过你没听说过也没关系,下面让你明白. Sublime Text是一款非常精巧的文本编辑器,适合编写代码.做笔记.写文章.它用户界面十分整洁, ...

  6. MyBatis(3.2.3) - Handling the CLOB/BLOB types

    MyBatis provides built-in support for mapping CLOB/BLOB type columns. Assume we have the following t ...

  7. HDOJ2004成绩转换

    成绩转换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  8. Activity的启动模式(android:launchMode)

    在android里,有4种activity的启动模式,分别为: “standard” (默认) “singleTop” “singleTask” “singleInstance” 它们主要有如下不同: ...

  9. 设计模式——设计模式之禅day2

    接口隔离原则 接口分为两种: ● 实例接口( Object Interface) , 在Java中声明一个类, 然后用new关键字产生一个实例, 它是对一个类型的事物的描述, 这是一种接口. 比如你定 ...

  10. 删除SVN批处理(bat)

    添加批处理文件(deleteSVN.bat),文件内容: @echo off :start ::启动过程,切换目录 set pwd=%cd% cd %1 echo 工作目录是:& chdir ...