HDU 6181 Two Paths
来源:互联网 发布:软件的应用价值 编辑:程序博客网 时间:2024/05/21 06:28
答案为次短路或者最短路+和最短路相连或者最短路上的路径走两遍,怎么一开始大家都会啊
#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>#include<iostream>using namespace std;typedef long long ll;const int MAXN =(int)1e5+10;const ll INF =(ll)1e11;struct edge{ int to,nxt; ll w;}ed[MAXN<<1];int head[MAXN],cnt;void addedge(int u,int v,ll w){ ed[cnt].to=v; ed[cnt].w=w; ed[cnt].nxt=head[u]; head[u]=cnt++;}ll d1[MAXN],d2[MAXN];int lst[MAXN];struct node{ int s,u;ll dis; node(){}node(int _s,int _u,ll _dis){s=_s;u=_u;dis=_dis;} bool operator < (const node &a)const{ return dis>a.dis; }};priority_queue<node>q;void dij(){ while(!q.empty()){ node now=q.top();q.pop(); int u=now.u,s=now.s;ll dis=now.dis; if(d2[u]!=INF)continue; if(d1[u]==INF){ d1[u]=dis; lst[u]=s; } else { d2[u]=dis; } for(int i=head[u];i!=-1;i=ed[i].nxt){ int v=ed[i].to;ll w=ed[i].w; if(d2[v]!=INF)continue; if(d2[v]>dis+w){ q.push(node(u,v,dis+w)); } } }}int main(){ int t; scanf("%d",&t); while(t--){ cnt=0; int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)d1[i]=d2[i]=INF,lst[i]=0,head[i]=-1; for(int i=1;i<=m;i++){ int u,v;ll w; scanf("%d%d%lld",&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } q.push(node(0,1,0)); dij(); ll ans=d2[n]; int now=n; while(now!=0){ for(int i=head[now];i!=-1;i=ed[i].nxt){ ans=min(ans,d1[n]+ed[i].w*2); } now=lst[now]; } printf("%lld\n",ans); } return 0;}
阅读全文
0 0
- HDU 6181 Two Paths
- HDU 6181 Two Paths
- hdu 6181 Two Paths
- HDU 6181 Two Paths (次短路)
- hdu 6181 Two Paths (次短路)
- 刷题—— hdu 6181 Two Paths
- hdu 6181 Two Paths(次短路)
- Hdu 6181 Two Paths【次短路】
- hdu-6181 Two Paths次短路
- HDU 6181 Two Paths 次短路
- hdu 6181 Two Paths (次短路)
- HDU 6181 Two Paths(dijk的深入)
- Two Paths HDU
- hdu 6181 Two Paths -最短路条数+次短路
- hdu 6181 Two Paths (多校第十场)
- HDU 6181 Two Paths【次短路】【模板题】
- HDU 6181 Two Paths(次短路变形)
- [hdu 6181 Two Paths] Dijkstra求次短路
- canvas简单操作
- IntelliJ IDEA 14 注册码
- java Filter-(java 过滤器)
- 343. Integer Break
- 关于EventBus3.0的介绍
- HDU 6181 Two Paths
- 残差网络resnet
- 简析 RDB 中的 触发器
- 浏览器如何向服务器发出请求
- Java如何实现对Mysql数据库的行锁
- cloudera安装过程报错:Your password does not satisfy the current policy requirements
- Android 查看Sqlite数据库数据
- C++ Primer 第三章 字符串、向量和数组
- UVALive7015(bfs求最短路)