HDU - 4725 The Shortest Path in Nya Graph(拆点+Dijkstra)
题意:N个点,每个点有一个层号L,相邻的两层 Li 与 Li+1 之间的距离为C。另外给出M条无向边,求从点1到点N的最短路。
分析:同一层之间的两点距离并不是0,这是一个小坑。依次把相邻两层的所有点连边会导致复杂度很高。可以将每一层看作一个点,但是把它和层中的点连边会导致同层的两点距离为0。
为了避免这种情况,可以将每层拆作两点,表示入点和出点。所以所建图中一共有3N个点。1~N为原图中的点,N+1~2*N为每层出点,2*N+1~3*N为每层入点。对每个在该层中的点u,将其连至出点N+i;再将入点2N+i连至u。再将相邻两层的出点入点对应连接。最后跑一下Dijkstra。
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<vector>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
typedef long long LL;
const int maxn = 3e5+;
const LL INF = 1ll<<;
struct Edge{
int to,next;
LL val;
};
struct HeapNode{
LL d; //费用或路径
int u;
bool operator < (const HeapNode & rhs) const{return d > rhs.d;}
};
struct Dijstra{
int n,m,tot;
Edge edges[maxn<<];
bool used[maxn];
LL d[maxn];
int head[maxn]; void init(int n){
this->n = n;
this->tot=;
memset(head,-,sizeof(head));
} void Addedge(int u,int v ,LL dist){
edges[tot].to = v;
edges[tot].val = dist;
edges[tot].next = head[u];
head[u] = tot++;
} void dijkstra(int s){
memset(used,,sizeof(used));
priority_queue<HeapNode> Q;
for(int i=;i<=n;++i) d[i]=INF;
d[s]=;
Q.push((HeapNode){,s});
while(!Q.empty()){
HeapNode x =Q.top();Q.pop();
int u =x.u;
if(used[u])
continue;
used[u]= true;
for(int i=head[u];~i;i=edges[i].next){
Edge & e = edges[i];
if(d[e.to] > d[u] + e.val){
d[e.to] = d[u] +e.val;
Q.push((HeapNode){d[e.to],e.to});
}
}
}
}
}G; int lay[maxn]; //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int N,M,T,u,v,cas=;
LL tmp,C;
scanf("%d",&T);
while(T--){
scanf("%d%d%lld",&N,&M,&C);
G.init(*N);
for(int i=;i<=N;++i){
scanf("%d",&tmp);
G.Addedge(i,tmp+N,); //1~N为层,N~2*N为出点,2*N~3*N为入点
G.Addedge(tmp+*N,i,);
}
for(int i=;i<=N-;++i){
G.Addedge(N+i,*N+i+,C); //将相邻两层出点入点对应连接
G.Addedge(N+i+,*N+i,C);
}
for(int i=;i<=M;++i){
scanf("%d%d%lld",&u,&v,&tmp);
G.Addedge(u,v,tmp);
G.Addedge(v,u,tmp);
}
G.dijkstra();
if(G.d[N]==INF) G.d[N]=-;
printf("Case #%d: %lld\n",cas++,G.d[N]);
}
return ;
}
HDU - 4725 The Shortest Path in Nya Graph(拆点+Dijkstra)的更多相关文章
- Hdu 4725 The Shortest Path in Nya Graph (spfa)
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
- HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]
HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...
- HDU 4725 The Shortest Path in Nya Graph
he Shortest Path in Nya Graph Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged o ...
- HDU 4725 The Shortest Path in Nya Graph(构图)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 4725 The Shortest Path in Nya Graph (最短路)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu 4725 The Shortest Path in Nya Graph (最短路+建图)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- HDU 4725 The Shortest Path in Nya Graph (最短路 )
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...
随机推荐
- MongoDB基本用法
MongoDB基本用法(增删改高级查询.mapreduce) 分享一下我经常用到的自己写的mongo用法示例 该示例基于当前最新的mongo驱动,版本为mongo-2.10.1.jar,用junit写 ...
- Nginx配置里的fastcgi_index和index
在配置nginx时有时会遇到, 所以记录一下 location ^~ /wechat/ { index index.php; fastcgi_pass 127.0.0.1:9000; fastcgi_ ...
- Hibernate_day04--HQL多表查询_Hibernate检索策略
HQL多表查询 Mysql里面多表查询 1 内连接 查的是两个表关联的数据 2 左外连接 查的是左边表所有数据,右边表关联数据 3 右外连接 HQL实现多表查询 Hql多表查询 (1)内连接 (2)左 ...
- Deep Learning的基本思想
假设我们有一个系统S,它有n层(S1,…Sn),它的输入是I,输出是O,形象地表 示为: I =>S1=>S2=>…..=>Sn => O,如果输出O等于输入I,即输入I ...
- 【iOS开发】 AudioSession设置, 切换扬声器和听筒详解-保留其他应用音乐(备忘)
本文转载至 http://blog.sina.com.cn/s/blog_693de6100101f1g8.html (2013-04-10 17:25:24) 转载▼ 标签: audiosessio ...
- 巨蟒python全栈开发flask12项目开始4
1.App实现录音功能 2.App上传录音 3.Toy消息提醒+消息存储 4.Toy收取消息 5.Chat页面完善&&本地播放&&网络播放&&获取聊天记 ...
- bash characters
linux shell通配符(wildcard) 通配符是由shell处理的(不是由所涉及到命令语句处理的,其实我们在shell各个命令中也没有发现有这些通配符介绍), 它只会出现在 命令的“参数”里 ...
- Spark源码分析 – DAGScheduler
DAGScheduler的架构其实非常简单, 1. eventQueue, 所有需要DAGScheduler处理的事情都需要往eventQueue中发送event 2. eventLoop Threa ...
- IOUtils方式上传下载文件
package com.css.hdfs04; import java.io.File; import java.io.FileInputStream; import java.io.FileOutp ...
- Visual Studio 2017 新特性
1. out-variables(Out变量) 以前,我们使用out变量的时候,需要在外部先申明,然后才能传入方法,类似如下: string ddd = ""; //先申明变量 c ...