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行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
随机推荐
- loj#2978. 「THUSCH 2017」杜老师(乱搞)
题面 传送门 题解 感谢yx巨巨 如果一个数是完全平方数,那么它的所有质因子个数都是偶数 我们把每一个数分别维护它的每一个质因子的奇偶性,那么就是要我们选出若干个数使得所有质因子的个数为偶数.如果用线 ...
- JUC包下CyclicBarrier学习笔记
CyclicBarrier,一个同步辅助类,在API中是这么介绍的: 它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序中,这 ...
- urlencode编码问题(以及urlparse) 转
网址链接中的中文编码 中文的gbk(GB2312)编码: 一个汉字对应两组%xx,即%xx%xx 中文的UTF-8编码: 一个汉字对应三组%xx,即%xx%xx%xx 可以利用百度进行URL编码解码 ...
- Django 中 admin 的执行流程
Django 中 admin 的执行流程 1 循环加载执行所有已经注册的 app 中的 admin.py 文件 def autodiscover(): autodiscover_modules('ad ...
- 网络寻路(思维+vector的应用)-----------蓝桥备战系列
标题:网络寻路 X 国的一个网络使用若干条线路连接若干个节点.节点间的通信是双向的.某重要数据包,为了安全起见,必须恰好被转发两次到达目的地.该包可能在任意一个节点产生,我们需要知道该网络中一共有多少 ...
- HDU - 4035 循环型概率DP
题解待会在上 #include<iostream> #include<algorithm> #include<cstdio> #include<cstring ...
- hdu 1231 最大连续和
题意:给定一组数,求最大的连续和,且输出开始与结尾 #include<iostream> #include<cstdio> using namespace std; int s ...
- 【Python】探测网站是否可以访问
首先贴上简陋的python脚本 #coding:utf-8 import urllib,linecache for line in linecache.updatecache('url.txt'): ...
- 【CTF】某xss练手小游戏
http://test.xss.tv 1.http://47.94.13.75/test/level1.php?name=test 直接插入即可,如: http://47.94.13.75/test/ ...
- python附录-builtins.py模块str类源码(含str官方文档链接)
python附录-builtins.py模块str类源码 str官方文档链接:https://docs.python.org/3/library/stdtypes.html#text-sequence ...