hdu 2680 Choose the best route(SPFA或者Dijkstra)
来源:互联网 发布:java获取浏览器类型 编辑:程序博客网 时间:2024/05/19 09:10
http://acm.split.hdu.edu.cn/showproblem.php?pid=2680
把0看成一个大源点。走一遍最短路就行了。
Dijkstra优先队列优化
#include<iostream>#include<cstring>#include<cstdio> #include<queue>#include<vector>#define INF 0x3f3f3f3fusing namespace std;const int maxn=1005;struct node{ int v,len; bool operator < (const node&a)const{ return len>a.len; }};vector<node>G[maxn];int n,m,s,d[maxn],vis[maxn];void Dijkstra(){ memset(vis,0,sizeof(vis)); for(int i=0;i<=n;i++) d[i]=INF; d[0]=0; priority_queue<node>Q; Q.push(node{0,0}); while(!Q.empty()) { node now=Q.top(); Q.pop(); int u=now.v; if(vis[u]) continue; vis[u]=1; for(int i=0;i<G[u].size();i++) { int v=G[u][i].v,w=G[u][i].len; if(!vis[v]&&d[v]>d[u]+w) { d[v]=d[u]+w; Q.push(node{v,d[v]}); } } } }int main(){ while(~scanf("%d%d%d",&n,&m,&s)) { int u,v,w,x; for(int i=0;i<=n;i++) G[i].clear(); while(m--) { scanf("%d%d%d",&u,&v,&w); G[u].push_back(node{v,w}); } scanf("%d",&m); while(m--) { scanf("%d",&x); G[0].push_back(node{x,0}); } Dijkstra(); if(d[s]==INF) printf("-1\n"); else printf("%d\n",d[s]); } return 0;}
SPFA
#include<iostream>#include<cstring>#include<cstdio> #include<queue>#include<vector>#define INF 0x3f3f3f3fusing namespace std;const int maxn=1005;struct node{ int to,w;};vector<node>G[maxn];int n,m,s,d[maxn],vis[maxn];void SPFA(int src){ for(int i=0;i<=n;i++) { d[i]=INF; vis[i]=false; } d[src]=0; vis[src]=true; queue<int>q; q.push(src); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=false; for(int i=0;i<G[u].size();i++) { int v=G[u][i].to,w=G[u][i].w; if(d[v]>d[u]+w) { d[v]=d[u]+w; if(!vis[v]) { vis[v]=true; q.push(v); } } } }}int main(){ while(~scanf("%d%d%d",&n,&m,&s)) { int u,v,w,x; for(int i=0;i<=n;i++) G[i].clear(); while(m--) { scanf("%d%d%d",&u,&v,&w); G[u].push_back({v,w}); } scanf("%d",&m); while(m--) { scanf("%d",&x); G[0].push_back({x,0}); } SPFA(0); if(d[s]==INF) printf("-1\n"); else printf("%d\n",d[s]); } return 0;}
阅读全文
0 0
- hdu 2680 Choose the best route(SPFA或者Dijkstra)
- hdu 2680 Choose the best route(spfa/dijkstra)
- HDU 2680:Choose the best route【Dijkstra & SPFA】
- HDU 2680 Choose the best route【多源最短路,Dijkstra+spfa】
- hdu 2680 Choose the best route (最短路问题 dijkstra | spfa)
- HDU-2680 Choose the best route(SPFA)
- HDU-2680-Choose the best route【spfa】
- HDU 2680 Choose the best route(dijkstra)
- HDU--2680Choose the best route【Dijkstra】
- hdu 2680 Choose the best route (dijkstra)
- HDU 2680 Choose the best route dijkstra
- HDU 2680-Choose the best route(Dijkstra)
- HDU 2680Choose the best route (SPFA)
- hdu 2680 Choose the best route (Dijkstra & 反向图)
- HDU-2680 Choose the best route(最短路[Dijkstra])
- hdu-2680-Choose the best route(dijkstra)
- HDU 2680 Choose the best route(Dijkstra)
- HDU2680---Choose the best route(dijkstra&&SPFA)
- Spring(26)——PathMatchingResourcePatternResolver
- 050 函数凹凸性
- iOS下 WebRTC 视频渲染
- Python动态参数
- CentOS学习24_Centos7 关闭防火墙
- hdu 2680 Choose the best route(SPFA或者Dijkstra)
- tinyalsa
- 新版android studio去掉cmake
- 类与对象第四题
- 文章标题
- CSP考试 2017年9月第1题 打酱油 C++实现
- 用户组的建立和权限修改
- Java集合框架的一些总结
- 码云上传小工具