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 (Java/Others)
Total Submission(s): 13445 Accepted Submission(s): 2856
en un grafico, and just solo hay que cambiar un poco el algoritmo. If you do
not understand a word of this paragraph, just move on.
The Nya graph is an undirected graph with "layers". Each node in the graph
belongs to a layer, there are N nodes in total.
You can move from any node in layer x to any node in layer x + 1, with cost
C, since the roads are bi-directional, moving from layer x + 1 to layer x is
also allowed with the same cost.
Besides, there are M extra edges, each connecting a pair of node u and v,
with cost w.
Help us calculate the shortest path from node 1 to node N.
cases.
For each test case, first line has three numbers N, M (0 <= N, M <= 105)
and C(1 <= C <= 103), which is the number of nodes, the number of
extra edges and cost of moving between adjacent layers.
The second line has N numbers li (1 <= li <= N), which
is the layer of ith node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w
(1 <= w <= 104), which means there is an extra edge, connecting a
pair of node u and v, with cost w.
moving from node 1 to node N.
If there are no solutions, output -1.
【题意】
给一张图,n个点,m条有权无向边,每个点属于某一层,相邻层间的任意两点存在一条权值为C的边,问1到n的最短路
【分析】
此图数据量比较大,暴力建图不可取!会MLE or TLE.
可以将层也抽象化成点,也就是一共有N个点节点和N个层节点,然后按照层与层之间(双向,权值C)、点与点之间(即后来给的M条边)、点与相对应的层之间(层指向点,权值0),点与对应层的相邻层之间(点指向层,权值C)建图,最后求最短路即可
解释一个代码中难理解的地方:
这里是点和所在层建立关系
不能建双向边的原因是假设有两个点在同一层
比如有三个点,点1在第一层,点2也在第一层,虚拟第一层为点4,那么1-4有一条距离为0的点,4-1有一条距离为0的点
2-4有一条距离为0的点,4-2有一条距离为0的点,那么1-2距离就成为0了,这是不对的。
这两个if建立单向边的原因是,如果三层,中间一层没有点,建立双向边会导致最上和最下的两层可以相通,而事实上是不通的
【代码】
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=2e5+5;
#define pir pair<int,int>
int n,m,c,cas,cnt,S,la[N],dis[N];bool has[N];
struct node{int v,w,next;}e[N<<2];int tot,head[N];bool vis[N];
inline void addedge(int x,int y,int z){
e[++tot].v=y;e[tot].w=z;e[tot].next=head[x];head[x]=tot;
}
inline void add(int x,int y,int z){
addedge(x,y,z);
addedge(y,x,z);
}
inline void Clear(){
tot=0;
memset(head,0,sizeof head);
memset(has,0,sizeof has);
memset(vis,0,sizeof vis);
memset(dis,0x3f,sizeof dis);
}
inline void Init(){
scanf("%d%d%d",&n,&m,&c);
for(int i=1;i<=n;i++) scanf("%d",&la[i]),has[la[i]]=1;
for(int i=1,x,y,z;i<=m;i++) scanf("%d%d%d",&x,&y,&z),add(x,y,z);
for(int i=1;i<=n;i++) if(has[i]&&has[i+1]) add(i+n,i+1+n,c);
for(int i=1;i<=n;i++){
addedge(la[i]+n,i,0);
if(la[i]>1) addedge(i,la[i]+n-1,c);
if(la[i]<n) addedge(i,la[i]+n+1,c);
}
}
#define mp make_pair
inline void dijkstra(){
priority_queue<pir,vector<pir>,greater<pir> >q;
q.push(mp(dis[S=1]=0,S));//vis[S]=1;
while(!q.empty()){
pir t=q.top();q.pop();
int x=t.second;
if(vis[x]) continue;
vis[x]=1;
for(int i=head[x];i;i=e[i].next){
int v=e[i].v;
if(!vis[v]&&dis[v]>dis[x]+e[i].w){
q.push(mp(dis[v]=dis[x]+e[i].w,v));
}
}
}
printf("Case #%d: %d\n",++cnt,dis[n]<0x3f3f3f3f?dis[n]:-1);
}
int main(){
for(scanf("%d",&cas);cas--;){
Clear();
Init();
dijkstra();
}
return 0;
}
HDU 4725 The Shortest Path in Nya Graph(构图)的更多相关文章
- 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,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 ...
- HDU - 4725 The Shortest Path in Nya Graph 【拆点 + dijkstra】
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...
随机推荐
- 查看linux系统硬盘目录占用大小
http://jingyan.baidu.com/article/3aed632e198ae870108091b4.html du -sh /* 先看看根目录下面 du -sh /usr/* du ...
- Java 之外,是 Scala 还是 Groovy?【转载】
原文地址 Scala 和 Groovy 都是基于 JVM 的语言,相比 Java,它们都有语法更加简明和表达能力更丰富.对于那些既想不脱离开 JVM 又想避免 Java 繁琐语句的开发人员来说,Sca ...
- centos7操作SSH/SSHD服务(查看/启动/重启/自启)
查看状态: systemctl status sshd.service 启动服务: systemctl start sshd.service 重启服务: systemctl restart sshd. ...
- MongoDB 进程控制系列二:结束进程
1:如果某个进程产生了异常,可以考虑将其kill掉 db.killOp(10417) db.killOp(10417/*opid*/) 等同于: db.$cmd.sys.killop.findOne( ...
- dup2替换
今天看APUE上一道题,要求不能用fcnt1来替换dup1. 刚开始的思路是dup一个,测试发现与期望的不一致就马上关闭,发现遇到无限循环,刚才想了下,才发现一旦close掉,再次dup仍然是分配最小 ...
- 编写SHELL脚本--编写简单脚本
1.简单脚本文件hello.sh,内容如下 #!/bin/bash pwd ls -al 执行脚本:bash hello.sh 或者使用root命令: ./hello.sh 2.接受用户参数 $0 ...
- 【驱动】Linux初级驱动系列框架
[系统环境搭建] 1.uboot的命令 set serverip .xx set ipaddr .xxx set bootcmd tftp zImage\;bootm //开发模式 set bootc ...
- 【Java】Comparable和Comparator接口的区别
Java提供了只包含一个compareTo()方法的Comparable接口.这个方法可以个给两个对象排序.具体来说,它返回负数,0,正数来表明已经存在的对象小于,等于,大于输入对象. Java提供了 ...
- MXNET:权重衰减
权重衰减是应对过拟合问题的常用方法. \(L_2\)范数正则化 在深度学习中,我们常使用L2范数正则化,也就是在模型原先损失函数基础上添加L2范数惩罚项,从而得到训练所需要最小化的函数. L2范数惩罚 ...
- Linux x64系统上安装 oracle 11g R2 x64
1.首先到官网上下载oracle 11g x64位软件包 下载地址: http://download.oracle.com/otn/linux/oracle11g/R2/linux.x64_11gR2 ...