PAT (Advanced Level) 1087. All Roads Lead to Rome (30)
暴力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)的更多相关文章
- 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)
题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...
- PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra
题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...
- [图的遍历&多标准] 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 ...
- 1087. All Roads Lead to Rome (30)
时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different ...
- 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 ...
- 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 ...
- 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
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 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
随机推荐
- python基本排序算法(一)
一.冒泡排序 这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端(升序或降序排列),就如同碳酸饮料中二氧化碳的气泡最终会上浮到顶端一样,故名“冒泡排序”. 冒泡排序算法的原理如下: 比 ...
- Xcode 6 创建 Objective-C category
1. Command + N 2. 选择 iOS - Source - Objective-C File 3.File Type 选择 Category,Class 填基于的类名,File填扩展的名
- python多进程与多线程编程
进程(process)和线程(thread)是非常抽象的概念.多线程与多进程编程对于代码的并发执行,提升代码运行效率和缩短运行时间至关重要.下面介绍一下python的multiprocess和thre ...
- (39)zabbix snmp自定义OID nginx监控实例
为什么要自定义OID? 前面的文章已经讲过zabbix如何使用snmp监控服务器,但是他有一个很明显的局限性:只能监控定义好的OID项目 假如我们想知道nginx进程是否在运行?在没有zabbix a ...
- dom4j 常用操作
package com.wanbang.wbyyb.common.util; import com.alibaba.fastjson.JSONObject; import com.wanbang.wb ...
- Linux菜鸟起飞之路【二】Linux基本常识
一.Unix操作系统基本常识 1.什么是Unix? Unix是一个计算机操作系统,是一个用来协调.管理和控制计算机硬件与软件资源的控制程序. 2.Unix操作系统的特点? 多用户与多任务.多用户表示在 ...
- 前端,字体图标,盒子显隐,2d形变,盒子阴影
---恢复内容开始--- 字体图标 1.将font-awesome-4.7.0文件夹放入项目内 2.在html head中连接 3.在body中导入 盒子显隐 1.使用高度显隐 <p>-- ...
- as eclipse 对比
eclipse目录结构 src:存放prj源码 gen:自动生成文件,R类存放资源文件的唯一id android+版本:项目jar包 assets:res存放资源文件,不会被gen生成资源id,同过a ...
- js-dom-EventUtil
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【03】Chrome提示印象笔记剪藏插件"已停用不支持的扩展程序"怎么办?
[03] Chrome提示印象笔记剪藏插件"已停用不支持的扩展程序"怎么办? 刚好也遇上了这个问题,百度了一下,以下是解决方法,亲测可行: 1.首先把需要安装的第三方插件,后缀.c ...