#include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
#include <unordered_map>
#include <algorithm>
#include <climits> #define CMB(ID1, ID2) (((ID1)<<9) | (ID2)) using namespace std; class City {
public:
vector<int> adj;
int dist;
City(int d = INT_MAX): dist(d){}
}; typedef pair<int, int> P; // sparse table
unordered_map<int, int> dist;
unordered_map<int, int> cost; void print_cities(vector<City> &cities) {
int len = cities.size(); for (int i=; i<len; i++) {
printf("%d\n", i);
int adj_len = cities[i].adj.size();
for (int j=; j<adj_len; j++) {
int cid = CMB(i, cities[i].adj[j]);
printf("\t%d %d %d", cities[i].adj[j], dist[cid], cost[cid]);
}
printf("\n");
}
} void dfs(vector<int> &final, vector<int>& path, int path_cost, int &final_cost, vector<City>& cities, int idx) {
if (cities[idx].dist == ) {
// we reach the start point
if (path_cost < final_cost) {
final_cost = path_cost;
final = path;
final.push_back(idx);
}
return;
}
City& cur_city = cities[idx];
int adj_len = cur_city.adj.size(); for (int i=; i<adj_len; i++) {
int adj_idx = cur_city.adj[i];
int pdist = cities[adj_idx].dist + dist[CMB(idx, adj_idx)];
if (pdist == cur_city.dist) {
// adj city on the shortest path
path.push_back(idx);
path_cost += cost[CMB(idx, adj_idx)];
// follow it
dfs(final, path, path_cost, final_cost, cities, adj_idx); path_cost -= cost[CMB(idx, adj_idx)];
path.pop_back();
}
}
} int main() {
int N, M, S, D;
scanf("%d%d%d%d", &N, &M, &S, &D); vector<City> cities(N); int c1, c2, d, c; for (int i=; i<M; i++) {
scanf("%d%d%d%d", &c1, &c2, &d, &c);
dist.insert(make_pair(CMB(c1, c2), d));
dist.insert(make_pair(CMB(c2, c1), d));
cost.insert(make_pair(CMB(c1, c2), c));
cost.insert(make_pair(CMB(c2, c1), c));
cities[c1].adj.push_back(c2);
cities[c2].adj.push_back(c1);
} cities[S].dist = ; //print_cities(cities); priority_queue<P, vector<P>, greater<P> > nodes; nodes.push(make_pair(, S)); bool updated = true;
while (updated) {
updated = false;
P node = nodes.top();
nodes.pop();
int cur_idx = node.second;
int cur_dst = node.first;
if (cur_dst > cities[cur_idx].dist) {
// there is another shorter path to the current selected city
// so the node info is out of date, just drop it
updated = true;
continue;
} City& cur_city = cities[cur_idx];
int alen = cur_city.adj.size(); // traverse adj cities of the current city
for (int i=; i<alen; i++) {
int adj_idx = cur_city.adj[i];
City& adj_city = cities[adj_idx]; int new_dist = cur_city.dist + dist[CMB(cur_idx, adj_idx)];
if (new_dist < adj_city.dist) {
adj_city.dist = new_dist;
nodes.push(make_pair(new_dist, adj_idx));
updated = true;
}
}
} vector<int> final, path;
int path_cost = , final_cost = INT_MAX;
dfs(final, path, path_cost, final_cost, cities, D); int flen = final.size();
if (flen < ) {
return ;
}
reverse(final.begin(), final.end()); printf("%d", final[]);
for (int i=; i<flen; i++) {
printf(" %d", final[i]);
}
printf(" %d %d", cities[D].dist, final_cost);
return ;
}

又是最短路径,好像很喜欢,有点烦

PAT 1030 Travel Plan的更多相关文章

  1. PAT 1030 Travel Plan[图论][难]

    1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...

  2. PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)

    1030 Travel Plan (30 分)   A traveler's map gives the distances between cities along the highways, to ...

  3. 1030 Travel Plan (30 分)

    1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, toge ...

  4. [图算法] 1030. Travel Plan (30)

    1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...

  5. PAT A 1030. Travel Plan (30)【最短路径】

    https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...

  6. PAT (Advanced Level) 1030. Travel Plan (30)

    先处理出最短路上的边.变成一个DAG,然后在DAG上进行DFS. #include<iostream> #include<cstring> #include<cmath& ...

  7. PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径

    模板题最短路+输出路径如果最短路不唯一,输出cost最小的 #include <iostream> #include <cstdio> #include <algorit ...

  8. PAT 甲级 1030 Travel Plan

    https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392 A traveler's map gives ...

  9. 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)

    题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...

随机推荐

  1. vue的生命周期钩子函数

    一.vue生命周期图示 二.钩子函数执行时间 beforeCreate      在创建实例之前,data只声明但没有赋值  在实例初始化之后,数据观测 (data observer) 和 event ...

  2. 1. C语言对文件的操作

    1. 文件常见输入输出函数与屏幕.键盘输入输出函数的对比,如:fprintf.fscanf等. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h ...

  3. Python实现——决策树(部分函数/连续数据)

    由于上一例的实现中只针对了离散数据,为了扩充处理范围,我实现了一下对线性数据的简单处理,在其中我选择用中位数作为指标,平均数.众数等等其他数据在我看来异曲同工,最终也都会有较相似的结构. 求连续数据的 ...

  4. 安装 CentOs 系统 及 Python 及 Scrapy 框架

    1: 先安装Centos 系统: 为什么选择CentOs系统,而不选择Ubuntu ? 我在Ubuntu上尝试了三次安装 python 和 Scrapy ,结果都没成功,在运维老王的建议下 使用Cen ...

  5. java基础_01

    一.java中的数据类型 1.基本数据类型:四类八种 byte(1),boolean(1),short(2),char(2),int(4),float(4),long(8),double(8); 2. ...

  6. day--43 HTML标签和CSS基本小结

    HTML标签和CSS基本小结一:常用标签 01:块标签 p,h1--h6 ,hr ,div 02:内联标签 b,i,u,s 小提示:块标签可以嵌套内置元素或者某些块级元素,但内联元素不能包含块级元素 ...

  7. O(1)乘法与快速乘O(log)

    //O(1)快速乘 inline LL quick_mul(LL x,LL y,LL MOD){ x=x%MOD,y=y%MOD; return ((x*y-(LL)(((long double)x* ...

  8. BZOJ - 1497 最小割应用

    题意:基站耗费成本,用户获得利益(前提是投入成本),求最大获利 最小割的简单应用,所有可能的收益-(消耗的成本/失去的收益),无穷大边表示冲突,最小割求括号内的范围即可 #include<ios ...

  9. c++ 222

        [成功者的习惯]   1.背后说别人好话:听到某人说别人坏话时只微笑: 2.过去的事不全让人知道: 3. 尊敬不喜欢你的人:对事无情,对人有情: 4.多做自我批评:为别人喝彩: 5.感恩:学会 ...

  10. 破解mysql数据库的密码

    发现的1小问题 语句打错以后应该退出本语句,再继续打新语句.也可以打\c,退出本语句. 如何破解数据库的密码: 1:通过任务管理器或者服务管理,关掉mysqld(服务进程) 2:通过命令行+特殊参数开 ...