PAT 1030 Travel Plan
#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的更多相关文章
- PAT 1030 Travel Plan[图论][难]
1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...
- 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 ...
- 1030 Travel Plan (30 分)
1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, toge ...
- [图算法] 1030. Travel Plan (30)
1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...
- PAT A 1030. Travel Plan (30)【最短路径】
https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...
- PAT (Advanced Level) 1030. Travel Plan (30)
先处理出最短路上的边.变成一个DAG,然后在DAG上进行DFS. #include<iostream> #include<cstring> #include<cmath& ...
- PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径
模板题最短路+输出路径如果最短路不唯一,输出cost最小的 #include <iostream> #include <cstdio> #include <algorit ...
- PAT 甲级 1030 Travel Plan
https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392 A traveler's map gives ...
- 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)
题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
随机推荐
- linux防火墙(一)—— iptables架构介绍
一.防火墙的分类 一般宏观来说,防火墙分为主机型防火墙,例如我们为了防止个人电脑被攻击,而开启的防火墙,还分为网关型防火墙,一般部署在企业的网关,用于过滤和转发,保证整个企业的网络环境安全性. 按照物 ...
- sql 表字段模糊连接
select AreauserCode,RtuName from TB_AreaUser as tau right join TB_MaintenanceInfo inf on inf.RtuName ...
- 使用navicat将mysql转换成sqlserver
使用navicat将mysql转换成sqlserver 1. 打开navicat,连接所需要装换的mysql数据库. 2. 选择所需要转换的数据源,点击右键选择数据传输.如图: 3. 打开数据传输面板 ...
- 一个数字从后向前输入每一位数字,Camel和Pascal命名规范,IsValid()
int num = int.Parse(Console.ReadLine()); ; ) { n = num % ; num /= ; Console.WriteLine(n); } Camel和Pa ...
- HLS-搭建Nginx流媒体服务器
Nginx本身是一个非常出色的HTTP服务器,FFMPEG是非常好的音视频解决方案.这两个东西通过一个nginx的模块nginx-rtmp-module,组合在一起即可以搭建一个功能相对比较完善的流媒 ...
- spring中的idref标签详解
spring中的idref元素 idref元素是一个简单的对容器中存在的另外一个bean的检错途径(通过id); <idref bean="someBeanId"/> ...
- [HNOI/AHOI2018]排列
[Luogu4437] 如果\(a[i]=j\)则序列\(p[]\)中\(j\)必须排在\(i\)前面,如果\(j\)不在范围内则不管,求一个式子\(\sum_{i=1}^n iw_{p[i]}\)的 ...
- POJ_1984 Navigation Nightmare 【并查集】
一.题面 POJ1984 二.分析 这题还是比较有意思的一题. 首先需要清楚的是,这题与普通并查集的区别在于它的节点之间的权值是二维的,因为是曼哈顿距离,肯定不能直接存距离,这样将不利于后面的路径压缩 ...
- ZOJ - 3623 完全背包变种
题意理解有误导致方程建歪,题意是n种类型的船造成至少L伤害的最小时间,攻击过程是不必同步的 #include<iostream> #include<algorithm> #in ...
- 有关eval用法的小结
首先要明白eval.这个是可以把字符串代码,直接当做js运行.比如 var ss="alert(1);";这个时候ss保存的是文本. 使用 eval(ss);这样就相当于,执行了s ...