HDU 2680-Choose the best route-最短路+超级源点
来源:互联网 发布:nginx 配置多目录访问 编辑:程序博客网 时间:2024/06/17 16:21
/** 单源最短路+超级源点 最短路采用Dijkstra算法用优先队列优化 超级源点是为了解决多起点 单终点的问题 题意: n个点m条单边 w个起点,问你从这些起点到 终点 s 的最短距离 考虑 设一个 超级源点0 从 源点0 到w个起点的距离都是0,然后 w 个起点到 s 的最短距离就是 从超级源点0到s的最短距离*/#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <ctime>#include <iostream>#include <algorithm>#include <string>#include <vector>#include <deque>#include <list>#include <set>#include <map>#include <stack>#include <queue>#include <iomanip>#define mem(a,x) memset(a,x,sizeof(a))#define gcd(a,b) __gcd(a,b)#define lowbit(x) (x&-x)#define LL long long#define MOD 1000000007#define INF 0x3f3f3f3f#define pi (acos(-1.0))#define eps (1e-8)using namespace std;const int maxn = 1100;struct edge{ int to,val; edge(int t,int v):to(t),val(v){}};struct HeapNode{ int v,val; bool operator < (const HeapNode &a) const{ return val > a.val; }};int dis[maxn];bool vis[maxn];vector<edge> G[maxn];int Dijkstra(int n,int s){ priority_queue<HeapNode> qu; for(int i=1;i<=n;i++) dis[i] = INF; memset(vis,0,sizeof(vis)); dis[0] = 0; qu.push((HeapNode{0,0})); while(!qu.empty()) { HeapNode x = qu.top();qu.pop(); int v = x.v; if(vis[v]) continue; for(int i=0;i<G[v].size();i++) { edge u = G[v][i]; if( dis[u.to] > dis[v] + u.val ){ dis[u.to] = dis[v] + u.val; qu.push((HeapNode){u.to,dis[u.to]}); } } } if( dis[s] >= INF ) return -1; return dis[s];}int main(){ int n,m,s; ///1000 20000 n while(~scanf("%d%d%d",&n,&m,&s)) { ///init for(int i=0;i<maxn;i++) G[i].clear(); int u,v,val; for(int i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&val); edge e(v,val); G[u].push_back(e); } int w,wx; scanf("%d",&w); for(int i=0;i<w;i++) { scanf("%d",&wx); G[0].push_back((edge){wx,0}); } printf("%d\n",Dijkstra(n,s)); } return 0;}
阅读全文
0 0
- HDU 2680-Choose the best route-最短路+超级源点
- HDU 2680 Choose the best route多源点最短路
- hdu 2680 Choose the best route 最短路spfa+超级源点
- poj2680---Choose the best route(超级源点+最短路)
- HDU 2680 Choose the best route 多源点+一汇点求最短路
- hdu 2680-Choose the best route最短路dijkstra
- HDU 2680 Choose the best route 变形最短路 DIJKSTRA
- HDU 2680Choose the best route(dijk最短路)
- hdu 2680 Choose the best route(最短路)
- hdu 2680 Choose the best route(最短路dijkstra(有重边))
- hdu 2680 Choose the best route 最短路
- HDU 2680 Choose the best route 最短路
- HDU-2680 Choose the best route(最短路[Dijkstra])
- hdu-2680Choose the best route (dijkstra求最短路)
- HDU 2680Choose the best route (最短路+迪杰斯特拉)
- #HDU 2680 Choose the best route 【SPFA最短路】
- hdu 2680 Choose the best route(最短路)
- HDU 2680 Choose the best route(最短路,反向)
- 求数组中的最大最小值
- spring回顾系列:定时任务
- 一元线性同余方程简介
- 线段树
- 欢迎使用CSDN-markdown编辑器
- HDU 2680-Choose the best route-最短路+超级源点
- 《循序渐进》前端学习视频教程资料汇总
- Android --- Json解析
- 草稿
- 循环语句
- 42. Trapping Rain Water
- 数据结构与算法(Java描述)-17、递归转化成非递归算法以及迷宫算法
- 递归公式时间复杂度举例
- File Permissions and the chmod/chgrp/chown commands