The Shortest Path in Nya Graph

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

Problem 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
题目大意:
给你一堆点。首先,这些点之间有一些双向边;然后,这些点都有自己的一个分层。相邻层的点互相之间可以花费代价c互相到达。求单源最短路。
 
最短路的建图问题。
给每层安排一个入点一个出点。入点到该层每点安排一条权值为0的单向边;出点到该层每点有一条反向的权值为0的单向边;出点有一条到相邻层入点的权值为c的单向边。

这样spfa就可以了。
注意一般的spfa用队列实现更稳定,效果更好。
 
//spfa用队列实现一般较快
//主要是一个建图的构思(是不是学完网络流会领会更多呢) #include <stack>
#include <queue>
#include <cstdio>
#include <cstring> using namespace std; const int maxn = ; int layer[maxn+]; struct
{
int to;
int w;
int next;
}edge[maxn*+];
//点的编号1..n 每层再安排一个出点n+1..n*2 一个入点n*2+1..n*3
int head[maxn*+];
int vis[maxn*+];
int dis[maxn*+]; int main()
{
int t,k=;
scanf("%d",&t);
while(t--)
{
int n,m,c;
scanf("%d%d%d",&n,&m,&c);
for(int i=;i<=n;i++)
scanf("%d",layer+i); //建边
memset(head,-,sizeof(head));
int cnt=;
for(int i=;i<m;i++)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
edge[cnt].to=b;edge[cnt].w=w;edge[cnt].next=head[a];head[a]=cnt++;
edge[cnt].to=a;edge[cnt].w=w;edge[cnt].next=head[b];head[b]=cnt++;
}
//每层安排一个入点一个出点
for(int i=;i<=n;i++)
{
int a,b,w;
a=i;b=n+layer[i];w=;
edge[cnt].to=b;edge[cnt].w=w;edge[cnt].next=head[a];head[a]=cnt++;
a=n*+layer[i];b=i;w=;
edge[cnt].to=b;edge[cnt].w=w;edge[cnt].next=head[a];head[a]=cnt++;
}
//额外安排的点相邻是C为代价的
for(int i=;i<n;i++)
{
int a,b,w;
a=n+i;b=n*+i+;w=c;
edge[cnt].to=b;edge[cnt].w=w;edge[cnt].next=head[a];head[a]=cnt++;
a=n+i+;b=n*+i;w=c;
edge[cnt].to=b;edge[cnt].w=w;edge[cnt].next=head[a];head[a]=cnt++;
}
//printf("11111\n"); //spfa开跑!
memset(vis,,sizeof(vis));
memset(dis,-,sizeof(dis));
queue<int> s;
s.push();
vis[]=;
dis[]=;
while(!s.empty())
{
int p=s.front();s.pop();
vis[p]=;
//printf("%d\n",p);
for(int i=head[p];i!=-;i=edge[i].next)
{
int v=edge[i].to,w=edge[i].w;
if(dis[v]==-||dis[v]>dis[p]+w)
{
dis[v]=dis[p]+w;
if(!vis[v])
s.push(v),vis[v]=;
}
}
}
//printf("22222\n"); printf("Case #%d: %d\n",k++,dis[n]);
}
return ;
}

hdu 4725 The Shortest Path in Nya Graph (最短路+建图)的更多相关文章

  1. Hdu 4725 The Shortest Path in Nya Graph (spfa)

    题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...

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

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

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

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

  6. (中等) 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 ...

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

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

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

随机推荐

  1. 突破至暗时刻,HCIE-RS的6个月成就之路

    我是今年四月份报的HCIE培训,到考完面试总共六个月的时间,对于HCIE整个考试的流程来说,六个月的时间不短,但也不是很长.尤其是面试,需要花费大量的时间和精力,下面我就把我整个备考历程做个简单的分享 ...

  2. selenium中延时等待三种方式

    selenium中的延时等待方式有三种:强制等待:sleep()  隐示等待:implicitly_wait()  显示等待 WebDriverWait() 1.强制等待:sleep(),time模块 ...

  3. 利用tomcat搭建图片服务器

    今天来教大家如何使用 tomcat 来搭建一个图片的服务器 1.先将tomcat解压一份并改名 2.此时apache-tomcat-8.5.43-windows-x64-file为图片服务器 依次打开 ...

  4. Java基础知识总结之类的集合

    Java集合概述 1.集合类也叫作容器类.它的功能相当于一个容器.可以存储数量不确定的数据,以及保存具有映射关系的数据(也被称为关联数组). 2.Java的集合(容器),它是用来”装对象的“(实际上是 ...

  5. JavaScript笔记五

    1.条件分支语句 - switch语句 - 语法: switch(条件表达式){ case 表达式: 语句... break; case 表达式: 语句... break; case 表达式: 语句. ...

  6. day 18 random模块 时间模块 sys模块 os模块

    import random 利用random模块可以进行从一个列表或者数字范围之间随机取出一个数字 # 取随机小数 : 数学计算 print(random.random()) # 取0-1之间的小数 ...

  7. Java基础面试题及答案(三)

    多线程 35. 并行和并发有什么区别? 并行是指两个或者多个事件在同一时刻发生:而并发是指两个或多个事件在同一时间间隔发生. 并行是在不同实体上的多个事件,并发是在同一实体上的多个事件. 在一台处理器 ...

  8. 堆的python实现及其应用

    堆的概念 优先队列(priority queue)是一种特殊的队列,取出元素的顺序是按照元素的优先权(关键字)大小,而不是进入队列的顺序,堆就是一种优先队列的实现.堆一般是由数组实现的,逻辑上堆可以被 ...

  9. MySQL主从介绍、配置主从、测试主从同步

    6月28日任务 说明:有不少同学不能一次性把实验做成功,这是因为还不熟悉,建议至少做3遍17.1 MySQL主从介绍17.2 准备工作17.3 配置主17.4 配置从17.5 测试主从同步有的同学,遇 ...

  10. lvm_lv_extend

    根分区lv扩容 xfs格式 neokylinV7.0 [root@localhost ~]# fdisk /dev/vda 欢迎使用 fdisk (util-linux 2.23.2). 更改将停留在 ...