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 ...
随机推荐
- 【程序员技术练级】学习一门脚本语言 python(一)文件处理
现在工作上主要用的语言是java,java在企业级的应用上能够发挥很好的用途,但有时候要做一个小功能时,比如批量更新文件,抓取网页等,这时候用java就显得太笨重了.因此就学习了python这门脚本语 ...
- vsCode代码缩略图
vsCode配置代码缩略图: 文件--首选项--设置 搜索 minimap true 打开 false 关闭
- Thrift笔记(三)--Thrift框架通信源码分析
Thrift 客户端调用RPC的Demo public static void main(String[] args) throws Exception { TTransport transport ...
- bind 事件名称 命名空间
1.通过在事件名称后面添加以点号分隔的后缀来为事件名称指派命名空间 $("#button").bind("click.editMode",function(){ ...
- arcgis C#判断点在线段的左右侧
要判断一个点在一条线段的左侧还是右侧,从网上查到了这样一个算法.其实本质上就是一个sin角度的计算问题. 设线段端点为从A(x1,y1)到B(x2,y2),线外一点P(x0,y0),判断该点位于有向线 ...
- mysql 常用操作语句
1 根据表中的其中一个字段的值来修改同行某字段的值 UPDATE radar a INNER JOIN radar b ON a.id=b.id SET a.letter=LEFT(b.filena ...
- android的MVP模式
MVP简介 相信大家对MVC都是比较熟悉了:M-Model-模型.V-View-视图.C-Controller-控制器,MVP作为MVC的演化版本,那么类似的MVP所对应的意义:M-Model-模型. ...
- Android Proguard.flags LOCAL_PROGUARD_FLAGS
在Android项目中用到JNI,当用了proguard后,发现native方法找不到很多变量,原来是被produard优化掉了.所以,在JNI应用中该慎用progurad啊. 解决办法: 1.在An ...
- 使用canvas来绘制折线图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Selenium2学习(五)-- SeleniumBuilder辅助定位元素
前言 福利来了,对于用火狐浏览器的小伙伴们,你还在为定位元素而烦恼嘛? 上古神器Selenium Builder来啦,哪里不会点哪里,妈妈再也不用担心我的定位元素问题啦!(但是也不是万能,基本上都能覆 ...