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 ...
随机推荐
- 如何用ChemDraw中的ChemFinder查询反应过程
ChemFinder是ChemDraw化学绘图软件的重要插件之一,ChemFinder是一个贮存众多化学信息的数据库管理系统,不仅可以用于查询基本化学结构,用户还可以用ChemFinder查询需要的反 ...
- 快学scala习题解答--第五章 类
5 类 5.1 改进5.1节的Counter类,让它不要在Int.MaxValue时变成负数 class Count{ private var value = Int.MaxValue else v ...
- 自动化测试环境准备robotframework
(一)针对python2.7版本的自动化环境准备: python 下载地址: https://www.python.org/downloads/ 这里选择Python2.7系列的,后面涉及到wxPyt ...
- jenkins 配置 ssh插件
一.安装SSH插件 系统管理->插件管理,在可选插件下,过滤SSH,找到publish over ssh插件,直接安装(我这里已经安装过了,在已安装选项下可以找到publish over ssh ...
- java方法的理解、调用栈与异常处理
一.流程分支 If/else :基于boolean值的双分支 Switch:基于数字(整数.char.byte.枚举).字符串 类型的多分支 Int month =5; Switch 二.方法meth ...
- Sass之二(进阶篇)
源码链接:http://pan.baidu.com/s/1o8M51hC 1. 数据类型 1.1 Number 数字类型,小数类型,带有像素单位的数字类型,全部都属于Number类型 Number类型 ...
- CEF3 HTML5 audio标签为什么不能播放mp3格式的音频文件
CEF3 HTML5 audio标签 为什么不能播放mp3格式的音频文件 原因略. 解决方法: 找一个最新版的chrome ,我用的是24版本.路径 C:\Documents and Sett ...
- Exchange Port
Get-POPSettings-110 Get-IMAPSettings-143 Exchange Network Port References Exchange Server 2000 http: ...
- authz_core_module
w https://httpd.apache.org/docs/trunk/mod/mod_authz_core.html codeigniter index.html .htaccess <I ...
- xpath scrapy shell
w from scrapy.spider import Spider from scrapy.crawler import CrawlerProcess import pymysql conn = p ...