暴力DFS。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std; const int maxn=+;
int n,m;
string st;
map<string ,int>m1;
map<int ,string>m2;
int sz;
int h[maxn];
struct Edge
{
int u,v,c;
}e[maxn*maxn];
int tot;
vector<int>g[maxn]; int des;
int ans_cost=0x7FFFFFFF;
int ans_count=;
int ans_happy=;
int ans_point=;
int path[maxn],ans_path[maxn];
bool flag[maxn]; void dfs(int x,int cost,int happy,int point)
{
if(cost>ans_cost) return;
if(x==des)
{
if(cost<ans_cost)
{
ans_cost=cost;
ans_count=;
ans_happy=happy;
ans_point=point;
for(int i=;i<point;i++)
ans_path[i]=path[i];
} else if(cost==ans_cost)
{
ans_count++;
if(happy>ans_happy)
{
ans_happy=happy;
ans_point=point;
for(int i=;i<point;i++)
ans_path[i]=path[i];
} else if(happy==ans_happy)
{
if(point<ans_point)
{
ans_point=point;
for(int i=;i<point;i++)
ans_path[i]=path[i];
}
}
}
return;
} for(int i=;i<g[x].size();i++)
{
int id=g[x][i];
path[point]=e[id].v;
if(flag[e[id].v]==) continue;
flag[e[id].v]=;
dfs(e[id].v,cost+e[id].c,happy+h[e[id].v],point+);
flag[e[id].v]=;
}
} int main()
{
scanf("%d%d",&n,&m); cin>>st;
m1[st]=++sz; m2[sz]=st; for(int i=;i<=n-;i++)
{
string name; cin>>name;
m1[name]=++sz; m2[sz]=name;
int val; scanf("%d",&val);
h[sz]=val;
} des=m1["ROM"]; tot=;
for(int i=;i<=m;i++)
{
string U,V; int c; cin>>U>>V>>c;
e[tot++].u=m1[U]; e[tot].v=m1[V]; e[tot].c=c;
g[m1[U]].push_back(tot); e[tot++].u=m1[V]; e[tot].v=m1[U]; e[tot].c=c;
g[m1[V]].push_back(tot);
} memset(flag,,sizeof flag);
flag[]=;
dfs(,,,); printf("%d %d %d %d\n",ans_count,ans_cost,ans_happy,ans_happy/ans_point); cout<<st;
for(int i=;i<ans_point;i++)
cout<<"->"<<m2[ans_path[i]];
printf("\n"); return ;
}

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

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

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

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

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

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

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

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

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

  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. 关于回顾css发现的一些问题

    1.针对于before和after伪元素的用法: <style> .clearfix:before, .clearfix:after{ clear:both; content:" ...

  2. 电脑上文件的后缀名被隐藏,把一个文本文件改成.bat时,默认打开的还是文本。

    1.打开文件夹,选择组织,点击“文件夹和搜索选项”,如图: 2.选择“查看”,找到“隐藏已知文件类型的扩展名”,不要勾选这一项,如图: 3.点击“确定”或者“应用”

  3. dubbo 多连接,多线程池.

    1. consumer 多连接 Dubbo protocol options: <dubbo:protocolname=“dubbo” port=“9090” server=“netty” cl ...

  4. 基于IMD的包过滤防火墙原理与实现

    一.前言二.IMD中间层技术介绍三.passthru例程分析四.部分演示代码五.驱动编译与安装六. 总结 一.前言 前段时间,在安全焦点上看到了TOo2y朋友写的<基于SPI的数据报过滤原理与实 ...

  5. 628. Maximum Product of Three Numbers@python

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  6. IntelliJ IDEA 中自定义模板代码的缩写

    方法一:新建 Live Template step1. 点击 File – Setting step2.选择 Live Template,点击右侧的+号,选择 Template Group step3 ...

  7. (42)zabbix使用IT services 了解服务器SLA整体情况

    什么是IT Services 服务器或者某项服务.业务的可用率,不懂技术的上级领导会过问最近服务器可用率如何.所有api的状况怎么样? 通常一些技术人员会说负载怎么样,哪些cpu使用率怎么样,硬盘使用 ...

  8. 常用c++函数

    strrev(str)  (str为字符串)倒序输出字符串 floor(x),有时候也写做Floor(x),其功能是“向下取整”,或者说“向下舍入”,即取不大于x的最大整数(与“四舍五入”不同,下取整 ...

  9. Lex与Yacc学习(八)之变量和有类型的标记(扩展计算器)

    变量和有类型的标记 下一步扩展计算器来处理具有单个字母名字的变量,因为只有26个字母 (目前只关心小写字母),所以我们能在26个条目的数组(称它为vbltable)中存储变量. 为了使得计算器更加有用 ...

  10. Errors occurred during the build. Errors running builder 'JavaScript Validator'

    选择一个项目--右键Properties--Builders--取消第2个"JavaScript Validator"的勾就OK了.