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. vue路由传参刷新丢失

    没有系统学习过vue,以前使用路由传参都是直接this.$router.push({name:'main',params:{'id': 123}})的,没有在路由定义中配置参数,如下: router: ...

  2. code migrate

    1. 从Git上clone 仓库到本地. git clone --mirror http://gittest:gittest@192.168.1.x/x.git 2. push 到codecommit ...

  3. 微服务中的Kafka与Micronaut

    今天,我们将通过Apache Kafka主题构建一些彼此异步通信的微服务.我们使用Micronaut框架,它为与Kafka集成提供专门的库.让我们简要介绍一下示例系统的体系结构.我们有四个微型服务:订 ...

  4. Java中的Calendar 类和SimpleDateFormat 类

    1.Calendar 类:import java.util.Calendar;               Calendar cal = Calendar.getInstance();        ...

  5. day20191205笔记

    Tips: 1.课堂效率 2.每天回顾昨天学习内容,趁热打铁+查漏补缺.(上课笔记,回去补充.) 默写: 1.请说出(访问修饰符)作用域public,private,protected,以及不写时的区 ...

  6. Java 虚拟机结构

    一 数据类型 与 Java 程序语言中的数据类型相似,Java 虚拟机可以操作的数据类型可分为两类:原始类型(Primitive Types,也经常翻译为原生类型或者基本类型)和引用类型(Refere ...

  7. html代码/如何做到有横线无竖线的表格/或横线有颜色/竖线没颜色

    改变它的css样式,table{ border-collapse:collapse;}table tr td{ border-bottom:1px solid #dedede;}

  8. 利用scatter()绘制颜色映射的二次方曲线

    程序如下: import matplotlib.pyplot as plt x_value = list(range(1, 1001)) y_value = [x**2 for x in x_valu ...

  9. win10 安装 MySQL-5.7.28 记录

    目录 一.安装前准备 二.安装步骤 三.安装时踩的坑 一.安装前准备 1.云盘下载安装包以及客户端工具 下载地址:MySQL-5.7.28 + SQLyog 2.官网下载安装包 下载地址:https: ...

  10. python原类、类的创建过程与方法

    今天为大家介绍一下python中与class 相关的知识-- 获取对象的类名 python是一门面向对象的语言,对于一切接对象的python来说,咱们有必要深入的学习与了解一些知识 首先大家都知道,要 ...