The Shortest Path in Nya Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13070    Accepted Submission(s): 2794

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725

Description:

This is a very easy problem, your task is just calculate el camino mas corto 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.

Input:

The first line has a number T (T <= 20) , indicating the number of test 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.

Output:

For test case X, output "Case #X: " first, then output the minimum cost moving from node 1 to node N.
If there are no solutions, output -1.

Sample Input:

2 3 3 3 1 3 2 1 2 1 2 3 1 1 3 3 3 3 3 1 3 2 1 2 2 2 3 2 1 3 4

Sample Output:

Case #1: 2

Case #2: 3

题意:

给出一个分层图,每个点只属于一层,点与点之间到达有一定花费,然后相邻两层移动也有一定花费。最后问从1号点到n号点的最小花费是什么。

题解:

朴素的想法就是点与点之间连边,层与层之间连边,但是空间会爆掉。

于是就像将“层”给分离出去,如果点i属于x层,就往n+x连边,这样会节约很多的空间。

但是这也有一个问题,就是跑最短路的时候点跑向相应的层,然后又跑回来。

所以我们考虑只连出边/入边,然后层直接与点相连。

具体代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 2e5+;
int n,m,c,t,tot;
int head[N],d[N],vis[N],belong[N],lay[N];
struct Edge{
int v,w,next ;
}e[N<<];
struct node{
int d,u;
bool operator < (const node &A)const{
return d>A.d;
}
};
void adde(int u,int v,int w){
e[tot].v=v;e[tot].w=w;e[tot].next=head[u];head[u]=tot++;
}
void Dijkstra(int s){
priority_queue <node> q;memset(d,INF,sizeof(d));
memset(vis,,sizeof(vis));d[s]=;
q.push(node{,s});
while(!q.empty()){
node cur = q.top();q.pop();
int u=cur.u;
if(vis[u]) continue ;
vis[u]=;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]>d[u]+e[i].w){
d[v]=d[u]+e[i].w;
q.push(node{d[v],v});
}
}
}
}
int main(){
cin>>t;
int cnt =;
while(t--){
cnt++;
scanf("%d%d%d",&n,&m,&c);
memset(head,-,sizeof(head));tot=;
memset(belong,,sizeof(belong));
memset(lay,,sizeof(lay));
for(int i=,tmp;i<=n;i++){
scanf("%d",&tmp);
lay[tmp]=;
belong[i]=tmp;
}
for(int i=;i<=n;i++){
if(!lay[i] || !lay[i-]) continue ;
adde(i+n,i+n-,c);
adde(i+n-,i+n,c);
}
for(int i=;i<=n;i++){
int tmp = belong[i];
adde(n+tmp,i,);
if(tmp>) adde(i,tmp+n-,c);
if(tmp<n) adde(i,tmp+n+,c);
//adde(i,n+tmp,0);
}
for(int i=,u,v,w;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
adde(u,v,w);adde(v,u,w);
}
Dijkstra();
printf("Case #%d: ",cnt);
if(d[n]==INF) puts("-1");
else cout<<d[n]<<endl;
}
return ;
}

