hdu 4725 The Shortest Path in Nya Graph 【拆点】+【最短路】
<题目链接>
题目大意:
每个点放在一层,然后给了n个点,相邻的两层距离是固定的c,有额外m条无向边,然后求1到n的最短路径,如果没有则输出-1 。
解题分析:
本题建图是关键,需要注意的是,每一层不一定只有一个点。因此,如果两层之间建边时只是简单将上面的所有点的相互连接,那么取极端情况,当只有两层
并且每层只有50000个点时,在O(N^2)的复杂度下,光是建图就已经爆了。所以我们对每一层进行拆点,但是如果每一层只拆成一个点的话,那么该层每一个
点与拆成的点之间是双向边,这样的话,该层之间所有的点之间的距离就为0了,明显不符合题意。所以我们每一层要拆成两个点,该层所有点----->拆点1,
拆点2----->该层所有点,这样该层所有点之间就不是相互可达了。
#include <bits/stdc++.h>
using namespace std; const int M = 8e5+;
#define INF 0x3f3f3f3f int n,m,c;
struct EDGE{ int to,val,nxt; }dge[M]; int head[M],cnt;
int vis[M]; struct NODE{
int loc,dis;
bool operator <(const NODE &tmp)const{ return dis>tmp.dis; }
}d[M];
inline void init(){ cnt=;memset(head,-,sizeof(head)); } inline void add(int u,int v,int w){
e[cnt]=(Edge){v,w,head[u]};head[u]=cnt++;
}
void dij(int N){
for(int i=;i<=N;i++){
vis[i]=;
d[i].loc=i,d[i].dis=INF;
}
priority_queue<NODE>q;
d[].dis=;
q.push(d[]);
while(!q.empty()){
NODE now=q.top();
q.pop();
if(vis[now.loc])continue;
vis[now.loc]=;
for(int i=head[now.loc];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if(d[v].dis>d[now.loc].dis+edge[i].val){
d[v].dis=d[now.loc].dis+edge[i].val;
q.push(d[v]);
}
}
}
} int main(){
int ncase=;
int T;scanf("%d",&T);
while(T--){
init();
scanf("%d%d%d",&n,&m,&c);
for(int i=;i<=n;i++){
int u;scanf("%d",&u);
add(i,n+*u-,); //如果只将每一层虚拟成一个点,那么这样建双向边的话,就会使每一层的点相互可达,并且权值为0,很明显不行
add(n+*u,i,); //所以要像这样,该层所有点指向N+2*u-1,N+2*u指向该层所有点,这样建图不会让该层所有点之间存在双向边,符合题意
} for(int i=;i<n;i++){
add(n+*i-,n+*(i+),c); //连接i--->j层,让第i层管入度的虚拟点变成建边的起始点(把图想象出来就很好理解了)
add(n+*(i+)-,n+*i,c); //连接j--->i层
} for(int i=;i<=m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
dij(*n);
if(d[n].dis==INF)d[n].dis=-;
printf("Case #%d: %d\n",++ncase,d[n].dis);
}
}
2018-09-02
hdu 4725 The Shortest Path in Nya Graph 【拆点】+【最短路】的更多相关文章
- HDU - 4725 The Shortest Path in Nya Graph(拆点+Dijkstra)
题意:N个点,每个点有一个层号L,相邻的两层 Li 与 Li+1 之间的距离为C.另外给出M条无向边,求从点1到点N的最短路. 分析:同一层之间的两点距离并不是0,这是一个小坑.依次把相邻两层的所有点 ...
- 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 ...
随机推荐
- JavaScript从入门到精通(附光盘1张):作者:明日科技出版社:清华大学出版社出版时间:2012年09月
本书介绍 一:本书 pdf 获取信息 本书下载:请申请加入本群 (QQ群:668345923), 并联系群主. 本群主有:本书pdf 全文教材 及附带的 光盘内容 二:本书目录介绍 第1篇 基 ...
- swoole 使用异步redis的前置条件
redis安装 官网下载redis 下载完成之后解压: 进入redis目录执行make: 进入src目录启动redis 启动成功如下: 启动后连接redis 编译安装hiredis 下载:https: ...
- C#概念总结(一)
1.C#程序的框架问题 首先是命名的空间申明 (NameSpace delclaration) 一个 ClASS class 方法 class属性 一个main 的方法 语句(Statement) ...
- 基于kali linux无线网络渗透测试
1.无线网络渗透测试目前主要有三种方式,分别是暴力破解PIN码,跑握手包,搭建伪热点三种方式,当然还存在其他的方式. 1.1暴力破解 路由器的PIN码由八位0-9的数字组成,PIN码由散步风组成,前四 ...
- 三 os模块
os模块是与操作系统交互的一个接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相 ...
- Python sendmail
#coding:utf- #强制使用utf-8编码格式 import smtplib #加载smtplib模块 from email.mime.text import MIMEText from em ...
- Spring Boot配置大全
Spring Boot项目使用一个全局的配置文件application.properties或者是application.yml,在resources目录下或者类路径下的/config下,一般我们放到 ...
- JCenter下载太慢, jcenter修改 https为http也许能帮助你
今天导入一个工程到studio,一直卡在下载那块. 看到下载地址是:https://jcenter.bintray.com/........https!!!! 到浏览器下载,果然也下载不下来.. 于是 ...
- 谷歌浏览器Software Reporter Tool长时间占用CPU解决办法
什么是Software Reporter Tool Software Reporter Tool是一个Chrome清理工具,用于清理谷歌浏览器中不必要或恶意的扩展,应用程序,劫持开始页面等等.当你安装 ...
- python---hash查找
以前只会用,没了解过原理. # coding = utf-8 class HashTable: def __init__(self): # 哈希表的初始大小已经被选择为 11.尽管这是任意的,但是重要 ...