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): 11694 Accepted Submission(s): 2537
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.
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.
If there are no solutions, output -1.
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
Case #2: 3
//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 (最短路+建图)的更多相关文章
- 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 ...
随机推荐
- 如何进行Flink项目构建,快速开发Flink应用程序?
项目模板 Flink应用项目可以使用Maven或SBT来构建项目,Flink针对这些构建工具提供了相应项目模板. Maven模板命令如下,我们只需要根据提示输入应用项目的groupId.artifac ...
- Fortran流程控制与逻辑运算、循环--xdd
1.IF语句 1 if() then ... end if 2 if() then ... else ... end if 3 if() then ... else if() then ... els ...
- /etc/security/limits.conf配置文件详解
这个文件主要是用来限制用户对系统资源的使用.是/lib64/security/pam_limits.so模块对应的/etc/serurity/pam_limits的配置文件. # /etc/secur ...
- 01-tornado学习笔记-Tornado简介
01-Tornado简介 Tornado是使用Python编写的一个强大的.可扩展的Web服务器.它在处理严峻的网络流量时表现得足够强健,但却在创建和编写时有着足够的轻量级,并能够被用在大量的应用 ...
- 为什么阿里巴巴Java开发手册中强制要求不要在foreach循环里进行元素的remove和add操作?
在阅读<阿里巴巴Java开发手册>时,发现有一条关于在 foreach 循环里进行元素的 remove/add 操作的规约,具体内容如下: 错误演示 我们首先在 IDEA 中编写一个在 f ...
- 全球 43 亿 IPv4 地址已耗尽!IPv6,刻不容缓
大家都知道目前网络协议使用的主要是 IPv4,全称为 Internet Protocol version 4,作用是为每一个网络和每一台主机分配一个 IP,IP 地址是一个 32 位的二进制数,算下来 ...
- https的安装(基于阿里云)
背景介绍:首先我的服务器在是阿里云的云服务器,web服务器使用的是nginx 进入到阿里云的ssl证书的管理界面,按需选择套餐后进行申请,申请完成后进行补全操作,最后是变成如下界面点击--下载进行证书 ...
- myql数据库,sql横排转竖排以及竖排转横排,oracle的over函数的使用
一.引言 前些日子遇到了一个sql语句的横排转竖排以及竖排转横排的问题,现在该总结一下,具体问题如下: 这里的第二题和第三题和下面所讲述的学生的成绩表是相同的,这里给大家留一下一个念想,大家可以自己做 ...
- Leecode_98_Validate_Binary_Search_Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- javascript基础修炼(13)——记一道有趣的JS脑洞练习题【华为云技术分享】
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/devcloud/article/detai ...