HDU4725:The Shortest Path in Nya Graph(最短路)的更多相关文章

  1. HDU-4725 The Shortest Path in Nya Graph 最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 如果直接建图复杂度过大,但是考虑到每层之间的有效边很少,只要在每层增加两个虚拟节点n+i和2*n ...

  2. HDU-4725 The Shortest Path in Nya Graph (拆点+dji)

    HDU 4725 The Shortest Path in Nya Graph : http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意: 在一个图中跑最 ...

  3. ACM学习历程—HDU4725 The Shortest Path in Nya Graph(SPFA && 优先队列)

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  4. HDU4725 The Shortest Path in Nya Graph SPFA最短路

    典型的最短路问题,但是多了一个条件,就是每个点属于一个layer,相邻的layer移动,如x层移到x+1层需要花费c. 一种显而易见的转化是我把这些边都建出来,但是最后可能会使得边变成O(n^2); ...

  5. HDU4725 The Shortest Path in Nya Graph dij

    分析:对于每一层,原来n个点,然后扩展为原来的三倍,每一层扩展一个入点,一个出点,然后跑最短路 注:tmd我把一个n写成m了,然后wa了7次,我都要怀疑人生了 #include<cstdio&g ...

  6. hdu4725 The Shortest Path in Nya Graph【最短路+建图】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297574.html      ---by 墨染之樱花 题目链接:http://acm.hdu ...

  7. hdu4725 The Shortest Path in Nya Graph

    这道题看了下很多人都是把每一层拆成两个点然后建图做的. 我的思路很直接,也不用建图,直接在更新每个点时更新他相邻的边和相邻的层,当然前提是每个点只更新一次,每个层也只更新一次,这样才能确保时间复杂度. ...

  8. 2013成都邀请赛J称号||HDU4725 The Shortest Path in Nya Graph(spfa+slf最短的优化)

    职务地址:HDU 4725 这题卡了好长时间了,建图倒是会建,可是不会最短路的算法优化,本以为都须要堆去优化的,打算学了堆之后再来优化.可是昨晚CF的一道题..(那题也是不优化过不了..)然后我就知道 ...

  9. 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 ...

随机推荐

  1. windows环境下,用python绘图库matplotlib绘图时中文乱码问题

    1.下载中文字体(看自己爱好就行)下面这个举例: SimHei - Free Font Download​www.fontpalace.co 2.下载之后,打开即可安装,将字体安装进windows系统 ...

  2. chromedriver各个版本的下载

    驱动的下载地址如下: http://chromedriver.storage.googleapis.com/index.html 注意:64位向下兼容,直接下载32位的就可以啦,亲测可用.

  3. (数据科学学习手札27)sklearn数据集分割方法汇总

    一.简介 在现实的机器学习任务中,我们往往是利用搜集到的尽可能多的样本集来输入算法进行训练,以尽可能高的精度为目标,但这里便出现一个问题,一是很多情况下我们不能说搜集到的样本集就能代表真实的全体,其分 ...

  4. Windows 10 下如何彻底关闭 Hyper-V 服务(翻外篇)

    原文:Windows 10 下如何彻底关闭 Hyper-V 服务(翻外篇) windows禁用/启用hyper-V,解决hyper-V与模拟器同时启用时造成冲突 我是这样解决的,以管理员身份运行命令提 ...

  5. uber司机已经激活了,就是还没有上传头

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. express与ejs,ejs在Linux上面的路径问题

    1.学习使用ejs模板(这个是ejs.js) var express = require('express'); var app = express(); app.set("view eng ...

  7. springmvc+spring-data-jpa+hibernate环境搭建与配置

    1.JPA诞生的缘由是为了整合第三方ORM框架,建立一种标准的方式,百度百科说是JDK为了实现ORM的天下归一,目前也是在按照这个方向发展,但是还没能完全实现.在ORM框架中,Hibernate是一支 ...

  8. 安装mathtype出问题卸载后 office2016打开mathtype弹错误窗口

    解决方法:找到 C:\Program Files (x86)\Microsoft Office\root\Office16\STARTUP目录下的MathType Commands 6 For Wor ...

  9. 名片管理系统demo

    # 定义一个列表,用来储存名片 def cardInfors(): # 打印功能提示 print('欢迎使用名片管理系统v6.6.6') print('1:添加一个名片') print('2:删除一个 ...

  10. 《python核心编程第二版》第5章习题

    5-1 整形 讲讲 Python 普通整型和长整型的区别 答:普通整型 32位,长整数类型能表达的 数值仅仅与你的机器支持的(虚拟)内存大小有关 5-2 运算符(a) 写一个函数,计算并返回两个数的乘 ...