#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 ;
}

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

最新文章

  1. C#模拟HTTP Form请求上传文件
  2. sql server常用语法点
  3. 将maven工程转成dynamic web project
  4. libc abi.dylib: terminate_handler unexpectedly threw an exception
  5. kafka常用的操作命令
  6. 【转载】跟着9张思维导图学习JavaScript
  7. javascript笔记4-函数表达式
  8. 高德地图关键字搜索删除上一次搜索的Marker
  9. [转]mysql 的日志的启动与查看
  10. easyUI的doCellTip 就是鼠标放到单元格上有个提示的功能
  11. 【百度地图API】北京周边7日游——图标按路线轨迹行动
  12. USACO Section 1.3 Prime Cryptarithm 解题报告
  13. Angular开发实践(四):组件之间的交互
  14. FFmpeg的H.264解码器源代码简单分析:解析器(Parser)部分
  15. pycharm 配置svn
  16. Java线程池ThreadPoolExecutor使用和分析(二) - execute()原理
  17. RMAN命令DELETE操作总结
  18. 【Python】解析Python中的条件语句和循环语句
  19. Foxmail7.2新建的文件夹不见了
  20. 完全理解 Python 迭代对象、迭代器、生成器

热门文章

  1. 【guava】前提条件
  2. java10:基于时间的版本控制
  3. gym 102082B dp
  4. sed的基本用法
  5. Java实现连接FTP服务并传递文件
  6. ssm裤架搭建异常: Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
  7. Fountains(非线段树版(主要是不太会用))
  8. leetcode 88 Merge Sorted Array 归并排序
  9. C# 获取控制面板软件信息
  10. Codeforces - 600E 树上启发式合并