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控制算法及其常见的改进与补偿算法,基本已经覆盖了无模型和简单模型PID控制经典算法的大部.再接下来的我们将讨论智能PID控制,智能PID控制不同于常规意义下的智能控制,是 ...
- 使用Node.js+Hexo+Github搭建个人博客(续)
一.写在前面 在我的上一篇博客<使用Nodejs+Hexo+Github搭建个人博客>中,已经介绍了如何使用 Hexo 在 Github Pages 上搭建一个简单的个人博客.该篇博文将在 ...
- Oracle从入门到精通(详细) 明日科技
- jquery 获取和设置 select下拉框的值
获取Select : 获取select 选中的 text : $("#ddlRegType").find("option:selected").text(); ...
- algorithm的基本注意事项
find(): 返还指向该迭代器的指针,找不到返还last:lnlt find(lnlt first,lnlt last ,const T&val);范围[first,last); list: ...
- Python实战二
要求:按照要求完成对文件的增.删.改.查操作. def add(**kwargs): '''新增内容,在指定位置新增''' while True: flag = False with open(&qu ...
- WireShark Flow capture analysis
Wiresharkl流量分析 1.图示是对WiFi数据接口的80号端口进行数据捕获,设置混杂模式 过滤表达式设置: IP地址设置过滤 ip.src==191.168.1.102 ip.dst ...
- bzoj 4816
这题是莫比乌斯反演的典型题也是很有趣的题. 题意:求,其中f为为斐波那契数列 那么首先观察一下指数,发现是我们熟悉的形式,可以转化成这样的形式: 令T=kd,且假设n<m,有: 令 则原式= 这 ...
- java测试
//信1705-1 20173527 刘津鑫package money;import java.io.IOException;import java.io.Serializable;import ja ...
- C. cltt的幸运数LCAtarjan
/*C: cltt的幸运数 Time Limit: 1 s Memory Limit: 128 MB Submit Problem Description 一棵树有n个节点,共m次查询,查询 ...