#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. 表单校验--js部分

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  2. location的属性

    http://localhost:8080/?a=b#/login location.host "localhost:8080" location.port 8080 locati ...

  3. 神奇的Form表单

    今天坐标单上传,提交的按钮使用了<button>,发现不论怎么写ajax和设置form表单,都会刷新页面,百思不得解,然后偶然间把<button>变成<input typ ...

  4. gitlab 服务器的搭建与使用全过程(二)

    <gitlab操作手册 1.0 > 此手册适用于 Mac 计算机 第一步:根据从管理员得到的用户名和初始密码登陆并修改密码.新密码不得少于8个字符 第二步:在自己的电脑上创建密钥,并提交提 ...

  5. 安装jdk1.8,编写环境变量

    好记性不如烂笔头!!!(我这是把jdk放在的/usr/local下,且命令为jdk1.8...复制的时候别搞错位置了) JAVA_HOME=/usr/local/jdk1./ JAVA_BIN=/us ...

  6. Domoticz 中添加彩云天气

    前言 用过一段时间的彩云天气 APP,最吸引我的地方是精确到局部区域的天气预测,虽然准确度并不算高,但是对于预测下雨还是不错的选择.在 Domoticz 中添加彩云天气的数据,利用的是彩云天气提供的 ...

  7. Android 开源项目使用指南

    1.日历项目 https://blog.csdn.net/iamchan/article/details/81214498

  8. JAVA数据结构--ArrayList动态数组

    在计算机科学中,动态数组,可扩展数组,可调整数组,动态表,可变数组或数组列表是一种随机存取可变大小列表数据结构,允许添加或删除元素.它提供许多现代主流编程语言的标准库.动态数组克服了静态数组的限制,静 ...

  9. Marlin (思维)

    The city of Fishtopia can be imagined as a grid of 44 rows and an odd number of columns. It has two ...

  10. Tensorflow基础-mnist数据集

    MNIST数据集,每张图片包含28*28个像素,把一个数组展开成向量,长度为28*28=784,故数据集中mnist.train.images是一个形状为[60000,784]的张量,第一个维度数字用 ...