PAT1087. All Roads Lead to Rome

题目大意

给定一个图的边权和点权, 求边权最小的路径; 若边权相同, 求点权最大; 若点权相同, 则求平均点权最大.

思路

先通过 Dijkstra 求得最短路径, 需要注意的是: 要保证每次松弛时 u 和 v 不相同, 否则会形成自环, 则从 ROM 开始 BFS 遍历每一条边权相同的路径.

代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
using namespace std;
#define MAXN 300
#define INF 0x7ffffff
int nVertex, nEdge;
map<string, int> s_i;
map<int, string> i_s;
vector<int> prepath[MAXN], temppath, anspath;
int vw[MAXN];
int ew[MAXN][MAXN];
int dis[MAXN];
int isVis[MAXN];
int cntRo = 0;
int ansHapy = 0;
double ansAvg = 0;
void dfs(int loc){
temppath.push_back(loc);
if(loc == 0){
int hapy = 0;
for(int i = 0; i < temppath.size(); i++)
hapy += vw[temppath[i]];
double avgHapy = hapy * 1.0 / (temppath.size() - 1);
if(hapy > ansHapy){
ansHapy = hapy;
ansAvg = avgHapy;
anspath = temppath;
}
else if(hapy == ansHapy && avgHapy > ansAvg){
ansAvg = avgHapy;
anspath = temppath;
}
cntRo++;
temppath.pop_back();
return;
}
for(int i = 0; i < prepath[loc].size(); i++){
dfs(prepath[loc][i]);
}
temppath.pop_back();
}
int main(){
scanf("%d%d", &nVertex, &nEdge);
string tempStr; cin >> tempStr;
s_i[tempStr] = 0; i_s[0] = tempStr;
for(int i = 1; i < nVertex; i++){
cin >> tempStr;
s_i[tempStr] = i; i_s[i] = tempStr;
scanf("%d", &vw[i]);
}
for(int i = 0; i < MAXN; i++){
for(int j = 0; j < MAXN; j++){
ew[i][j] = (i == j ? 0 : INF);
}
}
for(int i = 0; i < nEdge; i++){
string a, b; int c;
cin >> a >> b >> c;
ew[s_i[a]][s_i[b]] = ew[s_i[b]][s_i[a]] = c;
} for(int i = 0; i < nVertex; i++)
dis[i] = ew[0][i];
dis[0] = 0;
for(int i = 0; i < nVertex; i++){
int u = -1, minn = INF;
for(int j = 0; j < nVertex; j++){
if(!isVis[j] && dis[j] < minn){
minn = dis[j];
u = j;
}
}
isVis[u] = 1;
for(int v = 0; v < nVertex; v++){
if(u != v)
{
if(dis[v] > ew[u][v] + dis[u]){
dis[v] = ew[u][v] + dis[u];
prepath[v].clear();
prepath[v].push_back(u);
}
else if(dis[v] == ew[u][v] + dis[u]){
prepath[v].push_back(u);
}
}
}
}
int rom = s_i["ROM"];
dfs(rom);
printf("%d %d %d %d\n", cntRo, dis[rom], ansHapy, (int)ansAvg);
for(int i = anspath.size() - 1; i != 0; i--){
cout << i_s[anspath[i]] << "->";
}
printf("ROM");
return 0;
}

PAT1087. All Roads Lead to Rome的更多相关文章

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

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

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

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

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

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

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

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

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

  7. PAT_A1087#All Roads Lead to Rome

    Source: PAT A1087 All Roads Lead to Rome (30 分) Description: Indeed there are many different tourist ...

  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. [Modelsim] 仿真的基本操作

    切换路径,建立库并编译所有源文件之后, 键入命令: vopt +acc topmodulename -o top vsim top 其中topmodulename是顶层模块的名称.

  2. zstu 4212 ——String Game ——————【字符串处理】

    4212: String Game Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 337  Solved: 41 Description Alice a ...

  3. 深入理解Javascript封装DOMContentLoaded事件

    最近在写一个Javascript的框架,刚把DOMContentLoaded事件封装好,略带小兴奋,把开发过程中遇到的原理和兼容性问题做篇笔记,省的忘记到处找. 我们在写js代码的时候,一般都会添加w ...

  4. Spring JdbcTemplate 使用总结

    1.查询Object public Classify queryClassifById(int id){ String sql="select * from t_classify where ...

  5. vue的一些特殊特性

    一.使用$ref特性获取DOM元素 代码示例如下所示: <!DOCTYPE html> <html lang="en"> <head> < ...

  6. js实现手风琴效果

    之前在慕课网上有练习手风琴效果,但是老师使用jquery简简单单的两三行实现了,今天自己用js练习一下效果 <div id="divbox"> <ul> & ...

  7. CSS样式编写案例

    1.制作如图三角形效果: 步骤一:将右侧盒子设置为相对定位 步骤二:在右侧盒子里面新建个子盒子,设置宽高相等,为正方形,绝对定位 步骤三:将绝对定位的盒子用CSS3旋转属性旋转 2.制定如图的序列号 ...

  8. JMeter 配置元件之-HTTP Cookie管理器-实现 Cookie 登录

    下面以购物App常用的收藏功能为例,讲述如何在 JMeter 中使用 cookie 进行登录态操作.具体的操作步骤如下所示: 第一步.录制查看收藏列表的 HTTP请求: 录制查看收藏列表的HTTP请求 ...

  9. Fiddler-修改HTTP请求参数

    在进行 App 测试时,经常需要修改请求参数,以获得不同的显示效果,以查看相应的页面显示处理.例如:可以通过修改 HTTP请求 的参数,来获取不同的响应结果. 下面以修改 HTTP请求的商品系统编号为 ...

  10. 虚拟机配置静态 IP 以后无法连接的解决办法

    问题描述 将虚拟机内部 IP 地址从动态获取改成静态 IP 以后,远程连接失败. 问题分析 Azure 虚拟机的内部 IP 默认为动态分配, 由 DHCP 服务自动分配, 在虚拟机的生命周期内, 该 ...