PAT1087. All Roads Lead to Rome
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的更多相关文章
- 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 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
- 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 ...
- [图的遍历&多标准] 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 ...
- 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(SPFA+DP)
题目链接 All Roads Lead to Rome 题目大意:求符合题意(三关键字)的最短路.并且算出路程最短的路径有几条. 思路:求最短路并不难,SPFA即可,关键是求总路程最短的路径条数. 我 ...
- PAT_A1087#All Roads Lead to Rome
Source: PAT A1087 All Roads Lead to Rome (30 分) Description: Indeed there are many different tourist ...
- 1087. All Roads Lead to Rome (30)
时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different ...
- A1087. All Roads Lead to Rome
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...
随机推荐
- TOJ 2452 Ultra-QuickSort
描述 In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a se ...
- 小型Basic编译器问题
# include <stdio.h> # include <string.h> # include <ctype.h> # include <stdlib. ...
- java url生成二维码保存到本地
http://blog.sina.com.cn/s/blog_5a6efa330102v1lb.html http://blog.csdn.net/about58238/article/details ...
- HDU 1257——最少拦截系统——————【LIS变型题】
最少拦截系统 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- 白话SpringCloud | 第九章:路由网关(Zuul)的使用
前言 介绍完分布式配置中心,结合前面的文章.我们已经有了一个微服务的框架了,可以对外提供api接口服务了.但现在试想一下,在微服务框架中,每个对外服务都是独立部署的,对外的api或者服务地址都不是不尽 ...
- CSS3 -webkit-transform(元素变换)
CSS3 -webkit-transform(元素变换) -webkit-transform:none | 类型 类型:scale:缩放,1为原始大小.scale(x).正数放大,负数缩小.属性值 ...
- 前端测试框架 puppeteer 文档翻译
puppeteer puppeteer 是一个通过DevTools 协议提供高级API 来控制 chrome,chromium 的 NODE库; puppeteer默认运行在 headless 模式, ...
- J2EE课程设计:在线书店管理系统
1.系统实现 使用SpringMVC框架进行开发 使用Maven进行系统构建 使用MySql数据库 项目只实现了查看图书,搜索图书,加入购物车,创建订单,图书管理等基本功能 前台使用Bootstrap ...
- oracle UDT 有关数据字典的研究
1.数据及类型准备 创建了一个自定义类型 create or replace type addr_type as object( street varchar2(30); city varchar2( ...
- 首页的css
html,body{ margin:; padding:; background-color: lavenderblush; } a{ color:darkgray; } li{ list-style ...