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 ...
随机推荐
- PID控制器开发笔记之一:PID算法原理及基本实现
在自动控制中,PID及其衍生出来的算法是应用最广的算法之一.各个做自动控制的厂家基本都有会实现这一经典算法.我们在做项目的过程中,也时常会遇到类似的需求,所以就想实现这一算法以适用于更多的应用场景. ...
- Confluence 6 访问日志脚本
日志访问脚本在连接:https://confluence.atlassian.com/download/attachments/133267635/Atlassian-accessLogScripts ...
- Confluence 6 查看索引和提示
查看索引 Confluence 使用被称为 Lucene 的搜索引擎.如果你希望在你的 Confluence站点中查看更多有关索引的细节,你可以下载并且运行 Luke.Luke 是一个开发和诊断工具, ...
- react 为组件添加样式
width/height/fontSize:可以直接写数字: style={ width:200,height:200 } 其他带数字的可以:数字+'px' style={ lineHeight:20 ...
- lightoj1214 大数取模模板
#include<bits/stdc++.h> using namespace std; #define maxn 300 #define ll long long ll a,b; ]; ...
- sass方式实现颜色平铺(红色--->紫色)
<!DOCTYPE html><html lang="en"><head> <link rel="stylesheet" ...
- spring cloud Config--server
概述 使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spr ...
- python爬虫快递查询系统(源码)
import requestsimport json def get_express_type(postid): '''根据快递单号来智能判断快递类型''' url = 'http://www.kua ...
- 插件使用一树形插件---zTree
zTree是一款挺好用的树形插件,中文文档齐全,demo丰富. 官方网站是 http://www.treejs.cn/v3/main.php#_zTreeInfo 源码网站 https://githu ...
- MyBatis - 4.动态SQL
动态 SQL是MyBatis强大特性之一.极大的简化我们拼装SQL的操作. 动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处理器相似. MyBatis 采用功能强大的基于 OGNL ...